What is the difference between Assert and Verify in Selenium?

In Selenium testing, assert and verify are two important concepts often encountered. These concepts allow us to validate expected outcomes against actual results during test execution. We will explore the differences between assert and verify and the variations of assert known as hard assert and soft assert.

Assert in Selenium

In Selenium, the assert method is used to compare expected values with actual values. It primarily validates whether a certain condition or behavior is as expected. When an assert statement is executed, it checks if the condition evaluates to true. If the condition is true, the test execution continues seamlessly. However, if the condition is false, the assert fails, and the test case immediately terminates, indicating a failure.

Example of Assert

Let's consider a scenario where we need to validate the title of a web page using assert in Selenium.

String actualTitle = driver.getTitle();
String expectedTitle = "My App - Home";
assert.assertEquals(actualTitle, expectedTitle);

In this example, if the actual title does not match the expected title, the assert statement will fail, and the test case will stop executing further, highlighting the failure.

Verify in Selenium

On the other hand, the verify method in Selenium also performs a similar task of comparing expected values with actual values. However, unlike assert, verify does not halt the test execution immediately upon a failure. It allows the test to continue running, even if the verification fails, collecting all the failures encountered during the test run. This enables testers to gain a comprehensive overview of all the discrepancies.

Example of Verify

Let's take an example of verifying the presence of a specific element on a web page using the verify method.

boolean isElementPresent = driver.findElement(By.id("myElement")).isDisplayed();
verify.assertTrue(isElementPresent);

In this example, if the element with the specified ID is not present on the web page, the verify statement will register the failure but allow the test case to proceed, continuing with other verifications or assertions.

Hard Assert and Soft Assert

In addition to the basic assert and verify methods, Selenium provides two variations.

  • Hard Assert

  • Soft Assert

Hard Assert

A hard assert is a strict form of assertion. When a hard assert statement fails, it immediately terminates the test execution, indicating a failure. It is commonly used when subsequent validations or assertions are not meaningful without the successful completion of the current one.

Soft Assert:

A soft assert is more lenient, flexible and allows the test execution to continue even if an assertion fails. It accumulates all the failures encountered during the test run and reports them collectively at the end. This helps testers gather comprehensive information about the application's state and identify multiple failures within a single test run.

To utilize soft assert, testers need to instantiate a soft assert object and use its methods for assertions. It is essential to call the assertAll() method at the end of the test case to report all the failures collectively.

Copyright ©2024 Educative, Inc. All rights reserved