How To Use Serenity and Cucumber for Automation Testing

Fannytambunan
6 min readJan 6, 2021

To make a good automated testing framework, it’s must be readable and maintainable, so that it can be used by product owner, or another tester, or business people without programming skills

Cucumber is a popular tool for automating BDD-style. Cucumber can express acceptance criteria in a natural and human-readable form using format Gherkin (given, when, then), using this Gherkin helps people easy to understand what it want to be tested and result that want to be achieve with the automation testing, but the reporting in Cucumber is not very user-friendly.

Serenity is another BDD testing framework that has better reporting than Cucumber. Serenity BDD also has a defined PageObject class to model the UI interactions.

What you need to prepare :
- Java
- IntellIJ IDE
- Chrome Webdriver

After installed Intellij IDE, click Create New Project, pop up “New Project ” will be shown. Click on maven, choose project sdk, and then click checklist on Create from archetype

Click button Add Archetype

Add archetype of serenity-cucumber, you can find it in this link : https://mvnrepository.com/artifact/net.serenity-bdd/serenity-archetypes. Click on the serenity-cucumber version (in here I choose version 2.0.81)

Input GroupId, ArtifactId, and Version in the pop up Add Archetype in the IntellIj with data GroupId, ArtifactId, and Version of serenity-cucumber archetype

And then click Ok. In the box list of archetype, archetype of serenity-cucumber will be shown. Click on the serenity-cucumber archetype and then click Next

In the next step, input Project Name, choose Location of project to be saved, Project GroupId, ArtifactId, and Version, and then click Next

Make sure that groupId, artifactId, version, archetypeGroupId, archetypeArtifactId, archetypeVersion are already correct, then click Finish

Then the project will be like picture below. Inside the project there will be folder called src. Inside src there will be folder java and resources

Inside folder resources we will find folder named features, where we will save file feature Gherkin. In Gherkin, we will save test scenario of our automation test and each line of scenario in Gherkin feature class will maps to a method in Step Definition Class.

In the newly created project, there is built-in automation testing to test Wikionary page

Inside folder java we will find folder steps, where we will save file class Step Definition. In the Step Definition class there are methods that map each line of the scenario in the feature.

Each of these methods has a @Given, @When, or @Then annotation according to the scenario line being mapped.

For example, as you can see in the feature class in image above, you can see the scenario line : “Given the user is on the Wikionary home page” where this scenario is mapped to the DefinitionSteps class by the givenTheUserIsOnTheWikionaryHomePage method (you can see it in the image below) and on givenTheUserIsOnTheWikionaryHomePage has an annotation @Given according to the scenario that it’s mapped is a given with the value of the given annotation is the line scenario words that it’s mapped.

Inside folder java -> pages, we will find DictionaryPage. In this class we extends PageObject of Serenity to model UI of page that we want to automated (in this example is Wikionary).

In this class we create method/actions on the model ui, for example : method click button in ui page, enter text on input text in ui page, etc. Action on ui that create in DictionaryPage will be used in Step Definition class (in example is EndUserSteps)

Inside folder java -> org.tutorial.serenity.cucumber, we will also find the DefinitionTestSuite class. This class is the test runner class that will run the feature file. As you can see in the image below, in class DefinitionTestSuite there are annotations @RunWith and @CucumberOptions.

To run the test with Serenity, we put CucumberWithSerenity in annotation @RunWith, and inside of annotion @CucumberOptions provide some value like features (to tell cucumber where the feature files can be found), tags(tag of test scenario we want to run), and glue(to tell cucumber where to find the Step Definition classes)

By default, automation testing with serenity-cucumber archetype will be run with Firefox webdriver. We can custom the webdriver setting, for example we want using Chrome webdriver.

First install chrome webdriver. Download chrome webdriver : https://chromedriver.chromium.org/downloads select the same version as your Google Chrome version and download the file zip

Create folder appserver -> webdriver -> put file chromedriver.exe inside C:\. Open your environment variable and then add path to your chromedriver.exe (C:\appserver\webdriver) in System variable -> Path

To check if your chrome webdriver installed successfully, open your cmd and type : chromedriver -version, then it should return the version of chromedriver you just installed

Next create new package named driver under inside folder java-> org.tutorial.serenity.cucumber.

After that create a new java class named CustomDriver, then add implements to an interface class named DriverSource, and then implements method in CustomDriver (in intellij you can, put cursor on public class CustomDriver implements DriverSource -> alt + enter -> then small pop up will shown and choose implement methods choose method newDriver() and method takesScreenshots -> click Ok).

Then, set up DesiredCapabilities to open Chrome browser with version that you want (in this example I use version 80.0) like in the picture below, and don’t forget to set return true in takesScreenshots.

Next in the serenity.properties, add properties like :
- webdriver.driver = chrome
- webdriver.provided.type = mydriver
- webdriver.provided.mydriver = org.tutorial.serenity.cucumber.driver.CustomDriver (path of CustomDriver class)

Let’s try run this automation. You can run this automation by click Maven of the left side, and then double click on clean after that double click on verify, or you can click on clean and then ctrl + click on verify and then click on green play button to run the automation

Then it should open chrome browser and start automated Wiktionary according to test scenario inside LookupADefinition.feature and after run the automation will give result Test Passed

To see report of automation that we just ran, you can open index.html in folder target -> site -> serenity, by right click on index.html hover to Open in Browser and choose Chrome

In the report you can see the statistics of an overall testing result are also generated automatically.

In the report you also can see the testing result of every feature, scenario, and steps. The test data and the screenshot of every step are also listed in the report.

--

--