NullPointer Exception In running testCases via Selenium webDriver in Chrome - selenium

I am trying to run my test cases on Chrome and I had copied the path in the Properties file,but still console is throwing annoying statements like:
ERROR: The path to the chromedriver executable must be set by the webdriver.chrome.driver system property; for more information, see http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://code.google.com/p/chromium/downloads/list
FAILED CONFIGURATION: #BeforeTest startWebSession
java.lang.NullPointerException

One thing I have found is that the Chrome driver cannot be started from within Eclipse. It must be run from a command prompt. At least on Windows 7 64-bit.
Trying to run it from within Eclipse produces this exception:
Exception in thread "main" java.lang.IllegalStateException: The webdriver.chrome.driver system property defined chromedriver executable does not exist: C:\Windows\System32\chromedriver.exe
This problem only occurs for Chrome. IE and FireFox work fine from within Eclipse.

Download the chrome driver from http://code.google.com/p/chromedriver/downloads/list
Initialize your driver object in the following manner -
System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
By doing this the chrome driver works properly.

This is how do I initialize the ChromeDriver:
public RegulationUI() throws Exception{
ChromeDriverService service = ChromeDriverService.createDefaultService();
File file = new File(RegulationUI.class.getResource("/chromedriver.exe").toURI());
System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, file.getAbsolutePath());
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
driver = new ChromeDriver(service,options);
}
BTW my test class is named RegulationUI
Try this, it works for me and moreover, I know that this is "multicomputer" solution - our project is in subversion and this way everybody can run it, even if we have differently setup where exactly on disk the "working folder" for IDE is

Please download chromedriver.exe for Google chrome browser
please download IEdriver.exe for Internet explore.
Please And kept these files in a root folder of windows for simplicity. Lets consider your operating systems installed on c:\ (C Driver) create a folder name Selenium on C-Drive and Kept these binary(.exe) files. like c:\selenium
in your Testcase/testScript Write as
//For Chrome Browser:
Webdriver driver = new ChromeDriver();
java.io.File file = new File("c:\\selenium\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());

If you are using maven then try to use following in your pom:
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>RELEASE</version>
</dependency>
and use it like this for chrome in your setup:
ChromeDriverManager.getInstance().setup();
driver = new ChromeDriver();

Related

Unable to find a matching set of capabilities with selenium 3.8.1 and gecko driver 0.19.0

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Webdriver {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver", "C:\\Selenium\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
//System.setProperty("webdriver.chrome.driver","C:\\Selenium\\chromedriver.exe");
//WebDriver driver = new ChromeDriver();
driver.get("https://maps.mapmyindia.com");
Thread.sleep(2000);
driver.findElement(By.id("auto")).sendKeys("TCS");
Thread.sleep(2000);
driver.findElement(By.id("auto_geo")).click();
When i run this code on eclipse luna there is an error: Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Unable to find a matching set of capabilities
new FirefoxDriver(DesiredCapabilities caps);
is deprecated, use
FirefoxOptions options = new FirefoxOptions();
options.setCapability("marionette", false);
WebDriver webDriver = new FirefoxDriver(options);
and you are good to go
Another possible cause is the outdated Firefox version.
I upgraded the version and it works fine!
I could open the browser only setting options.setCapability("marionette", true);, then in the open window I upgraded through the "About Firefox" dialog. Then you have to remove the line about marionette.
Probably the one I had was only going to work with marionette while we are trying to use it with geckodriver, which has a different protocol. Anyone who knows more than me can confirm or deny!
SessionNotCreatedException
SessionNotCreatedException extends WebDriverException and is a RuntimeException which indicates that a session could not be created.
Possible Causes :
The possible causes of a new session not getting created are as follows :
Compatibility issues between JDK, Selenium, WebDriver and Web Browser versions.
Accessing the same port number by GeckoDriver or Marionette by the new session which previous session have't released yet.
Lack of access to CPU
Lack of Physical Memory
Lack of Swap Memory
Lack of Disc Cache
Lack of Network Bandwidth
Presence of OS chores within the system.
Code Block :
I don't see any coding issue in your code block as such.
Solution :
The simple solution would be as follows :
Always use the latest released version of JDK (Java SE 9.0.1), Selenium-Java client (v3.8.1), WebDriver variant (GeckoDriver v0.19.1) and Web Browser (Firefox Quantum Browser).
If the base version of the Web Browser is too old consider uninstalling the Browser through Revo Uninstaller and install a recent released GA version of Firefox Browser.
Always use quit() in the tearDown() method so that the webdriver and the webclient both are properly destroyed.
Clean the Project Workspace from your IDE before and after executing your Test Suite.
Clear the Browser Cache before and after the execution of your Tests.
Use CCleaner tool regularly to wipe away the OS chores.
Execute your Test.
You should add capabilities for firefox. Please modify your code as follows
System.setProperty("webdriver.gecko.driver", "C:\\Selenium\\geckodriver.exe");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities = DesiredCapabilities.firefox();
capabilities.setBrowserName("firefox");
capabilities.setVersion("your firefox version");
capabilities.setPlatform(Platform.WINDOWS);
capabilities.setCapability("marionette", false);
WebDriver driver = new FirefoxDriver(capabilities);
driver.get("https://maps.mapmyindia.com");
If your windows is windows_nt then put windows_nt instead of windows

ClassNotFoundException using Selenium Standalone Server

I have a grails/groovy project that has one feature that uses selenium standalone server to do some test automation.
I keep getting this error running my grails project in production mode. If I run it directly from IntelliJ it works perfectly so I'm not sure what exactly is wrong.
Here is the relevant code:
WebDriver driver
DesiredCapabilities capability = DesiredCapabilities.chrome()
capability.setBrowserName("chrome")
capability.setPlatform(Platform.WINDOWS)
driver = new RemoteWebDriver( new URL("http://192.168.83.124:4444/wd/hub"), capability);
I'm getting a ClassNotFoundException error on the last line.
In my libraries for the project I'm using selenium-java-2.53.0.jar, selenium-server-standalone-2.53.0.jar and sources has selenium-java-2.53.0-srcs.jar
I have absolutely no idea what's wrong and I've searched the internet exhaustively to find a solution but have found nothing. Any help is appreciated.
Here is the full text of the error:
2017-12-08 15:59:56,381 [http-bio-8080-exec-1] ERROR
errors.GrailsExceptionResolver - ClassNotFoundException occurred when processing request: [GET] /Diversotron-0.1/api/edit
com.google.common.base.Function. Stacktrace follows:
java.lang.ClassNotFoundException: com.google.common.base.Function
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2585)
at java.lang.Class.getDeclaredConstructors(Class.java:1906)
at
org.codehaus.groovy.util.LazyReference.getLocked(LazyReference.java:46)
at org.codehaus.groovy.util.LazyReference.get(LazyReference.java:33)
at diversotron.PropotronService.setPropotron(PropotronService.groovy:25)
at diversotron.ApiController.edit(ApiController.groovy:53)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
It's because Selenium isn't declared in my BuildConfig.groovy file. That's why it ran via IntelliJ but not in production. Doing a grails prod war will not include the libraries. They have to be specified in the buildconfoig.groovy file.
I had to add these lines.
def seleniumVersion = "2.53.0"
and in dependencies I had to add this:
runtime "org.seleniumhq.selenium:selenium-chrome-driver:$seleniumVersion"
Hopefully this will help someone else down the line!
Map chrome Options = new Map();
ChromeOptions.put("binary","/use/lib/chromium-browser/chromium-browser");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
Web Driver driver = new Chrome Driver(capabilities);
try this

Exception in thread "main" java.lang.IllegalStateException even when path of driver is set

I am getting below exception when I am running a selenium browser initialization using java main method. The driver is available at the right path.
The path to the driver executable must be set by the
webdriver.chrome.driver system property; for more information, see
https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest
version can be downloaded from
http://chromedriver.storage.googleapis.com/index.html at
com.google.common.base.Preconditions.checkState(Preconditions.java:738)
at
org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124)
at
org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:32)
at
org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137)
at
org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:330)
at
org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88)
at
org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:124)
at
invokebrowser.myfirsttestcase.initBroiwser(myfirsttestcase.java:23)
at invokebrowser.myfirsttestcase.main(myfirsttestcase.java:16)
Code Snippet
System.setProperty("Webdriver.chrome.driver","C:\\Javalibs\\chromedriver.exe");
WebDriver driver = new ChromeDriver();//getting exception here
In the code, Webdriver.code.driver..., W is mentioned in capital letter. It has to be in small letter as shown in the error message.
System.setProperty("webdriver.chrome.driver","C:\\Javalibs\\chromedriver.exe");
Hope this helps you. Thanks.
Here is the Answer to your Question:
While you work with Selenium 3.4.0, chromedriver 2.29 & Chrome 58.x you have to specify the absolute path of the chromedriver through System.setProperty
It's worth mentioning that the System Property is webdriver.chrome.driver
While you mention the absolute path of the chromedriver through System.setProperty, you have to either provide front slashes "/" or escape the back slashes "\\"
Your own code block will work for you with this simple twist:
System.setProperty("webdriver.chrome.driver","C:\\Javalibs\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
OR
System.setProperty("webdriver.chrome.driver","C:/Javalibs/chromedriver.exe");
WebDriver driver = new ChromeDriver();
Let me know if this Answers your Question.

firefox marionette bypass security exception

I am using Marionette / Geckodriver v9.0 for mac with selenium jar 2.53.1.
When it opens firefox 47 I'm getting the error that "Your connection is not secure".
The code I'm using to create the driver is:
FirefoxProfile firefoxProfile = null;
firefoxProfile = new FirefoxProfile();
firefoxProfile.setAcceptUntrustedCertificates(true);
firefoxProfile.setAssumeUntrustedCertificateIssuer(false);
DesiredCapabilities capability = DesiredCapabilities.firefox();
capability.setCapability("marionette", true);
capability.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
capability.setCapability("handlesAlerts", true);
return new MarionetteDriver(capability);
So, how do I get it to add my website as an exception or turn off the check? Or is it a feature that Marionette hasn't implemented yet?
Firefox: 50.1.0
Gecko Driver: 0.13 (install nuget package: Selenium.Firefox.WebDriver.0.13.0)
Firstly, open a standard Firefox browser, and make sure the default Firefox profile has added the unsecured site into the certificate exception list.
C# code as follows:
FirefoxProfileManager fpManager = new FirefoxProfileManager();
var profiles = fpManager.ExistingProfiles;
var defaultProfile = fpManager.GetProfile(profiles.First());
driver = new FirefoxDriver(defaultProfile);
You have to use nightly build of firefox of developer edition for now. It will not work on the current version.
https://bugzilla.mozilla.org/show_bug.cgi?id=1103196

Unable to start operaBlink through selenium webdriver

I am trying to start opera(v 36.0) using selenium 2.53.0 and Opera chromium driver _win64 (0.2.2).
When I try to run sample code, an exception occurred and browser didn't started.
Sample code:
File pathToOpera = new File("E://operadriver.exe");
DesiredCapabilities cap = DesiredCapabilities.operaBlink();
cap.setCapability("webdriver.opera.driver",pathToOpera);
driver = new OperaDriver(cap);
I am getting error as
java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.opera.driver system property; for more information, see https://github.com/operasoftware/operachromiumdriver. The latest version can be downloaded from https://github.com/operasoftware/operachromiumdriver/releases
at com.google.common.base.Preconditions.checkState(Preconditions.java:199)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109)
at org.openqa.selenium.opera.OperaDriverService.access$0(OperaDriverService.java:1)
at org.openqa.selenium.opera.OperaDriverService$Builder.findDefaultExecutable(OperaDriverService.java:118)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296)
at org.openqa.selenium.opera.OperaDriverService.createDefaultService(OperaDriverService.java:82)
at org.openqa.selenium.opera.OperaDriver.<init>(OperaDriver.java:137)
My execution environment: win 8, 64 bit
Add before Your code:
System.setProperty("webdriver.opera.driver","E://operadriver.exe");
System.setProperty("opera.binary","E://yourOperaPath.exe");