[New post] Integration of Serenity with Cucumber6 and JUnit5
vibssingh posted: " HOME In the previous tutorial, I have explained the Serenity BDD with Cucumber for Web Application using Junit4. In this tutorial, I will explain the same Test Framework using Serenity, Cucumber6 and JUnit5. This tutorial gives a clear picture for the"
In the previous tutorial, I have explained theSerenity BDD with Cucumber for Web Application using Junit4. In this tutorial, I will explain the same Test Framework using Serenity, Cucumber6 and JUnit5. This tutorial gives a clear picture for the initial setup of a BDD Framework .
What is JUnit5?
JUnit 5 is composed of several different modules from three different sub-projects.
The JUnit Platform serves as a foundation for launching testing frameworks on the JVM. It also defines the TestEngine API for developing a testing framework that runs on the platform.
JUnit Jupiter is the combination of the new programming model and extension model for writing tests and extensions in JUnit 5. The Jupiter sub-project provides a TestEngine for running Jupiter based tests on the platform.
JUnit Vintage provides a TestEngine for running JUnit 3 and JUnit 4 based tests on the platform. It requires JUnit 4.12 or later to be present on the class/module path.
JUnit5 is not completely integrated with Serenity with Cucumber. So, it is advisable to use jupiter-vintage-engine for the Cucumber TestRunner classes.
This framework consists of:-
Serenity - 2.6.0
Serenity Cucumber6 - 2.6.0
Serenity Junit5 - 1.6.0
JUnit Jupiter - 5.8.0
JUnit Vintage - 5.8.0
Java 11
Maven – 3.8.1
Selenium – 3.141.59
Maven Compiler Plugin - 3.8.1
Maven Surefire Plugin - 3.0.0-M5
Maven FailSafe Plugin - 3.0.0-M5
Implementation Steps
Download and Install Java on system
Download and setup Eclipse IDE on system
Setup Maven and create a new Maven Project
Update Properties section in Maven pom.xml
Add repositories and pluginRepository to Maven pom.xml
Add Serenity, Serenity Cucumber and JUnit5 dependencies to POM.xml
Update Build Section of pom.xml
Create a feature file under src/test/resources
Create the Step Definition class or Glue Code
Create a Serenity-Cucumber Runner class
Create serenity.conf file under src/test/resources
Create serenity.properties file in the root of the project
Run the tests from Command Line
Serenity Report Generation - Index.html and Serenity-Summary.html
The Eclipse IDE (integrated development environment) provides strong support for Java developer which is needed to write Java code. Click here to know How to install Eclipse.
Step 3 – Setup Maven and create a new Maven Project
Step 8 - Create a feature file under src/test/resources
The purpose of the Feature keyword is to provide a high-level description of a software feature, and to group related scenarios. To know more about Feature file, please refer this tutorial.
Feature: Login to HRM @ValidCredentials Scenario: Login with valid credentials Given User is on Home page When User enters username as "Admin" And User enters password as "admin123" Then User should be able to login successfully @InValidCredentials Scenario Outline: Login with invalid credentials Given User is on Home page When User enters username as '<username>' And User enters password as '<password>' Then User should be able to see error message '<errorMessage>' Examples: |username |password |errorMessage | |admin |admin |Invalid credentials | | |admin123 |Username cannot be empty | |Admin | |Password cannot be empty | | | |Username can be empty |
Step 9 - Create the Step Definition class or Glue Code
A Step Definition is a Java method with an expression that links it to one or more Gherkin steps. When Cucumber executes a Gherkin step in a scenario, it will look for a matching step definition to execute. You can have all of your step definitions in one file, or in multiple files.
LoginPageDefinitions.java
public class LoginPageDefinitions { @Steps StepLoginPage loginPage; @Steps StepDashboardPage dashPage; @Steps StepForgetPasswordPage forgetpasswordPage; @Given("User is on Home page") public void openApplication() { loginPage.open(); } @When("User enters username as {string}") public void enterUsername(String userName) { loginPage.inputUserName(userName); } @When("User enters password as {string}") public void enterPassword(String passWord) { loginPage.inputPassword(passWord); loginPage.clickLogin(); } @Then("User should be able to login successfully") public void clickOnLoginButton() { dashPage.loginVerify(); } @Then("User should be able to see error message {string}") public void unsucessfulLogin(String expectedErrorMessage) { String actualErrorMessage = loginPage.errorMessage(); assertEquals(expectedErrorMessage, actualErrorMessage); } }
Assertions in JUnit-Vintage Engine are imported from below package:-
import static org.junit.jupiter.api.Assertions.*;
DashboardPageDefinitions.java
public class DashboardPageDefinitions { @Steps StepDashboardPage dashPage; @Step public void verifyAdminLogin() { dashPage.loginVerify(); } }
The corresponding Test Step classes are - StepLoginPage.java and StepDashboardPage.java.
StepLoginPage.java
public class StepLoginPage extends PageObject { @Step("Enter Username") public void inputUserName(String userName) { $(By.name("txtUsername")).sendKeys((userName)); } @Step("Enter Password") public void inputPassword(String passWord) { $(By.name("txtPassword")).sendKeys((passWord)); } @Step("Click Submit Button") public void clickLogin() { $(By.name("Submit")).click(); } @Step("Error Message on unsuccessful login") public String errorMessage() { String actualErrorMessage = $(By.id("spanMessage")).getText(); return actualErrorMessage; } @Step("Click Forget Password Link") public void clickForgetPasswordLink() { $(By.linkText("Forgot your password?")).click(); } }
StepDashboardPage.java
public class StepDashboardPage extends PageObject { @Step("Successful login") public void loginVerify() { String dashboardTitle = $(By.id("welcome")).getText(); assertThat(dashboardTitle, containsString("Welcome")); } }
Step 10 - Create a Serenity-Cucumber Runner class
Cucumber runs the feature files via JUnit, and needs a dedicated test runner class to actually run the feature files. When you run the tests with Serenity, you use the CucumberWithSerenity test runner. You also need to use the @CucumberOptions class to provide the root directory where the feature files can be found.
import org.junit.runner.RunWith; import io.cucumber.junit.CucumberOptions; import net.serenitybdd.cucumber.CucumberWithSerenity; @RunWith(CucumberWithSerenity.class) @CucumberOptions(plugin = {}, features = "src/test/resources/features/LoginPage.feature", glue = "com.example.SerenityCucumberJunit5Demo.definitions") public class SerenityRunnerTest { }
Step 11 - Create serenity.conf file under src/test/resources
The serenity configuration file is used to configure the drivers so the test cases can run successfully. This file contains an operating system specific binary. The binary file sits between your test and the browser. It acts as an intermediary, an interface between your tests and the browser you are using.
You can also configure the webdriver.base.url property for different environements in the serenity.conf configuration file.
webdriver { driver = firefox } # # Define drivers for different platforms. Serenity will automatically pick the correct driver for the current platform # environments { default { webdriver.base.url = "https://opensource-demo.orangehrmlive.com/" } dev { webdriver.base.url = "https://opensource-demo.orangehrmlive.com/dev" } staging { webdriver.base.url = "https://opensource-demo.orangehrmlive.com/staging" } prod { webdriver.base.url = "https://opensource-demo.orangehrmlive.com/prod" } }
Step 11 - Create serenity.properties file in the root of the project
serenity.project.name = Serenity and Cucumber6 and JUnit5 Demo
Step 12 - Run the tests from Command Line
Open commandline and go to the location where pom.xml of the project is present and type the below command.
mvn clean verify
Step 13 - Serenity Report Generation
Above it shows the execution status. There are 5 passed and 1 failed test case.
There are multiple type of reports are generated. We are interested in index.html and serenity-summary.html. To know more about Serenity Reports, please refer tutorials for Index.html and Serenity-Summary.html.
index.html
2. serenity-summary.html
We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.