Monday, July 18, 2011

Running Selenium Tests on different browsers using Junit

Many people has asked me on how to run a test on different browsers using Junit without actually changing the value of browser after one test execution. For which I had always kept answering that it may not be possible with Junit. But recently while trying to find a solution for it I came across with the Parameterization option that is available with latest changes of Junit. This was not possible in earlier versions of Junit but now Junit has come up with lot of added options in the latest version of Junit 4.8. Following is a way by which you can execute your automation tests in multiple browsers without actually waiting to finish your tests and then passing the second browser value. See the example below:
import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import java.util.Arrays;
import java.util.List;

public class TestGoogleBase extends SeleneseTestBase {
  private String browser;
  public TestGoogleBase(String browser){
   public static List data() {
    return Arrays.asList(new Object[][]{{"*chrome"},{"*googlechrome"}});

  public void setUp() throws Exception {
   selenium = new DefaultSelenium("localhost", 4444, browser, "");

  public void testUntitled() throws Exception {"/");
   selenium.type("id=lst-ib", "testing");"//input[@value='Google Search']");
   for (int second = 0;; second++) {
    if (second >= 60) fail("timeout");
    try { if (selenium.isElementPresent("link=Software testing - Wikipedia, the free encyclopedia")) break; } catch (Exception e) {}
   }"link=Software testing - Wikipedia, the free encyclopedia");
   for (int second = 0;; second++) {
    if (second >= 60) fail("timeout");
    try { if (selenium.isTextPresent("Software testing")) break; } catch (Exception e) {}

  public void tearDown() throws Exception {


In the above class I am using the @RunWith(Parametrized.class) option that will tell Junit that this class is run as a parametrized class. A parameter value providing method is added to the class by denoting @Parameter annotation which returns a List< Object[]> array. This array consists of the browser names on which the test needs to be executed, like “*chrome”(Firefox) and “*googlechrome” in this case.
The constructor of the said class is modified to accept a “browser” string that is set when the said parameter function return the browser value.
This “browser” value is then used by the “setup” and inturn by the “Test” method while execution.


ankita said...

Hi Varun,
Great article
I m beginner for webdriver,i did as u said but my quen is i want to open all browser at same time not one by one how can i do that plz help me?

Looking for your reply

Thank You

Varun Menon said...

I haven't tried it in Junit but you can try the fork mode of Junit while using ant.
If you are using testng you can use parallel option of it.

redmonkey said...

I originally went down that route, but ended up hating it.

The main reason is that I couldn't specifically debug one test case for one browser... well I could but, every time i wanted to say test filtering of my table in firefox, i have to put up with all the other browsers jumping in my face. End when I do hit the breakpoint, i constantly have figure which browser i am in.

ALso the JUnit view in eclipse doesnt give me a clear enough view of which browser the test failed.

This is how I do it :

Varun Menon said...

Went through the above link that you provided. I personally feel the design pattern mentioned in the said link is not a good approach. As mentioned in the example user end up writing multiple test methods with the same logic but for different browser.
The example only mention one test what about 100 - 1000 test methods.
The approach for writing code or automation should be to write less and do more.

redmonkey said...

but dont you think it is very annoying when you want to debug a test method for only browser?

Varun Menon said...

Whenever you want to debug the test for a specific browser use that browser driver object to execute your tests and debug. Simple :)

redmonkey said...

what does that mean? Modifiying the data() method? Isnt there a danger that you could checkin that change?

Lalit Patil said...

Hey Varun,

Can we use Testng in selenium RC to run script on multiple browser's? If not then what is the feasible solution for that?

Varun Menon said...

Yes you can use TestNG. In-fact that is the main unit test framework you can use for parallel test execution with Selenium.
Take a look at following url:

Diego Farias said...

Thanks for the tip. Helped me a lot.

sudheerah said...

Is it possible do subject testing with Selenium2?

Post a Comment