Data Driven Tests using CSV file in Serenity


In the previous tutorial, I have explained the Data Driven Tests in Serenity where test data are defined in Tests. In this tutorial, I will explain the Data Driven tests in Serenity where we will get the test data from CSV file.

To start with this tutorial, refer this tutorial also which explains how to setup a project with Serenity and JUnit4.

Serenity lets us perform data-driven testing using test data in a CSV file. We store our test data in a CSV file (by default with columns separated by commas), with the first column acting as a header.

We need to create a test class containing properties that match the columns in the test data, as you did for the data-driven test in the previous example. The test class will typically contain one or more tests that use these properties as parameters to the test step or Page Object methods.

Here, we need to keep in mind that as the tests are parameterized , we need to use the Parameterized test runner to perform data-driven tests.


This is imported from below package


@UseTestDataFrom annotation is used to indicate where to find the CSV file (this can either be a file on the classpath or a relative or absolute file path – putting the data set on the class path (e.g. in src/test/resources) makes the tests more portable).

@UseTestDataFrom(value = "testdata/credentials.csv")

Below is the example where Test Data is retrived from .csv file and passed to the tests.


public class StepLoginPage extends PageObject {

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

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

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

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

	@Step("Enter Username")
	public void inputUserName(String userName) {

	@Step("Enter Password")
	public void inputPassword(String passWord) {

	@Step("Click Submit Button")
	public void clickLogin() {;

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


Now, create the Test Class which contains the tests, SerenityRunner as well as the path to the CSV test data file.

As you can see, I have mentioned 3 variables in CSV file – USERNAME, PASSWORD and ERRORMESSAGE. I have declared 3 private variable – username , password and errorMessage that match the columns in the test data file.

The test runner creates a new instance of this class for each row of data in the CSV file, assigning the properties with corresponding values in the test data

@UseTestDataFrom(value = "testdata/credentials.csv")
public class LoginTests {

	private String username;
	private String password;
	private String errorMessage;

	public void setUsername(String username) {
		this.username = username;

	public void setPassword(String password) {
		this.password = password;

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;

	WebDriver driver;

	StepLoginPage loginPage;

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

		// Given;

		// When

		// Then
		String actualErrorMessage = loginPage.errorMessage();
		Assert.assertEquals(errorMessage, actualErrorMessage);


This test can be executed by JUnit as well as from command line

JUnit – Right click on the Test, select Run As and then select JUnit Test.

The Test status can be seen as shown below

To run the tests using command line, use the below command

mvn clean verify

This will execute the tests and will generate the Test Execution Report as shown below.

The reports are generated as shown in below image.


Serenity Summary Report

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: Logo

You are commenting using your 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