WebDriver

How to Configure Selenium WebDriver

Hola Testers,

Today I’m going to to tell how to configure Selenium WebDriver. Yes, this is the first lesson for Selenium WebDriver. I hope this will be useful for new users. In here, I’m going to use Java as my programming language. I’m going to divide this tutorial for below partitions.

  • Create a project and configure .jar files
  • Configure TestNG
  • Configure Firefox browser
  • Configure Chrome browser
  • Configure Internet Explore browser

Create a project and configure .jar files

To create a project we need programming IDE. Here I’m using eclipse.

  • Open Eclipse
  • Create a new Java Project

Screen Shot 2017-04-11 at 5.25.04 PM

After create a java project we need to download selenium webdriver jars.

  • Go to seleniumhq download page
  • Download and unzip the WebDriver bindings

Screen Shot 2017-04-11 at 4.19.34 PM.png

  • Create a folder called “libs” in java project (right click on the project > New > Folder)

Screen Shot 2017-04-11 at 5.37.45 PM

  • Paste all the .jar files from extracted folder of the downloaded zip file

Screen Shot 2017-04-11 at 5.45.01 PMScreen Shot 2017-04-11 at 5.46.12 PM

Now we have to add those .jar files to build path.

Select all .jar files in “libs” folder > right click > Build Path > Add to Build Path

Screen Shot 2017-04-11 at 5.49.22 PM

  • After that you can see all the .jar files in newly created “Referenced Libraries” library folder

Screen Shot 2017-04-11 at 5.49.37 PM

Configure TestNG

Click on Help menu > Install New Software..

Screen Shot 2017-04-11 at 6.00.43 PM.png

  • Click on Add button, “Add Repository” box will appear.
  • Enter below values as inputs and click “OK”
    • Name: TestNG
    • Location: http://beust.com/eclipse

Screen Shot 2017-04-11 at 6.02.59 PM

  • Accept agreement details and install TestNG. (If it gives warning, click OK)
  • After successful installation system ask to restart eclipse, allow it.

Screen Shot 2017-04-11 at 6.15.04 PM.png

 

Configure Firebox browser

  • Download geckoDriver from seleniumhq download page Third Party Drivers, Bindings and Plugins section and unzip it

Screen Shot 2017-04-11 at 9.19.58 PM

  • Create a folder called “driver” in project and add unzipped file. Don’t copy through eclipse.

Screen Shot 2017-04-11 at 9.40.04 PM.png

  • Create a class
  • Import Firefox browser driver
import org.openqa.selenium.firefox.FirefoxDriver
  • Initialize FirefoxDriver object
System.setProperty("webdriver.gecko.driver", "path of geckodrivergeckodriver"); 
FirefoxDriver fd=new FirefoxDriver();

Sample code

@Test

  public void firefoxTest() {

 System.setProperty("webdriver.gecko.driver", "/Users/deranthika/Documents/eclipse/workspace/TestSelenium/drivers/geckodriver");

FirefoxDriver fd=new FirefoxDriver();

fd.get("http://google.lk");

  }

If you are using windows OS, you need to put location as below

System.setProperty("webdriver.gecko.driver","path of geckodriver.exe");

Screen Shot 2017-04-11 at 8.22.38 PM.png

Screen Shot 2017-04-11 at 9.42.55 PM.png

 

Configure Chrome browser

  • Instead of gecko, you need to download chromeDriver from seleniumhq download page
  • Download the latest stable version for your OS and copy it to drivers folder
  • Import chromeDriver
import org.openqa.selenium.chrome.ChromeDriver;
  • Create new test method
  • Initailize chromeDriver object
System.setProperty("webdriver.chrome.driver", "/Users/deranthika/Documents/eclipse/workspace/TestSelenium/drivers/chromedriver");

ChromeDriver cd=new ChromeDriver();

sample code

@Test

  public void chromeTest() {

 System.setProperty("webdriver.chrome.driver", "/Users/deranthika/Documents/eclipse/workspace/TestSelenium/drivers/chromedriver");

ChromeDriver cd=new ChromeDriver();

cd.get("http://google.lk");

  }

Screen Shot 2017-04-11 at 9.58.12 PMScreen Shot 2017-04-11 at 9.58.25 PM

Configure Internet Explorer browser

  • you need to download InternetExplorerDriver from seleniumhq download page
  • Download the latest stable version and copy it to drivers folder
  • Import InternetExplorerDriver
import org.openqa.selenium.ie.InternetExplorerDriver;
  • Create new test method
  • Initailize InternetExplorerDriver object
System.setProperty("webdriver.ie.driver", "C:\\Selenium\\IEDriverServer.exe");

InternetExplorerDriver id= new InternetExplorerDriver();

sample code

@Test

  public void chromeTest() {

System.setProperty("webdriver.ie.driver", "C:\\Selenium\\IEDriverServer.exe");

InternetExplorerDriver id= new InternetExplorerDriver();

id.get("http://google.lk");

  }

Untitled.png

I’ll add more browsers later 😉