DEV Community

Cover image for Assert and Verify in Selenium: Key Differences and Best Practices
satyaprakash behera
satyaprakash behera

Posted on

1 1 1 1 1

Assert and Verify in Selenium: Key Differences and Best Practices

Automation testing has become an important part of modern software development, and Selenium is one of the most popular web automation frameworks. To build robust and reliable tests using Selenium, it is crucial to understand the correct implementation of assertions and verification. In this article, we will explore the concepts of assert and verify in Selenium, understand their differences, and see some practical implementations for the same.

What are Assertions?

Assertions are the conditions that a test must meet to pass. In case an assertion fails, the test halts immediately, and you can go back to check the issue before proceeding. Assertion is an easy way to validate if the actual output of the test matches the expected output. These can be applied at different checkpoints throughout the test execution.

For example, you might use an assertion to verify that a particular web element contains a specific text or check whether a button is enabled.

In Selenium, assertions are used through the testing framework being used, i.e., TestNG or JUnit, where you can use built-in assertion libraries that allow you to compare expected and actual outcomes. Consider the below example of an assertion where we try to compare the actual and expected page titles.

`import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.Test;
public class MyFirstTest {

@Test
public void verifyPageTitle() {
    //Initialize webdriver
    WebDriver driver =  new ChromeDriver();
    driver.get("https://www.google.com/");

    //Get page title
    String actual= driver.getTitle();
    String expected = "Test title";

    //Assert that the page title matches with the expected title
    Assert.assertEquals(actual, expected,"Page title does not match!");

    //Clean up
    driver.quit();
}
Enter fullscreen mode Exit fullscreen mode

}`

Upon execution, you will notice that the test fails, with assertion error showing the failure as below-

Image description

When to use Assertions?
Critical Functionality– Assertions are suited for testing essential features where any failure invalidates subsequent steps.
Preconditions– When you need to establish the state of the application before performing further steps.
When an early failure should prevent the execution of the rest of the test.

Advantages of Using Assertions
Immediate Feedback—When assertions fail, you receive immediate feedback, which is helpful in debugging.
Clarity in Tests – Assertions make the tests more clear and readable by clearly stating what is expected.
Reliability – They help ensure that deviations from expected behaviors are caught early, hence maintaining reliability.

What are Verifications?

Verification, unlike assertions, checks conditions without halting the test execution. They are helpful in testing scenarios where you want to capture multiple errors during a single test run. Verification allows the test to continue and report all encountered issues instead of stopping at the first error.

For example, consider a scenario where you need to verify the status of the presence of multiple web elements on a page. Using verification, you can log the status of each element, and compile the list of discrepancies at the end. This comprehensive error log provides a holistic view of the application.

Though Selenium WebDriver does not provide a built-in verify command, modern testing frameworks offer ways to simulate verification behavior. For instance, TestNG offers a SoftAssert class that collects all the failures and marks the test as failed only after all the assertions have been validated. The below code is an example of SoftAssert in TestNG:

`import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class MyFirstTest {

@Test
public void verifyPageTitle() {
    //Initialize webdriver
    WebDriver driver =  new ChromeDriver();
    driver.get("https://www.google.com/");

   SoftAssert softAssert = new SoftAssert();
    //Get page title
    String actual= driver.getTitle();
    String expectedIncorrect = "Test title";
    String expectedCorrect = "Google";
//Assert that the page title matches with the expected title- this will fail
    softAssert.assertEquals(actual, expectedIncorrect,"Page title does not match!");
    System.out.println("Failure");

//Assert that the page title matches with the expected title- this will pass
    softAssert.assertEquals(actual, expectedCorrect,"Page title does not match!");
    System.out.println("Success");
    //Collect all soft assertion results
    softAssert.assertAll();

    //Clean up
    driver.quit();
}
Enter fullscreen mode Exit fullscreen mode

}`

In the above example, even when the first assertion fails due to an incorrect expected title, the test won’t stop executing. Only at the end, when assertAll() is called, all assertion results will be compiled, and the test result will be updated accordingly. Upon executing the code, the test results would look like below-

Image description

When to use Verification?

  • UI Testing – This involves verifying multiple elements or styles on a page when a single failure should not impact the entire test.
  • Comprehensive Reporting – When you need to capture and log multiple discrepancies to gain a holistic view of the application’s state.
  • Non-Critical Checks– When testing features where failures can be reviewed at a later stage and are not required for immediate attention.

Advantages of Using Verification

  • Comprehensive Error Reporting- Verification allows the capture of multiple issues in a single test execution, resulting in a detailed report.
  • Allows test continuity – Unlike the assertions where the test execution halts, verification allows for the continuation of the test through all checkpoints irrespective of any failure.
  • Enhanced debugging – Verification enables you to log multiple failures in a single run; developers and testers can address several issues in one go, thereby reducing overall debugging time.

Key Differences Between Assert And Verify

Image description

Best Practices To Implement Assertion And Verification

  • Use soft assertions when you need to verify multiple independent elements. However, avoid using them in critical test cases where a failure should prevent further execution of the test.
  • Provide descriptive error messages in your assertions and verifications to allow for easy debugging.
  • Automatically capture screenshots when an assertion or verification fails.
  • Implement explicit wait and fluent wait to handle dynamic content on web pages.
  • Ensure each test focuses on one specific aspect of the application.

Conclusion

Assertion and verification are two fundamental concepts of Selenium-based test automation. While assertions halt test execution upon failure, verifications are a lenient approach that allows tests to continue executing even when multiple discrepancies exist. Blending both ways judiciously can significantly enhance the robustness and maintainability of your tests. In this guide, we explored the details of assertions and verifications and the key differences between them. We have seen how by using certain best practices, we can enhance the efficiency of our test frameworks.

Source: This blog was originally published at testgrid.io

AWS Q Developer image

Your AI Code Assistant

Implement features, document your code, or refactor your projects.
Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

If you found this post helpful, please leave a ❤️ or a friendly comment below!

Okay