We can create data-driven Selenium WebDriver tests using the JUnit parameterization feature. This can be done by using the JUnit parameterized class runner.
In this blog, we will create a simple JUnit test case to test Facebook registration. We will specify the test data within our JUnit test case class. We will use various JUnit annotations to create a data-driven test.

Here is a sample code how to create a data-driven test using JUnit.
Do the following steps:

1. Create a new JUnit test class that uses a parameterized runner using @RunWith(value = Parameterized.class).

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;

import org.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.Parameterized;

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

@RunWith(value = Parameterized.class)
public class dataDrivenJUnitTest {
private static WebDriver driver;
}

2. Declare instance variables for the parameterized values in the dataDrivenJUnitTest class.

private String firstName;
private String lastName;
private String email;
private String password;

3. Define a method that will return the collection of parameters to the dataDrivenJUnitTest class by using the @Parameters annotation.

@Parameters
public static Collection testData() {
return Arrays.asList(new Object[][] {
{ "TesterFname1", "TesterLName1", "email1@email.com", "password" },
{ "TesterFname2", "TesterLName2", "email2@email.com", "password" },
{ "TesterFname3", "TesterLName3", "email3@email.com", "password" },
{ "TesterFname4", "TesterLName4", "email4@email.com", "password" } });
}

4. Add a constructor to the dataDrivenJUnitTest class, which will be used by the test runner to pass the parameters to the dataDrivenJUnitTest class instance.

public dataDrivenJUnitTest(String firstName, String lastName, String email, String password) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.password = password;
}

5. Finally, add the test case method testFacebookRegistration() that uses parameterized variables. Also, add the setup() and teardown() methods to the dataDrivenJUnitTest class.
Here is the full code:

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.junit.*;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;

@RunWith(value = Parameterized.class)
public class dataDrivenJUnitTest {

private static WebDriver driver;

@Before
public void setUp(){
driver = new FirefoxDriver();
driver.get("https://www.facebook.com/");
}

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

private String firstName;
private String lastName;
private String email;
private String password;

@Parameters
public static Collection testData() {
return Arrays.asList(new Object[][] {
{ "TesterFname1", "TesterLName1", "email1@email.com", "password" },
{ "TesterFname2", "TesterLName2", "email2@email.com", "password" },
{ "TesterFname3", "TesterLName3", "email3@email.com", "password" },
{ "TesterFname4", "TesterLName4", "email4@email.com", "password" } });
}

public dataDrivenJUnitTest(String firstName, String lastName, String email, String password) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.password = password;
}

@Test
public void testFacebookRegistration() throws Exception {
WebDriverWait wait = new WebDriverWait(driver, 20);
try {
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='u_0_1']")));
} catch (TimeoutException ex) {
Assert.fail("Not loaded");
}

// set first name
WebElement fname = driver.findElement(By.xpath("//*[@id='u_0_1']"));
fname.click();
fname.clear();
fname.sendKeys(firstName);

// set last name
WebElement lname = driver.findElement(By.xpath("//*[@id='u_0_3']"));
lname.click();
lname.clear();
lname.sendKeys(lastName);

// set email
WebElement emailAddress = driver.findElement(By.xpath("//*[@id='u_0_5']"));
emailAddress.click();
emailAddress.clear();
emailAddress.sendKeys(email);

// set email
WebElement emailAddress2 = driver.findElement(By.xpath("//*[@id='u_0_8']"));
emailAddress2.click();
emailAddress2.clear();
emailAddress2.sendKeys(email);

// set password
WebElement pass = driver.findElement(By.xpath("//*[@id='u_0_a']"));
pass.click();
pass.clear();
pass.sendKeys(password);

// click to registrate
WebElement reg = driver.findElement(By.xpath("//*[@id='u_0_i']"));
reg.click();

try{
// check the error message
WebDriverWait wait2 = new WebDriverWait(driver, 10);
wait2.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#u_0_f i.sx_2bac47")));
}
catch (NoSuchElementException e) {
Assert.fail("The element is not appeared.");
}
}
}

When the test is executed, for each row in the test data collection, the test runner will passing the test data as parameters to the dataDrivenJUnitTest class constructor. It will then execute all the tests in the dataDrivenJUnitTest class.
Instance variables are declared to store the test data passed by the test runner:
private String firstName;
private String lastName;
private String email;
private String password;

In the test case class constructor, these variables are assigned with values at runtime by the test runner from the test data collection.
In the testFacebookRegistration() method, we passed these variables to the Selenium WebDriver.

JUnit will display results for each set of test data as shown in the following screenshot:
pic2

Posted By István Lackó

    4 Responses to “Creating a data-driven test with JUnit”

  1. Tomek says:

    why anyone is still using default JUnit data driven implementation which is so damn unusable? I can’t believe you haven’t yet discovered tools like JUnitParams (or switched to TestNG or Spock…)

  2. thanks, i need flex testing tools name,please provide

    • Hi,

      There was a tool which we tried out in the past called FlexMonkey, you can check it out, maybe it still supported.
      And there is another good tool called Ranorex, but it is not free to use.

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>