The Ultimate Cheat Sheet On BDD Test Automation With Serenity
"I swear by my pretty floral bonnet, I will automate you." An introduction to the open source BDD framework Serenity.
Join the DZone community and get the full member experience.
Join For FreeThis is a true Serenity BDD tutorial. It focuses on open source library Serenity, which helps you to write tests, produce reports and map your automated tests to business requirements.
Let’s run through the basics to clearly understand how it works and how to run and debug your tests.
What is BDD?
BDD stands for behavior-driven development, in which an application is specified and designed by describing how it behaves. BDD offers the ability to enlarge the pool of input and feedback to include business stakeholders and end users who may not even have software development knowledge. BDD is one of the best options for continuous integration, and the Serenity test automation library is the best option for "Java-like" implementation.
So, let's assume that we have a story template:
As a <person (or role)>
I want <have some feature>
so that <check feature value>
In BDD, a story or feature template looks like a scenario:
Given <a scene of the scenario, it is typically something that happened in the past>
When <an event, or an action; can be a person interacting with the system, or it can be an event
triggered by another system>
Then <an expected outcome, or result>
This format is called Gherkin, and helps us write acceptance criteria in a natural, human-readable form.
BDD example (feature file):
Feature: Google search
In order to find items
As a generic user
I want to be able to search with Google
Scenario: Google search
Given I want to search in Google
When I search for 'Serenity BDD'
Then I should see link to 'Serenity BDD - Automated Acceptance Testing with Style'
You can use different examples in one scenario by referring to example tables. For instance, the following scenario illustrates how you can verify Google search for different words:
Scenario Outline: Google search multiple
Given I want to search in Google
When I search for '<search_request>'
Then I should see link to '<search_result>'
Examples:
| search_request | search_result |
| Serenity BDD | Serenity BDD - Automated Acceptance Testing with Style |
| Cucumber | Cucumber |
Configuring a Project
Let's create project from scratch by using Selenium framework as the core of the Serenity test automation library.
Create project structure (contact Serenity for project source code)
Add your feature files
Create under test/resources feature file:
example.feature Feature: Google search In order to find items As a generic user I want to be able to search with Google Scenario: Google search Given I want to search in Google When I search for 'Serenity BDD' Then I should see link to 'Serenity BDD - Automated Acceptance Testing with Style' Scenario Outline: Google search multiple Given I want to search in Google When I search for '<search_request>' Then I should see link to '<search_result>' Examples: | search_request | search_result | | Serenity BDD | Serenity BDD - Automated Acceptance Testing with Style | | Cucumber | Cucumber |
Organize the feature files in subdirectories according to the requirements. In the following directory structure we have feature definitions for several higher-level features: search and translate.
Create BDD testing runner class
The test runner class runs feature files. If the feature files are not in the same package as the test runner class, you also need to use the @CucumberOptions annotation to provide the root directory where the feature files and step definitions can be found.
Info: File should be in package that we specified in failsafe maven plugin (com.test.serenity.runTests)
GoogleSearchTests.java @RunWith(CucumberWithSerenity.class) @CucumberOptions(features = { "src/test/resources/features" }, glue = { "com.test.serenity.stepDefinitions" }) public class GoogleSearchTests { }
Step definitions of our testing tool
Each line of the Gherkin scenario maps to a method in a Java class, known as a Step Definition. Cucumber annotations like @Given, @When, and @Then match lines in the scenario to Java methods. You should define simple regular expressions to indicate parameters that will be passed into the methods (for example: "I search for 'something'" will be "I search for '(.*)'"):
GoogleSearchStepDefinitions.java public class GoogleSearchStepDefinitions { @Steps GoogleSearchSteps googleSearchSteps; @Given("I want to search in Google") public void iWantToSearchInGoogle() throws Throwable { googleSearchSteps.openGoogleSearchPage(); } @When("I search for '(.*)'") public void iSearchFor(String searchRequest) throws Throwable { googleSearchSteps.searchFor(searchRequest); } @Then("I should see link to '(.*)'") public void iShouldSeeLinkTo(String searchResult) throws Throwable { googleSearchSteps.verifyResult(searchResult); } }
Info: We use @Steps to add a layer of abstraction between the "what" and the "how" of our acceptance tests. Step definitions describe "what" the acceptance test is doing, in clear business domain language. So we say "I search for 'Serenity BDD'", not "user enters 'Serenity BDD' into google search field and clicks on the search button".
File structure for step definitions, steps, and running test classes looks like this:
Steps
Step Library is a Java class, with methods annotated with the @Step annotation:
GoogleSearchSteps.java public class GoogleSearchSteps { GoogleSearchPage searchPage; GoogleResultsPage resultsPage; @Step public void openGoogleSearchPage() { searchPage.open(); } @Step public void searchFor(String searchRequest) { resultsPage = searchPage.searchFor(searchRequest); } @Step public void verifyResult(String searchResult) { List<String> results = resultsPage.getResultsList(); Assert.assertTrue(results.contains(searchResult)); } }
Step definitions should not be complex, and should focus on working at a single level of abstraction. Step definitions typically operate with web services, databases, or WebDriver page objects. For example, in automated web tests like this one, the step library methods do not call WebDriver directly, but they rather interact with Page Objects.
Page Objects
Page Objects hide the WebDriver implementation details about how elements on a page are accessed and manipulated behind simple methods. Like steps, Page Objects are reusable components that make the tests easier to understand and to maintain.
After extending your page with Page Object, WebDriver instance will be injected into the current page. All you need is to implement WebDriver code that will interact with the page. For example, here is the page object for the Google search page:
@DefaultUrl("https://google.com") public class GoogleSearchPage extends PageObject { @FindBy(id = "lst-ib") private WebElement searchInputField; @WhenPageOpens public void waitUntilGoogleLogoAppears() { $("#hplogo").waitUntilVisible(); } public GoogleResultsPage searchFor(String searchRequest) { element(searchInputField).clear(); element(searchInputField).typeAndEnter(searchRequest); return new GoogleResultsPage(getDriver()); } }
Info: WebDriver method open() opens page according to @DefaultUrl annotation. If @DefaultUrl is missing — WebDriver will open webdriver.base.url from property file.
Note: You can use the $ method to access elements directly using CSS, id, or XPath expressions. Or you may use a member variable annotated with the @FindBy.
Running and Debugging
For running your test, use this Maven command: mvn clean verify
During execution you can monitor logs:
For debugging, you should have your debug profile in a pom.xml file.
Reports
As for me, reports is the best Serenity feature. You can compile living documentation, combine features and stories in a step-by-step narrative format that includes test data and their execution screenshots.
Main report page shows a wealth of information about tests: count of tests, duration of execution, failed and pending tests, etc.
For example, this page illustrates the test results for our first scenario:
Also, example tables can be a great way to summarize business logic:
An example of report results with errors:
Keep in mind that reports will show you what work has been done, and what work is in progress. Any scenario that contain steps without matching step definition methods will appear in the reports as Pending.
Serenity will use a feature package structure to group and combine test results for each feature. This way, Serenity can report how well each requirement has been tested, and what requirements have not been tested:
You can take a look at this project at Github.
Have thoughts on this post, then why not comment on it?
Published at DZone with permission of Yevhen Kryvun. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments