DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

Related

  • Exploring Mobile Device Lab: Pros and Cons
  • End-To-End Testing Unveiled: Navigating Challenges With a Personal Experience
  • Automated Testing: The Missing Piece of Your CI/CD Puzzle
  • 10 Things to Remember Before You Say Yes to Automation Testing

Trending

  • Is Agile Right for Every Project? When To Use It and When To Avoid It
  • Exploring Reactive and Proactive Observability in the Modern Monitoring Landscape
  • When Agile Teams Fake Progress: The Hidden Danger of Status Over Substance
  • How to Install and Set Up Jenkins With Docker Compose
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. The Ultimate Cheat Sheet On BDD Test Automation With Serenity

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.

By 
Yevhen Kryvun user avatar
Yevhen Kryvun
·
Sep. 07, 16 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
11.2K Views

Join the DZone community and get the full member experience.

Join For Free

This 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.

  1. Create project structure (contact Serenity for project source code)

  2. 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.

  3. 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 {
    }


  4. 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:

  5. 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.

  6. 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?

Test data Test automation

Published at DZone with permission of Yevhen Kryvun. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Exploring Mobile Device Lab: Pros and Cons
  • End-To-End Testing Unveiled: Navigating Challenges With a Personal Experience
  • Automated Testing: The Missing Piece of Your CI/CD Puzzle
  • 10 Things to Remember Before You Say Yes to Automation Testing

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: