Wednesday, February 10, 2016

Importing Selenium Javadocs to IDE (eclipse in this case)

Whenever you need help on selenium it's painstaking to browse javadocs vis web.
Here's how to add java doc's to your IDE (Eclipse in my case)

1. Go to http://www.seleniumhq.org/download/ . Right click on Javadocs link and copy the link address















2. Go to eclipse > Right click on project that you want to add Javadocs to > Select properties

3. On Properties window select "Java Build Path" from left navigation

4. On Java Build Path window select "Libraries" tab and go down and find the selenium-java-XXXX. jar  file (in my case XXXX-2.50.0)


5. Expand the JAR file select "javadoc location:" and click Edit button


6. Enter copied javadocs URL (http://seleniumhq.github.io/selenium/docs/api/java/index.html) and remove index.html > Click OK

7. Now you get the definitions of webdriver functions inside the eclipse

Sunday, February 7, 2016

How to open Google Chrome browser with Selenium web driver

If you want to open a Firefox browser and navigate to www.google.com following code will work


import org.openqa.selenium.firefox.FirefoxDriver;


public class chrome_test {

public static void  main (String[] args){

FirefoxDriver browser = new FirefoxDriver ();
browser.get("http://www.google.com");

}


}


Let's try the same format to open a Chrome browser


import org.openqa.selenium.chrome.ChromeDriver;




public class chrome_test {

public static void  main (String[] args){

ChromeDriver browser = new ChromeDriver ();
browser.get("http://www.google.com");

}


}


Once you run the code you will get an exception (I'm using Eclipse for compiling)

Exception in thread "main" java.lang.IllegalStateException: 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:199)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:109)
at org.openqa.selenium.chrome.ChromeDriverService.access$0(ChromeDriverService.java:1)
at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:296)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:116)
at chrome_test.main(chrome_test.java:8)



Why is this...?


This is because even though you can start a Firefox driver directly, you'll need a specific web driver provided by Google to start your Chrome browser from selenium.


How to resolve this..?


  1. Go to https://sites.google.com/a/chromium.org/chromedriver/downloads
  2. Select the latest driver
  3. Download the .zip file for your operating system
  4. Unzip the file to your machine (anywhere u wish). This is a file named chromedriver.exe (lets think that I unzipped it at D:\Selenium)
  5. Now go back to code and add the following line (with your location of .exe file)
  6. Remember to modify path with double "\" as you can see below.


System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver.exe");

Save and Run
It will work

FULL CODE

import org.openqa.selenium.chrome.ChromeDriver;


public class chrome_test {

public static void  main (String[] args){

System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\chromedriver.exe");
ChromeDriver browser = new ChromeDriver ();
browser.get("http://www.google.com");

}


}