Serenity Test Automation: A Beginner’s Guide

Pratik Barjatiya
4 min readMay 29, 2018

--

Serenity BDD — A great tool for applying Behavior Driven Development (BDD). This is a solution for automated acceptance testing that generates well-illustrated testing reports.

Serenity BDD is a powerful tool for implementing Behavior-Driven Development (BDD). It supports automated acceptance testing and produces detailed, visually appealing reports, making it easier for teams to understand testing outcomes.

What is Behavior-Driven Development (BDD)?

Behavior-Driven Development (BDD) combines techniques from Test-Driven Development (TDD), domain-driven design, and object-oriented analysis to create a collaborative approach to software development.

In simpler terms, BDD extends TDD by using human-readable language to define behaviors and expected outcomes, making it particularly effective in tackling complex business requirements.

Why Choose Serenity?

Serenity simplifies writing structured, maintainable automated tests. It integrates seamlessly with popular BDD libraries like Cucumber and JBehave, as well as traditional testing tools like JUnit and TestNG.

It’s especially useful in Agile environments, enabling test developers to create user stories that guide development and validation through acceptance criteria.

Serenity Components

1. Requirements

Serenity uses plain English to define Capabilities (Epics), Features, and Stories. BDD annotations like GIVEN, WHEN, and THEN describe behavior and expected outcomes.

  • Plain narrative English is used to define the Requirements components
  • Annotations like GIVEN, WHEN and THEN are used to define this

Example:

@Authorization
Feature: Authorization- Application Authentication Module
Scenario: Authentication in Application
Given I launch application
When I execute sign in with credentials
Then I should observe correct redirection over login success/fail case
And I certify login behavior
Requirements

2. Step Definitions

Step definitions map BDD annotations to actual code logic. They initialize screen objects and implement actions for test scenarios.

  • This will have the code similar to traditional way of automation scripts.
  • Here we define the functions for each annotations mentioned in the above requirements
  • Initialize various screen objects and call native screen functions

Example

@Given("^I launch applicaton$")
public void iOpenApplication() throws Exception {
// Write code here that turns the phrase above into concrete actions
driver.get("application_URL");
}
@When("^I execute sign in with credentials$")
public void iSignIn() throws Exception {
// Write code here that turns the phrase above into concrete actions
PageFactory.initElements(driver, AutomationHomePage.class);
PageFactory.initElements(driver, LoginPage.class);
SignInAction.Execute(driver,datamap);
Assert.assertEquals(true,AutomationHomePage.profile_pic.isDisplayed());
}
@Then("^I should observe correct redirection over login success/fail case")
public void iClickOnMyAccount() throws Exception{
PageFactory.initElements(driver, AutomationHomePage.class);
AutomationHomePage.profile_pic.click();
AutomationHomePage.my_account.click();
PageFactory.initElements(driver, AccountPage.class);
Assert.assertEquals(true,AccountPage.billing_details.isDisplayed());
}
@And("^I certify login behavior")
public void certifyLogin() throws Exception{
PageFactory.initElements(driver,AccountPage.class);
Assert.assertEquals(true,AccountPage.sign_in_acccount_details.getText().startsWith("Signed in"));
ExtentCucumberFormatter.setTestRunnerOutput("STEP: Login Certify - Success");
}
Step Definations

3. Step Actions

Step Actions define reusable methods and manage page object locators. This modularity allows you to maintain flexibility in your framework design.

  • This is flexible enough to design your own framework. This post uses Page Objects. One can use Keywords as well and define keywords in Step Actions
  • For Page Objects, Step Actions will have Page Object Locators Classes and Page Object Actions methods

Example: Page Object Locators

public class AutomationHomePage extends BaseClass{    public AutomationHomePage(WebDriver driver){
super(driver);
}
@FindBy(xpath = "//a[contains(.,'Sign In')]")
public static WebElement sign_in;
@FindBy(how=How.LINK_TEXT, using="Contact us")
public static WebElement contact_us;
@FindBy(id = "user-pic")
public static WebElement profile_pic;
@FindBy(xpath = "//*[@id='autoSearchBox']//li/a[contains(.,'My Account')]")
public static WebElement my_account;
@FindBy(xpath = "//*[@id='autoSearchBox']//li/a[contains(.,'Sign Out')]")
public static WebElement sign_out;
@FindBy(xpath = "//button[contains(.,'SEARCH')]")
public static WebElement search_button;
}

Page Object Actions

public class SignInAction {
public static void Execute(WebDriver driver,List<HashMap<String,String>> map) throws Exception{
AutomationHomePage.sign_in.click();
setTestRunnerOutput("Click action is performed on My Account link" );
LoginPage.email.sendKeys(map.get(0).get("username"));
setTestRunnerOutput("username is entered in UserName text box" );
LoginPage.password.sendKeys(map.get(0).get("password"));
setTestRunnerOutput("password is entered in Password text box" );
LoginPage.sign_in_button.click();
setTestRunnerOutput("SignIn Action is successfully perfomred");
}
}
Step Actions

Framework Structure

A sample Serenity framework structure typically includes:

  • Requirements: Define behavior.
  • Step Definitions: Map behaviors to logic.
  • Step Actions: Implement actions.
  • Reports: Generate Serenity’s comprehensive reports.

Sample structure looks like

Framework Structure
Framework Structure

Test Reports

Serenity provides detailed reports that enhance visibility into testing outcomes, making it easier for stakeholders to understand progress and issues.

Serenity Test Report
Serenity Test Reports

💡 If you found this valuable, please upvote, like, or subscribe to stay updated on the latest insights. Your engagement helps us grow and share more knowledge! Let’s connect in the comments 🚀.

--

--

Pratik Barjatiya
Pratik Barjatiya

Written by Pratik Barjatiya

Data Engineer | Big Data Analytics | Data Science Practitioner | MLE | Disciplined Investor | Fitness & Traveller

Responses (3)