Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

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:

 

 

Step

Action

Expected Result

Test Data

Actual Result

Test Result

Note

1

Navigate to Selenium Master Test App URL: http://www.seleniummaster.com/seleniummastertestapp/index.php

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 (TestSeleniumMasterLogin.java): 

 

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
@RunWith(Parameterized.class)
public class TestSeleniumMasterLogin{
  //define browser  
  private String browser;
  private Selenium selenium;
  //create a constructor with browser
  public TestSeleniumMasterLogin(String browser){
   this.browser=browser;
  }
  //define each browser as a parameter
  @Parameterized.Parameters
   public static Collection data() {
    return Arrays.asList(new Object[][]{{"*chrome"},{"*googlechrome"},{"*firefox"}});
   }
   @Before
  public void setUp() throws Exception {
   selenium = new DefaultSelenium("localhost"4444, browser, "http://www.seleniummaster.com");
   selenium.start();
   selenium.windowMaximize();
  }
 
  @Test
  public void LoginTest() throws Exception {
   selenium.open("/seleniummastertestapp/index.php");
   selenium.type("id=login_login_username""test");
   selenium.type("id=login_login_password""XXXXX")//here password is omitted
   selenium.click("id=login_submit");
   for (int second = 0;; second++) {
    if (second >= 60fail("timeout");
    try 
      assertEquals("Online users", selenium.getText("css=#sb-onlineusers > h3"));
      if (selenium.isElementPresent("//div[@id='sb-onlineusers']/h3[text()='Online users']")) 
        break
    catch (Exception e) {}
    Thread.sleep(1000);
   }
     
  }
 
  @After
  public void tearDown() throws Exception {
   selenium.stop();
  }
  
}

Step 3: Execute the application as Java JUnit test. 

 

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