What’s up Guys! Welcome to automationcalling.com

SerenityBDD_RestAssured

Reading Time: <5 Minutes

In this blog, we are going to take a look at why Serenity BDD and How we can implement Rest Assured with Serenity BDD in terms of JUnit and Cucumber Framework script development.

Why Serenity BDD?

Serenity BDD formerly called (Thucydides), which actually helps to write a better and more effective automation acceptance test. It’s an open source library that provides robust and effective automation feature, world-class report generation and living documentation. The important thing is, you don’t need to write code from scratch to develop the framework.

Key Features of Serenity BDD

  • The wrapper has been built on top of Selenium, Rest Assured, Appium and Cucumber Framework
  • A lot of Built-in functionality
    • Managing State between steps
    • Run Test in Parallel in terms of Junit as well as Cucumber feature file
  • The flexibility of Test Execution on Cloud (BrowserStack, SauceLabs etc.,)
  • JIRA Integration (Automation Results publish to JIRA or Bug Opened for failed cases)
  • Existing Annotation @Screenshots, @Managed, @Steps etc., @Steps Annotation will provide you extra level of abstraction and make your test more readable, reusable and maintanable
  • Very Less or No code implementation required for framework enhancement.

What is Rest Assured?

Rest Assured is an open source and popular library in the market which helps to perform Rest API Test Automation.

I have already written a blog about Rest Assured which can be looked at Rest Assured Core + TestNG + Hamcrest

Rest Assured Integration with SerenityBDD

In this topic, I’m going to explain how we can use existing Rest Assured Util and modify as per SernityBDD Requirement.

Serenity BDD implemented Rest Assured wrapper to provide better/faster execution and generate report documentation. I’m going to explain how a small tweak required in my existing Rest Assured Util that helps to provide quality report.

To Start with existing Framework, Please make sure you added the following artifact dependencies in pom.xml

Dependencies:

  • serenity-sore
  • serenity-junit
  • serenity-rest-assured
  • serenity-cucumber

Plugins:

  • maven-surefire-plugin (This is required but you can mark true next to Skip tag)
  • maven-failsafe-plugin. For this, you can include runner class if it’s cucumber BDD or add junit folders
  • maven-compiler-plugin
  • serenity-maven-plugin

Goals:

It must be “verify” and “aggregate”

for eg., Maven commands: mvn clean verify serenity:aggregate

Small Tweak in Existing Code:

For integrating Rest Assured with Serenity BDD, we can still use the same Rest Assured util “Rest Assured Core + TestNG + Hamcrest” but a small tweak is required to generate the better and quality report.

For eg., endpoint, header, body, the response status code etc., Plase take a look on below screenshot.

result_screenshot1

result_screenshot2

In the above screenshot, there is a button called “REST Query” which actually show up in detail of Rest API invocation.

result_1

Result_2

By default, the detail Rest API configuration will not generate until or unless you modify initialization in the following code.

From: RestAssured.given().log().all();
To: SerenityRest.given().log().all();

As soon as you updated like above code, rest of stuff will be taken care of by Serenity BDD Framework, and it does display report like above snapshots.

simple_implementation_architecture

In this framework, the test automation approach is very simple. You can use JUnit runner class for normal test execution or Cucumber Runner class to run the test in Cucumber JVM way. The above picture depicts it’s Robust, no duplicate code and better acceptance test and maintenance.

Reusable Component:

Rest Assured Core + TestNG + Hamcrest

Feature Steps:

This class is mostly to create and maintain all business functions.

for eg.,

@Step("Searches all states of a country")
public void searchAllStatesofaCountry() {
    try {
        restAssuredCore.initRestAssuredCore("http://services.groupkt.com/", "state/");
        restAssuredCore.setURLEncodingStatus(false).invokeRestCall("GET", "get/IND/UP").getAPIResponseAsPrettyPrint();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Step provide a title in detail view report which actually explaining what is the method about.

Feature Step Definition:

This class is the implementation of the acceptance test in the feature file. This will also help us to maintain better code (reduce no of lines), avoid redundant or duplicate code and better maintenance in code

@Then("^I get (\\d+) records found in the given country code$")
public void i_get_records_found_in_the_given_country_code(int recordCount) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    states_territories_of_a_country.validateRecordCount(36);
}

To play with this sample framework, please feel free to visit  and clone GitHub https://github.com/automationcalling/serenitybddrestassured

Thanks for your time, Please do subscribe for more updates!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s