vibssingh posted: " HOME In the previous tutorial, I have explained Serenity BDD with Cucumber for Web Application. this tutorial, I will explain about the Integration of Serenity with JUnit4. Pre-Requisite Java 11 installedMaven installedEclipse or IntelliJ instal"
Step 5 - Create test code under src/test/java folder
@RunWith(SerenityRunner.class) 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 @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()); } }
The JUnit test can be run using the Serenity test runner.
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.
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")); } }
StepForgetPasswordPage.java
public class StepForgetPasswordPage extends PageObject { @Step("Verify Forget Password Page ") public boolean ForgetPasswordPage() { Boolean resetPasswordButton = $(By.id("btnSearchValues")).isDisplayed(); return resetPasswordButton; } }
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. When you declare a field of type StepLoginPage in the test, Serenity will instantiate it for you. The page is automatically instantiated and ready to be used.
@Managed WebDriver driver;
@Managed declares a WebDriver instance that will be managed by Serenity. The WebDriver instance will be initialized automatically.
The driver parameter lets you define what WebDriver driver you want to run these tests in. Possible values include firefox, chrome, iexplorer, phantomjs, appium, safari, edge and htmlunit . Default value of driver is Firefox.
@Managed(driver="chrome")
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.
Step 7 - Create serenity.properties file in the root of the project
serenity.project.name = Serenity and Junit Demo
Step 8 - Run the tests through commandline which generates Serenity Report
Open commandline and go to the location where pom.xml of the project is present and type the below command.
mvn clean verify
Below is the execution status.
There are 2 type of reports are generated.
Index.html
2. Emailable Report (Serenity-Summary.html)
These reports are present under /target/site/serenity.
Skipping the tests
In Serenity, you use the @Pending annotation, either for a test or for a @Step-annotated method, to indicate that the scenario is still being implemented and that the results are not available yet. These tests appear as 'Pending' (shown in blue) in the test reports.
@Test @Pending @Title("Verify Forgot your password link") public void clickForgetPasswordLink() { // Given loginPage.open(); // When loginPage.clickForgetPasswordLink(); // Then Assert.assertTrue(forgetpasswordPage.ForgetPasswordPage()); }
Tests marked with @Ignore will appear as 'Ignored' (from JUnit) appears as grey in the test reports.
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.