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
After create a java project we need to download selenium webdriver jars.
- Go to seleniumhq download page
- Download and unzip the WebDriver bindings
- Create a folder called “libs” in java project (right click on the project > New > Folder)
- Paste all the .jar files from extracted folder of the downloaded zip file
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
- After that you can see all the .jar files in newly created “Referenced Libraries” library folder
Configure TestNG
Click on Help menu > Install New Software..
- Click on Add button, “Add Repository” box will appear.
- Enter below values as inputs and click “OK”
- Name: TestNG
- Location: http://beust.com/eclipse
- Accept agreement details and install TestNG. (If it gives warning, click OK)
- After successful installation system ask to restart eclipse, allow it.
Configure Firebox browser
- Download geckoDriver from seleniumhq download page Third Party Drivers, Bindings and Plugins section and unzip it
- Create a folder called “driver” in project and add unzipped file. Don’t copy through eclipse.
- 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");
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"); }
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"); }
I’ll add more browsers later 😉