Data Driven Tests in Serenity with JUnit

HOME

In the previous tutorial, I have explained the Integration of Serenity with JUnit4. In this tutorial, I will explain Data Driven Tests in Serenity with JUnit. Serenity provides features to support Data Driven tests. Refer this tutorial to know how to setup a Serenity project with JUnit4.

There is a parameterized Test Runner to perform data driven tests in JUnit4.

@RunWith(SerenityParameterizedRunner.class)

This runner is very similar to the JUnit Parameterized test runner. Here, @TestData annotation is used to provide test data to the test, and you can use all of the other Serenity annotations like (@Managed, @Steps, @Title and so on). This test runner will also generate proper serenity reports for the executed tests.

Below is an example of data-driven serenity test. In this test, I have created a Test Class (LoginTests) and Step Class (StepLoginPage). I am passing a set of incorrect credentails to the Login page and will verify the error message.

Here is the code for LoginPageDefinitions.

@RunWith(SerenityParameterizedRunner.class)
public class LoginTests {

	@TestData
	public static Collection<Object[]> testData() {
		return Arrays.asList(new Object[][] {
         { "Admin12", "admin123" },
         { "abc", "abc12" }, 
         { "_Admin1", "admin123_" },
	     { " ", " " } 
      });
	}

	private String userName = "";
	private String passWord = "";

	public LoginPageDefinitions(String userName, String passWord) {
		this.userName = userName;
		this.passWord = passWord;
	}

	@Managed
	WebDriver driver;

	@Steps
	StepLoginPage loginPage;

	@Test
	@Title("Login to application should be unsuccessful with error message")
	public void unsucessfulLogin() throws InterruptedException {

		// Given
		loginPage.open();

		// When
		loginPage.inputUserName(userName);
		loginPage.inputPassword(passWord);
		loginPage.clickLogin();

		// Then
		String actualErrorMessage = loginPage.errorMessage();
		Assert.assertEquals("Invalid credentials", actualErrorMessage);
	}

}

@TestData is the annotation for a method which provides parameters to be injected into the test class constructor by Parameterized. testData() method returns an array list of objects as shown above.

The test data is injected into member variables – userName and passWord. These values are represented as instance variables in the test class, and instantiated via the constructor. These member variables are used in the test.

@Managed is annotated as a WebDriver field that is managed by the Test Runner. The Serenity Test Runner will instantiate this WebDriver before the tests start, and close it once they have all finished.

Here is the code for the StepLoginPage.

public class StepLoginPage extends PageObject {

	@FindBy(name = "txtUsername")
	WebElementFacade username;

	@FindBy(name = "txtPassword")
	WebElementFacade password;

	@FindBy(name = "Submit")
	WebElementFacade submitButton;

	@FindBy(id = "spanMessage")
	WebElementFacade errorMessage;

	@FindBy(id = "forgotPasswordLink")
	WebElementFacade linkText;

	@Step("Enter Username")
	public void inputUserName(String userName) {
		username.sendKeys((userName));
	}

	@Step("Enter Password")
	public void inputPassword(String passWord) {
		password.sendKeys((passWord));
	}

	@Step("Click Submit Button")
	public void clickLogin() {
		submitButton.click();
	}

	@Step("Error Message on unsuccessful login")
	public String errorMessage() {
		String actualErrorMessage = errorMessage.getText();
		System.out.println("Actual Error Message :" + actualErrorMessage);
		return actualErrorMessage;
	}

There are two ways to run the tests.

  1. Run the tests as JUnit Tests. Right click on the test and select Run As ->JUnit Test.

2. Run the tests through command line using below command.

mvn clean verify

This will run the tests as well as generate the test execution reports – index.html and serenity-emailable.html.

So, the tests are run and the reports are generated at the shown path.

Index.html

Serenity-Summary.html

We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s