Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20



Running Selenium Tests on different browsers using Junit

Many web applications need to be tested on different browsers to check their compatibilities with various browsers.

When using JUnit, we can run a test on different browsers without actually changing the value of browser after one test execution. With the Parameterization option that is available with latest changes of Junit, this can be implemented very easily.

See the example below. With the example, you can execute your automation tests in multiple browsers without actually waiting to finish your tests and then passing the second and third browser value.

Let's test Selenium Master Login Test Case in 3 browsers.

Test Case:





Expected Result

Test Data

Actual Result

Test Result



Navigate to Selenium Master Test App URL:

Log in page displays

 2  Enter username in the Username field.  Username should be entered and displayed in the username field  test      
 3  Enter user password in the Password field.  Password should be entered in the password field  ?????      
 4  Click on the Log in button.  Button should be clicked.        
 5  Verify that "Online users" text should be displayed after logging in.          

Step 1:  Run the Selenium RC Server with the command below. 

                                  C:\Selenium Server>java -jar selenium-server-standalone-2.39.0.jar

verify that the server has been started. 

Step 2: run Eclipse, create a Java Project, add a package and add a JUnit class. The project structure will look like this below. 


Java Source Code ( 


package com.seleniummaster.differentbrowsers;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Collection;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
//run with Parameterized class in JUnit
public class TestSeleniumMasterLogin{
  //define browser  
  private String browser;
  private Selenium selenium;
  //create a constructor with browser
  public TestSeleniumMasterLogin(String browser){
  //define each browser as a parameter
   public static Collection data() {
    return Arrays.asList(new Object[][]{{"*chrome"},{"*googlechrome"},{"*firefox"}});
  public void setUp() throws Exception {
   selenium = new DefaultSelenium("localhost"4444, browser, "");
  public void LoginTest() throws Exception {"/seleniummastertestapp/index.php");
   selenium.type("id=login_login_password""XXXXX")//here password is omitted"id=login_submit");
   for (int second = 0;; second++) {
    if (second >= 60fail("timeout");
      assertEquals("Online users", selenium.getText("css=#sb-onlineusers > h3"));
      if (selenium.isElementPresent("//div[@id='sb-onlineusers']/h3[text()='Online users']")) 
    catch (Exception e) {}
  public void tearDown() throws Exception {

Step 3: Execute the application as Java JUnit test. 


The above JUnit test result indicated that Login Test passed on all 3 browsers.