Software Testing Learning Hub

Selenium Lesson 7 – Execute Selenium Script on Different Browsers

Lesson 7 -Selenium Scripts on different browsers

In this lesson we will see how we can execute the same test case on different browser. While Defining the Automation Test Strategy we do consider the Test Execution on different internet browsers and versions of it. During implementation we should know how to do these things and if you are new to Selenium webdriver then it little bit to find these code at one place. Automation Testers who just started automating the test cases using selenium webdriver often don’t know how to run the test on different browsers. In this post I will cover the how to launch the Tests on

  1. Chrome
  2. Internet Explorer (IE)
  3. Firefox
  4. Safari.

 

1) Chrome: Standard Selenium Webdriver bundle does not contain the Chrome Driver executable. You need to download the executable and need to put inside your project.
Step 1: Download the Chrome Webdriver
Location from where you can download is:  https://code.google.com/p/chromedriver/downloads/list?can=1&q=

Step 2: Define the ChromeDriver Path: Either you set the path inside system PATH or you can define it inside the code. Use following code.

System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

Step 3: Creating an Object of Chrome Driver: now you are all set for the Launching the Chrome. Create an object and write a code on top of it.

WebDriver driver = new ChromeDriver ();

 

Code: Example:

@Before
public void createChromeDriver() 
{
System.setProperty("webdriver.chrome.driver","D:/Selenium/drivers/chromedriver.exe ");
WebDriver driver = new ChromeDriver();

@Test
public void searchonGoogle() 
{
driver.get("http://www.google.com");
driver.findElement(By.name("q")).sendKeys("Qeworks");
driver.findElement(By.name("btnG")).click();
new WebDriverWait(driver, 3)
.until(ExpectedConditions.titleIs("QeWorks - Software Testing Website"));
}

2) Internet Explorer (IE): this is same as Chorme WebDriver. Its Executable is not included in the Standard Selenium Webdriver bundle. Same Steps we will follow for IE.

Step 1: Download the IE Webdriver
Location from where you can download is: https://code.google.com/p/selenium/downloads/list
Step 2: Define the IE Driver Path: Either you set the path inside system PATH or you can define it inside the code. Use following code.

System.setProperty("webdriver.ie.driver", "/path/to/IEDriver");

Step 3: Creating an Object of IE Driver: now you are all set for the Launching the Chrome. Create an object and write a code on top of it.

WebDriver driver = new InternetExplorerDriver ();

Code: code will similar to the code which we have written for chrome.

@Before
public void createIEDriver() 
{
System.setProperty("webdriver.ie.driver","D:/Selenium/drivers/IEDriver.exe ");
WebDriver driver = new InternetExplorerDriver ();

@Test
public void searchonGoogle() 
{
driver.get("http://www.google.com");
driver.findElement(By.name("q")).sendKeys("Qeworks");
driver.findElement(By.name("btnG")).click();
new WebDriverWait(driver, 3)
.until(ExpectedConditions.titleIs("QeWorks - Software Testing Website"));
}

 

Now we will discuss the simple ones i.e Safari and Firefox. Why simple? Your really don’t need to do much code. First of all. These browser executable are part of the standard Selenium webdriver bundle.  so now you need to just invoke them.
3) Safari: Starting with Selenium 2.30.0, the SafariDriver comes bundled with the Selenium server.

 

@before
public void createSafariDriver() {
assumeTrue(isSupportedPlatform());
driver = new SafariDriver();

 

4) Firefox: this is most simpler one. if you just record the script using selenium IDE and export the same in JAVA Webdriver, you will get the code to launch the Firefox browser.

Step 1: Download the Gecko Webdriver

Step 2: Define the gecko Driver Path: Either you set the path inside system PATH or you can define it inside the code. Use following code.

System.setProperty("webdriver.gecko.driver", "/path/to/gecko driver");

Step 3: Creating an Object of firefox Driver: now you are all set for the Launching the Chrome. Create an object and write a code on top of it.

@before
public void createfirefoxDriver() {
driver = new FirefoxDriver();
}

 

I hope this post will helpful for beginners. Stay connected… more advance topics are coming soon…