![]() Instead, let’s see how to change this code to use soft assertions. Therefore, the test will fail immediately when the first assertion fails. WebElement logOutButton = driver.findElement(By.linkText("Log out")) Īssert.assertTrue(logOutButton.isDisplayed(), "Log Out button is not visible") String actualMessage = successMessage.getText() Īssert.assertTrue(ntains(expectedMessage), "Actual message does not contain expected message.\nActual Message: " actualMessage "\nExpected Message: " expectedMessage) String expectedMessage = "Congratulations Michael Scott. WebElement successMessage = driver.findElement(By.tagName("strong")) String actualUrl = driver.getCurrentUrl() Īssert.assertEquals(actualUrl, expectedUrl, "Actual page url is not the same as expected") driver.get("") ĭriver.findElement(By.id("username")).sendKeys("student") ĭriver.findElement(By.name("password")).sendKeys("Password123") ĭriver.findElement(By.cssSelector("button#submit").click() Let’s look at the example of how to do this with the TestNG Java testing framework. Example using Soft and Hard assertionsĭifferent test frameworks implement soft assertions in a different way. We will not have to rerun the test just to learn that there is something else broken. So we will be able to fix the expected result for 5 and 6, and that would fix our test. If we use soft assertions, we will know that steps 5 and 6 failed, but step 7 passed. We will have to fix the test (change expected URL) and run it again, just to learn that the success message changed too. If we use hard assertions, the test will fail immediately after the URL verification, and all we would know is that the URL changed. ![]() Let’s assume the URL and Success message changed, but the logout button is still there. Therefore, all steps and soft assertions in the automated test will execute before failing the test. ![]() Soft Assertions are the type of assertions that do not throw an exception immediately when an assertion fails. Test steps after hard assertion will not be executed and the next test in the test suite will start. Soft Assert vs Hard AssertĪ Hard Assertion is a type of assertion that throws an exception immediately when an assert statement fails. In order to execute all three verifications without failing tests, we can use soft assertions instead of hard assertions. As a result, we can execute these verifications in any order. Steps 5, 6, and 7 are verifications, and they do not depend on each other. Verify button is displayed on the new page.A new page should contain expected text (‘Congratulations’ or ‘successfully logged in’).Verify new page URL contains /logged-in-successfully/.Lets look at Test case 1: Positive LogIn test on our Practice Test Login page. However, the exception is when we make a few verifications without additional test steps in between verifications and they do not depend on each other (can be executed in any order). And usually, one test case should have only one verification. In other words, small tests, that execute only one test case. One of the rules of good test automation is to write small fast atomic tests.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |