Skip to main content

Development

Configure Selenium Tests in Multiple Browsers

shutterstock_106257662_smallThis post will cover how to configure tests to run in multiple browsers using JUnit. Consider any testing framework: it should have a Driver script and a Driving script. The Driver script is the starting point of a test. The flow of a test execution usually begins from the Driver script. We can configure the Driver script in such a way to handle multiple browsers all by itself using the power of annotations and leaving the business logic alone to the user for implementing. In order to achieve that, we need a basic understanding of the JUnit annotations.

@BeforeClass: Any function inside this annotation is the first to run before any of classes/methods start executing. All the initial configuration must be done here.

@AfterClass: This is usually the last method to run after all other methods/tests have completed executing, functions inside this annotation execute to complete the test.

@Before: Methods inside this annotation would execute every time before a method in @Test runs.

@After: Methods inside this annotation would execute every time after a method in @Test runs.

With the help of these annotations in JUnit, we can configure our tests to run in Multiple browsers without worrying about initialization of different browsers every time.

Here’s an example where we have a Driver script and Driving script to run our tests:

DriverScript.java

import java.util.Arrays;
import java.util.Collection;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
 

@RunWith(Parameterized.class)
public class Driverscript {
 
public static WebDriver driver;
public String browser;
 

@BeforeClass
public static void init() {
//initial code to setup environment variables
}

 

@Parameters
public static Collection getBrowser(){
return Arrays.asList(new Object[][] {{“FF”},{“Chrome”},{“IE”}});
}

 

public Driverscript(String browser){
this.browser= browser;
}

 

@Before
public void setup(){
System.out.println(“Browser:”+ browser);
if(browser.equalsIgnoreCase(“FF”)){
driver = new FirefoxDriver();
} else if(browser.equalsIgnoreCase(“IE”)){
System.setProperty(“webdriver.ie.driver”,”C:\\IEDriverServer.exe”);
driver = new InternetExplorerDriver();
} else if(browser.equalsIgnoreCase(“Chrome”)){
System.setProperty(“webdriver.chrome.driver”, “C:\\chromedriver.exe”);
driver = new ChromeDriver();
}
}

 

@Test
public void sampleTest(){
Drivingscript drScript = new Drivingscript();
drScript.main(driver);
}
}

 

DrivingScript. Java

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.Reporter;

 
public class Drivingscript {
public void main(WebDriver driver){
try {
driver.get(“http://newtours.demoaut.com”);
driver.findElement(By.name(“username”)).sendKeys(“mercury”);
driver.findElement(By.name(“password”)).sendKeys(“mercury”);
driver.findElement(By.name(“login”)).click();
driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
Assert.assertTrue(driver.getTitle().equals(“Home Page”));
Reporter.log(“Assertion Passed”);
driver.findElement(By.linkText(“(Logout)”)).click();
} catch(Exception e){
e.printStackTrace();
}
}
}

If your run the driverscript.java via JUnit the following sequence of actions happen

  1. Since the class is declared with @RunWith(parameterized.class) this class expects parameters from @Parameter annotation.
  2. First parameter – FF is passed from the @Parameters annotation to the constructor.
  3. @Before now accepts the parameter from the constructor and initializes the respective browser and @Test executes with FF browser.
  4. @After now terminates the browser
  5. Second parameter – Chrome is passed from the @Parameters annotation to the constructor.
  6. @Before now accepts the parameter from the constructor and initializes the respective browser and @Test executes with Chrome browser.
  7. @After now terminates the browser.
  8. Third parameter – IE is passed from the @Parameters annotation to the constructor.
  9. @Before now accepts the parameter from the constructor and initializes the respective browser and @Test executes with IE browser.
  10. @After now terminates the browser

 

Configure Tests to run in Multiple Browsers using TestNg

Multiple browser testing can also be configured in TestNG an another testing framework. Before going into the details, understanding of basic annotations in TestNG would be useful.

@BeforeClass: Any function inside this annotation is the first to run before any of classes/methods start executing. All the initial configuration must be done here.

@AfterClass: This is usually the last method to run after all other methods/tests have completed executing, functions inside this annotation executes to complete the test.

@BeforeMethod: Methods inside this annotation would execute every time before a method in @Test runs.

@AfterMethod: Methods inside this annotation would execute every time after a method in @Test runs.

@BeforeTest: This annotation is configured to run only once before any of the methods in @Test starts executing. So any methods inside this annotation would run only once in the cycle.

@AfterTest: This annotation is configured to run only once after all the tests annotated with @Test has finished executing. Even this annotation would execute only once in the cycle.

With the help of these annotations in TestNg, we can configure our tests to run in Multiple browsers without worrying about initialization of different browsers every time.

Here goes a example where we have a driver script , driving script and testng suite.xml to run our tests.

DriverScript.java

Package com.test;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

 
public class Driverscript {
public static WebDriver driver;

@BeforeClass
public static void setup(){
//code for initial setting like environment variables
}

@BeforeMethod

@Parameters ({“browser”})
public void init(String browser){
if(browser.equalsIgnoreCase(“FF”)){
driver = new FirefoxDriver();
} else if(browser.equalsIgnoreCase(“IE”)){
System.setProperty(“webdriver.ie.driver”,“C:\\IEDriverServer.exe”);
driver = new InternetExplorerDriver();
} else if(browser.equalsIgnoreCase(“Chrome”)){
System.setProperty(“webdriver.chrome.driver”, “C:\\chromedriver.exe”);
driver = new ChromeDriver();
}
}

@Test
public void sampleTest(){
Drivingscript drScript = new Drivingscript();
drScript.main(driver);
}

 

@AfterMethod
public void tearDown(){
driver.close();
}

 

@AfterClass()
public static void finish(){
driver.quit();
}
}

 

DrivingScript. Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.Assert;
import org.testng.Reporter;
 

public class Drivingscript {
public void main(WebDriver driver){
try {
driver.get(“http://newtours.demoaut.com”);
driver.findElement(By.name(“username”)).sendKeys(“mercury”);
driver.findElement(By.name(“password”)).sendKeys(“mercury”);
driver.findElement(By.name(“login”)).click();
driver.manage().timeouts().pageLoadTimeout(5, TimeUnit.SECONDS);
Assert.assertTrue(driver.getTitle().equals(“Home Page”));
Reporter.log(“Assertion Passed”);
driver.findElement(By.linkText(“(Logout)”)).click();
} catch(Exception e){
e.printStackTrace();
}
}
}

 

Suite.XML
<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd”>
<suite name=”Sample” parallel=”none”>
<test verbose=”1″ name=”FF Test”>
<parameter name=”browser” value=”FF”/>
<classes>
<class name=”com.test.Driverscript”/>
</classes>
</test>
<test verbose=”1″ name=”Chrome Test”>
<parameter name=”browser” value=”Chrome”/>
<classes>
<class name=”com.test.Driverscript”/>
</classes>
</test>
<test verbose=”1″ name=”Internet Explorer Test”>
<parameter name=”browser” value=”IE”/>
<classes>
<class name=”com.test.Driverscript”/>
</classes>
</test>
</suite>
 

If your run the suite.xml via testing the following sequence of actions happen

  1. Parameter browser with value FF is passed from the first test in the suite to the @BeforeMethod.
  2. Firefox browser is initialized for the first time and the @Test executes in Firefox browser.
  3. @AfterMethod executes immediately after that and halts the Firefox browser.
  4. Parameter browser with value Chome is passed from the second test in the suite to the @BeforeMethod.
  5. Chrome browser is initialized for the second time and the @Test executes in Chrome browser.
  6. @AfterMethod executes immediately after that and halts the Chrome browser.
  7. Parameter browser with value IE is passed from the third test in the suite to the @BeforeMethod.
  8. InternetExplorer browser is initialized for the third time and the @Test executes in Internet Explorer browser.
  9. @AfterMethod executes immediately after that and halts the Internet Explorer browser.

 

Thoughts on “Configure Selenium Tests in Multiple Browsers”

  1. What is the difference between JUnit and TestNG.
    Which is easy and better to use?
    Kindly let us know if there are any advantages of one over the other.

  2. Daniel Vivek Raman Post author

    Junit and TestNG , both are test Frameworks but TestNG is advanced where in we have extra options to play with, including reports when compared to JUnit

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Daniel Vivek Raman

More from this Author

Categories
Follow Us