|
Selenium is one of the widely used automated testing tools for web applications. It supports writing test scripts in different languages, including Java, C#, Python, and Ruby. The primary aspect of writing automation is validation. By using methods such as Assert and Verify, we can validate any functionalities and create reliable scripts.
Let’s understand more about Assert and Verify, their differences, usage, and how we can use them in Selenium scripts.
Understanding Assertions in Testing
Assertions are one of the basic concepts in testing. Assertions are used to verify whether a particular condition is true or not. If the condition is true, test execution continues. If the condition is false, the test execution stops at that point and marks the test case as failed. Here is an informative blog to understand Assertion Testing: Key Concepts and Techniques.
Asserts help to find if the application behaves as expected and if there is any deviation from the requirement, that is captured and mark the scenario as failed.
Types of Assertions
Assertions are classified into two types based on their behavior. Let us understand more about the types of assertions.
-
Hard Assertions: These are the most commonly used assertions. When a hard assertion case fails, the test execution stops at that step and marks the test step failed. Hard executions are mainly used when the subsequent step has a dependency on the success of assertions.For example, consider we have an automation script to add an item to the cart. The next step would be to check if the cart is empty, only then we can proceed to the checkout page. To check this we can add a hard assert, which validates that the cart is not empty, assert passes and goes to the checkout page. Otherwise, if the cart is empty, the assert and the test case fails.
-
Soft Assertions: Soft assertions are different from hard ones. Here, the test execution will not stop even if the assertion fails. Instead the failure will be recorded, and the test execution will continue. At the end of test, all the failures will be reported. Soft assertions are mainly used to validate the UI elements in the screen which are not critical for functionality.For example, we have added an assertion to validate if the cart button is in red color. But in application it has blue color. So, the assertion gets failed. But this won’t break the test case and it continues to execute.
Common Assert Methods in Selenium
Selenium provides several assert methods that can be used to validate different aspects of the web application. Some of the commonly used assert methods include:
-
assertEquals(expected, actual): This method checks whether the expected value is equal to the actual value. It is used to verify the correctness of specific values, such as text on a web page, element attributes, or any other data.
Assert.assertEquals("Expected Title", driver.getTitle());
-
assertTrue(condition): This method verifies that a specified condition is true. It is typically used to check the presence of elements, visibility of elements, or any boolean conditions.
Assert.assertTrue(driver.findElement(By.id("elementId")).isDisplayed());
-
assertFalse(condition): This method checks that a specified condition is false. It is often used to ensure that certain elements are not present or not visible on the page.
Assert.assertFalse(driver.findElement(By.id("elementId")).isDisplayed());
-
assertNotNull(object): This method verifies that an object is not null. It is useful for checking that certain elements are present on the page.
WebElement element = driver.findElement(By.id("elementId")); Assert.assertNotNull(element);
-
assertNull(object): This method checks that an object is null. It is used to verify that certain elements are not present on the page.
WebElement element = driver.findElement(By.id("nonExistentElementId")); Assert.assertNull(element);
Common Verify Methods in Selenium
Selenium does not provide built-in verify methods. However, you can achieve verification using soft assertions or custom implementation. Here are a few ways to implement verification in Selenium:
-
Using Soft Assertions: Soft assertions can be used to achieve verification by collecting all assertion failures and reporting them at the end of the test.
SoftAssert softAssert = new SoftAssert(); softAssert.assertEquals(driver.getTitle(), "Expected Title"); softAssert.assertTrue(driver.findElement(By.id("elementId")).isDisplayed()); softAssert.assertAll(); // This will report all the assertion failures
-
Custom Verification Methods: You can create custom verification methods to log failures and continue with the test execution.
public void verifyEquals(String actual, String expected, String message) { try { Assert.assertEquals(actual, expected); } catch (AssertionError e) { System.err.println("Verification failed: " + message); } } public void verifyTrue(boolean condition, String message) { try { Assert.assertTrue(condition); } catch (AssertionError e) { System.err.println("Verification failed: " + message); } }
Best Practices for Using Assert and Verify Methods
-
Choose the Right Type: Use hard assertions for critical validations where subsequent test steps depend on the success of the assertion. Use soft assertions or verification methods for non-critical checks where you want to gather more information about multiple failures.
-
Clear and Descriptive Messages: Always provide clear and descriptive messages in your assertions and verification methods. This helps in understanding the context of the failure when analyzing test reports.
Assert.assertEquals(driver.getTitle(), "Expected Title", "Page title does not match");
-
Avoid Overuse of Assertions: While assertions are essential, overusing them can make your tests brittle and difficult to maintain. Focus on validating the critical aspects of the application.
-
Group Related Assertions: Group related assertions together to make your tests more organized and readable. This also helps in identifying which part of the test failed when analyzing test reports.
SoftAssert softAssert = new SoftAssert(); softAssert.assertEquals(driver.getTitle(), "Expected Title", "Page title does not match"); softAssert.assertTrue(driver.findElement(By.id("elementId")).isDisplayed(), "Element is not displayed"); softAssert.assertAll();
-
Handle Exceptions Gracefully: When using custom verification methods, handle exceptions gracefully to ensure that the test execution continues smoothly.
public void verifyElementPresent(By locator, String message) { try { Assert.assertTrue(driver.findElement(locator).isDisplayed(), message); } catch (NoSuchElementException e) { System.err.println("Verification failed: Element not found - " + message); } }
-
Use Verifications for Non-Critical Conditions: Use verifications for conditions that are important but not critical. This allows the test to continue and log all failures, providing a comprehensive report.
-
Combine Assertions and Verifications: Combining both allows for robust test scenarios. Assertions can be used for initial critical checks, followed by verifications for other validations.
-
Log Clear Messages: Ensure that assertion and verification messages are clear and descriptive, making it easier to understand the cause of failures.
-
Use Soft Assertions Wisely: While soft assertions are useful, overuse can lead to tests that are difficult to debug. Ensure that soft assertions are used in appropriate contexts.
- Keep Tests Independent: Ensure that tests do not depend on each other. Each test should be able to run independently and provide meaningful results.