Serenity Test Automation: A Beginner’s Guide
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.
What is Behavior Driven Development?
- 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
- 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.
- It is considered an effective practice when the “problem space” of the business specifications to solve is complex
Why Serenity?
- 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.
- 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
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 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");
}
}
Framework Structure
Sample structure looks like
Test Reports