Turn your manual testers into automation experts! Request a DemoStart testRigor Free

Assert and Verify Methods in Selenium for Robust Testing

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.
  • 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.
  • 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"));
  • 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"));

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.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 {
      } 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");
  • 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.

Challenges using Assert and Verify

The main challenge of using assert or verify is the dependency on element identification. Selenium uses the conventional method of element identification where the element properties like id, class, CSS Selector or XPath are used. All these properties depend on the DOM properties of the element. Therefore, if any element’s property or any hierarchy in DOM changes, these elements will be unable to be identified. This causes the test case to fail.

These failures are false positives, which means the application did not fail, but an error in the automation script caused the test to fail. These failures make the automation less credible. This is one of the reasons why selenium is not preferred for automation tools. That is where modern automation tools such as testRigor excel.

Assert and Verify in testRigor

Using testRigor, you can easily bypass the drawbacks of Selenium assertions through its intelligent capabilities. testRigor stands out by enabling users to write test scripts in plain English (or other natural languages), eliminating the need for coding expertise. This empowers a broader range of team members, including manual QA testers, management, business analysts, and stakeholders, to contribute to automated test creation. This also improves the test case coverage, thereby finding more bugs, and making the application more stable.

Also, testRigor uses a unique approach for element locators. You simply describe elements by the text you see on the screen, using the power of AI to find them automatically. This means your tests adapt to changes in the application’s UI, eliminating the need to update fragile selectors constantly. This helps the team focus more on creating new use cases than fixing the flaky XPaths. Here is an example where you identify elements with the text you see for them on the screen.
click "cart"
click on the button "Delete" below "Section Name"
testRigor assertion can be written in plain English as:
check that page contains "Item Deleted"

That’s it!

For more clarity you can go through powerful features of testRigor and its documentation.


Assert and Verify have been important for automation testing in Selenium, as they are the pillars of test validations. However, in the current software development, where organizations aim for quick and frequent release, it will be very difficult to trust tools such as Selenium, which has a high chance of creating false positive bugs.

In today’s competitive scenario using intelligent tools such as testRigor makes the software delivery easy and faster. testRigor makes the use of assertions easy and also speeds up the test execution by a tremendous 100X as compared to Selenium. Here is a list of testRigor’s benefits, which can help you decide better.

Join the next wave of functional testing now.
A testRigor specialist will walk you through our platform with a custom demo.
Related Articles

Zephyr Overview

Test cases form the basis of software testing, acting as a map that testers use to not just decide what to test but also to ...

Azure DevOps Overview

Continuous Integration and Continuous Delivery (CI/CD) is being increasingly adopted for software development to keep up with the ...

TestRail Overview

Having the assistance of tools to perform tasks is a true luxury. This holds good in every industry, even software testing. There ...