With JUnit 4 we’re able to Parameterize the tests. Parameterized tests allow developers to run the same tests over and over again using different values.

Now we introduce the way to use the parameterized JUnit tests with Selenium WebDriver.

Here is the sample code, which opens the Google search page, searches three different phrases and checks the results.

package ParametricJUnitBlog;

import java.util.Arrays;
import java.util.Collection;

import org.junit.After;
import org.junit.Assert;
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 org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

@RunWith(Parameterized.class)
public class test2 {

    WebDriver driver;
    private String searchTxt;

    public test2(String searchText){
this.searchTxt = searchText;
    }

    @Parameters
public static Collection<Object[] > data(){
    Object[][] data = new Object[][] { {"1"}, {"JU"}, {"JUnit Parallel"}};
    return Arrays.asList(data);
    }

    @Before
    public void setUp(){
        driver = new FirefoxDriver();
    }

    @After
    public void tearDown(){
        driver.quit();
    }

    @Test
    public void testFindElements() throwsException{
        driver.get("http://www.google.com");
        Thread.sleep(500);
        WebElement searchArea = driver.findElement(By.name("q"));
        searchArea.sendKeys(searchTxt);
        Thread.sleep(500);
        WebElement searchButton = driver.findElement(By.name("btnG"));
        searchButton.click();
        Thread.sleep(500);
        String pageSource = driver.getPageSource();
        Assert.assertTrue(pageSource.contains(searchTxt));
        Thread.sleep(2000);
    }
}

The first thing we need to do is to annotate the test class with @RunWith(Parameterized.class)

The second thing is to create a public static method annotated with @Parameters that returns a Collection of Objects (as Array) as test data set. In our case it returns three different text objects.

Now we need to create a public constructor that takes in what is equivalent to one “row” of test data.
In our case it gives a value to the searchTxt variable.
In the test we simple use the searchTxt variable.

Because the test knows that it’s parameterized, it automatically runs the test, in our case, 3 times in a row, one after the other.

ParametricJUnittest

Here is another code sample with the same logic, just in this case we parameterized the browser drivers.
This code will open each browsers, run the test in the first browser and then in the second browser.

package ParametricJUnitBlog;

import java.util.Arrays;
import java.util.Collection;

import org.junit.After;
import org.junit.Assert;
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 org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

@RunWith(Parameterized.class)
public class test {

    WebDriver driver;
    private WebDriver browser;

    public test(WebDriver browser){
this.browser = browser;
    }

    @Parameters
public static Collection<Object[] > data(){
    Object[][] data = newObject[][] { { newChromeDriver() }, { newFirefoxDriver() }};
    return Arrays.asList(data);
    }

    @Before
    public void setUp(){
        driver = browser;
    }

    @After
    public void tearDown(){
        driver.quit();
    }

    @Test
    public void testFindElements() throwsException{
        driver.get("http://www.google.com");
        Thread.sleep(500);
        WebElement searchArea = driver.findElement(By.name("q"));
        searchArea.sendKeys("JUnit");
        Thread.sleep(500);
        WebElement searchButton = driver.findElement(By.name("btnG"));
        searchButton.click();
        Thread.sleep(500);
        String pageSource = driver.getPageSource();
        Assert.assertTrue(pageSource.contains("JUnit"));
        Thread.sleep(1000);
    }
}

 

Posted By István Lackó

    6 Responses to “Parameterized JUnit tests with Selenium WebDriver”

  1. olivier n says:

    Thanks for the post.

    However, how would you handle multiple tests in this class? Let’s stay you have another test called testFindElements2(). If you run your tests have more than 1 test per class, then all but the first test will fail. Thanks!

  2. Sathish Kumar says:

    Hi Istvan
    Thanks for your post,
    Parameterizing Browser is not working, How can we give a test data dynamically while the test is executing.

    Please provide a solution.

    Thanks.

  3. Gerrit Leder says:

    Hello thanks for the outstanding example for JUnit with selenium use. Unfortunately in the source code examples some whitespace is missing.

    Regards
    Gerrit

Leave a Reply




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>