vibssingh posted: " HOME In the previous tutorial, I have explained about the Integration of Serenity BDD with JUnit4. In this tutorial, I'll explain the Integration of Serenity BDD with JUnit5. Pre-Requisite Java 11 installedMaven installedEclipse or IntelliJ inst"
In the previous tutorial, I have explained about the Integration of Serenity BDD with JUnit4. In this tutorial, I'll explain the Integration of Serenity BDD with JUnit5.
Pre-Requisite
Java 11 installed
Maven installed
Eclipse or IntelliJ installed
This framework consists of:
Java 11
Maven – 3.8.1
Serenity – 2.6.0
Serenity JUnit5– 1.6.0
JUnit Jupiter – 5.8.0
JUnit Vintage - 5.8.0
Maven Surefire Plugin – 3.0.0-M5
Maven Failsafe Plugin – 3.0.0-M5
Maven Compiler Plugin – 3.8.1
Implementation Steps
Update Properties section in Maven pom.xml
Add repositories and pluginRepository to Maven pom.xml
Add Serenity, Serenity Cucumber and JUnit dependencies to POM.xml
Update Build Section of pom.xml
Create test code under src/test/java folder
Create serenity.conf file under src/test/resources
Create serenity.properties file in the root of the project
Run the tests through commandline which generates Serenity Report
Structure of Project
Step 1 – Update Properties section in Maven pom.xml
Step 5 - Create test code under src/test/java folder
LoginPageDefinitions.java
@SerenityTest public class LoginPageDefinitions { @Managed WebDriver driver; @Steps StepLoginPage loginPage; @Steps StepDashboardPage dashPage; @Steps StepForgetPasswordPage forgetpasswordPage; @Test @Title("Login to application should be successful") public void sucessfulLogin() { // Given loginPage.open(); // When loginPage.inputUserName("Admin"); loginPage.inputPassword("admin123"); loginPage.clickLogin(); // Then dashPage.loginVerify(); } @Test @Ignore @Title("Login to application should be unsuccessful with error message") public void unsucessfulLogin() throws InterruptedException { // Given loginPage.open(); // When loginPage.inputUserName("abc"); loginPage.inputPassword("abc12"); loginPage.clickLogin(); // Then String actualErrorMessage = loginPage.errorMessage(); Assert.assertEquals("Invalid credentials", actualErrorMessage); } @Test @Title("Verify Forgot your password link") public void clickForgetPasswordLink() { // Given loginPage.open(); // When loginPage.clickForgetPasswordLink(); // Then Assert.assertTrue(forgetpasswordPage.ForgetPasswordPage()); } }
To run a JUnit5 test with Serenity BDD, simply add the annotation @net.serenitybdd.junit5.SerenityTest (instead of @org.junit.runner.RunWith(net.serenitybdd.junit.runners.SerenityRunner.class) for JUnit4.
@SerenityTest
@Test is imported from package:-
import org.junit.jupiter.api.Test;
StepDashboardPage.java
public class StepDashboardPage extends PageObject { @Step("Successful login") public void loginVerify() { String dashboardTitle = $(By.id("welcome")).getText(); assertThat(dashboardTitle, containsString("Welcome")); } }
StepForgetPasswordPage.java
public class StepForgetPasswordPage extends PageObject { @Step("Verify Forget Password Page ") public boolean ForgetPasswordPage() { Boolean resetPasswordButton = $(By.id("btnSearchValues")).isDisplayed(); return resetPasswordButton; } }
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(); } }
The @Steps annotation marks a Serenity step library. Create the test following the Given/When/Then pattern and using step methods from the step library. The @Title annotation lets you provide your own title for this test in the test reports. Serenity @Title is considered for the Serenity report.Consistently with Junit4 the @Title annotation does not influence the name in the Junit report.
The JUnit Serenity integration provides some special support for Serenity Page Objects. In particular, Serenity will automatically instantiate any PageObject fields in your JUnit test.
Junit5 @Disabled annotation can be used on test and step methods(same as @Ignore in JUnit4).
Step 6 - Create serenity.conf file under src/test/resources
Serenity.conf file is used to specify various features like type of webdriver used, various test environments, run test in headless mode and many more options.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.