Quantcast
Viewing all articles
Browse latest Browse all 22

Parameterized JUnit tests with Selenium WebDriver

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.

Image may be NSFW.
Clik here to view.
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);
    }
}

 


Viewing all articles
Browse latest Browse all 22

Trending Articles