How I implemented Test Automation using Serenity

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.

What is Behavior Driven Development?

  1. As per wiki definition, Behavior-driven development combines the general techniques and principles of TDD with ideas from domain-driven design and object-oriented analysis and design to provide software development and management teams with shared tools and a shared process to collaborate on software development
  2. In other words, BDD is extension of TDD which facilitated the use of a simple natural language constructs that can express the behavior and the expected outcomes.
  3. It is considered an effective practice when the “problem space” of the business specifications to solve is complex

Why Serenity?

  1. Serenity aims to make it easy and quick to write well-structured, maintainable automated acceptance criteria, using best BDD libraries like Jbehave/Cucumber or conventional testing library likes Junit, TestNG.
  2. Serenity also facilitate adoption to most widely used Agile Methodologies. Example, we need to Create User Stories for Test Developers, which developers need to develop them until acceptance tests will be successfully passed. This can be explained with the components of Serenity BDD

Serenity Components

Requirements — This is the agile way of defining the requirements, contains Capabilities (Epics), Features and Stories.

  • 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

Step Definition — Here we use define the requirements.

  • 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

Step Actions — Here we define the actions as called in Step Definitions .

  • 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

Sample structure looks like

Framework Structure

Test Reports

Serenity Test Reports

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Head First Java (Chapter 01) Breaking the surface

golang concurrent

Top 7 characteristics of a modern data architecture

How to syntax highligh .mvir files in Vim and VSCode (Facebook Libra and Move)

How to spin up a Linux machine and SSH into it?

FAQ about software testing

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pratik Barjatiya

Pratik Barjatiya

More from Medium

TestNG testing framework

TestNG

How to Perform Cross Browser Testing From Various Geo Locations

Three Different Ways on Cucumber Runner

XPath Technology