Assert and Verify Methods in Selenium

Topics Covered

Overview

Assert and verify in Selenium are methods used for validating expected conditions during test execution. The assert methods halt the test immediately if the condition fails, while the verify methods continue the test execution even if the condition fails. This allows for collecting multiple verification failures before reporting the final results. These methods are essential for ensuring that the application under test behaves as expected and for validating the desired outcomes during automated testing.

What are Assertions?

Assertions are statements or expressions used in software testing to validate expected outcomes or conditions. They are typically written within test cases to check if a specific condition holds during the execution of the test.

When a test assertion is executed, it evaluates the actual result or state of the system against an expected value or condition. If the assertion passes, the test continues executing. However, if the assertion fails, it indicates a test failure, and the test is halted, marking it as unsuccessful.

Assertions play a crucial role in automated testing as they provide a means to validate the correctness of the application being tested. They help identify defects or unexpected behaviors in the software by comparing the actual outcomes with the expected ones. Assertions can be used to check various conditions, such as equality, inequality, the presence of elements, error messages, and more.

Commonly used assertion libraries in different programming languages include JUnit for Java, NUnit for .NET, and pytest for Python. These libraries provide a wide range of assertion methods that testers can use to perform comparisons and verify the correctness of their test cases.

Types of Assertions

There are mainly two types of Assertions, Hard Assertions, and Soft Assertions. Let's discuss them in detail.

Hard Assertions or Asserts

Hard assertions, also known as asserts, are a type of assertion that halt the test execution immediately when the assertion fails. They are commonly used in testing frameworks and libraries to validate expected conditions or outcomes.

When a hard assertion is executed and the condition being checked is not met, it triggers an assertion failure. The test execution stops at that point, and the test is marked as a failure. Any subsequent test steps or assertions after the failed hard assertion are not executed.

Hard assertions are useful when the failure of a specific condition implies that the test case cannot proceed further or the expected behavior of the system under test is not met. They provide a clear indication of test failures and help identify issues in the system.

Methods under Hard Assertions in Selenium:

  • assertEquals(expected, actual): Compares the expected value with the actual value. If they are not equal, the test fails.
  • assertTrue(condition): Checks if the given condition is true. If it is false, the test fails.
  • assertFalse(condition): Checks if the given condition is false. If it is true, the test fails.
  • assertNull(object): Checks if the given object is null. If it is not null, the test fails.
  • assertNotNull(object): Checks if the given object is not null. If it is null, the test fails.
  • assertSame(expected, actual): Checks if the expected object and actual object refer to the same instance. If they do not refer to the same instance, the test fails.
  • assertNotSame(expected, actual): Checks if the expected object and actual object do not refer to the same instance. If they refer to the same instance, the test fails.
  • fail(message): Explicitly fails the test with the given failure message.

Here's an example of hard assertion methods using Java's JUnit framework:

Code Explanation

In this example, we have a loginTest method that performs a login functionality on a web page. After the login, we use hard assertions to verify the expected conditions:

  • Assert.assertTrue(welcomeMessage.isDisplayed(), "Welcome message is not displayed") verifies that the welcome message is displayed on the page.
  • Assert.assertEquals(actualUsername, expectedUsername, "Incorrect username displayed") compares the actual username displayed on the page with the expected username.

If any of these assertions fail, the test case will immediately fail, and the failure message will be displayed.

Soft Assertions or Verify

Soft asserts, also known as soft assertions or verification points, are a feature provided by some testing frameworks or assertion libraries to validate multiple conditions within a single test case without immediately failing the test execution. Unlike hard assertions (which halt the test execution on failure), soft asserts log the failures and allow the test to continue executing, capturing all the failures encountered.

Soft asserts are beneficial when you want to perform multiple validations in a test case and collect all the failures rather than stopping the test execution at the first failure. This can help in scenarios where you want to gather a comprehensive list of issues or failures within a single test run.

Some of the common methods used under soft assertions are:

  • assertEquals(expected, actual): Compares the expected value with the actual value. If they are not equal, the failure is recorded but does not stop the test execution.
  • assertNotEquals(first, second): Checks that the two values are not equal. If they are equal, the failure is recorded.
  • assertTrue(condition): Checks if the given condition is true. If it is false, the failure is recorded.
  • assertFalse(condition): Checks if the given condition is false. If it is true, the failure is recorded.
  • assertNull(object): Checks if the given object is null. If it is not null, the failure is recorded.
  • assertNotNull(object): Checks if the given object is not null. If it is null, the failure is recorded.
  • assertSame(expected, actual): Checks if the expected object and actual object refer to the same instance. If they do not refer to the same instance, the failure is recorded.
  • assertNotSame(expected, actual): Checks if the expected object and actual object do not refer to the same instance. If they refer to the same instance, the failure is recorded.

These methods are similar to the hard assertion methods, but with soft asserts, the failures are accumulated instead of immediately stopping the test execution. To trigger the soft assert and log all the failures, you need to call the assertAll() method at the end of the test case.

Code examples demonstrating the use of Soft asserts are shown below:

Code Explanation

In this example, the SoftAssert class is used to perform soft assertions. The assertEquals(), assertTrue(), and assertFalse() methods are used to validate conditions. Failure messages are provided to help identify the cause of failure. Finally, the assertAll() method is called to trigger the soft assert and log any failures encountered during the test execution.

The assertAll() method is essential to ensure that all soft assertions are evaluated and any failures are logged. Without calling assertAll(), the soft assertions will not be processed, and failures will not be recorded.

Intergration of Soft Assertion with Selenium Webdriver

Soft assertions, when integrated with Selenium WebDriver, play a crucial role in test execution and reporting. One of the key features of soft assertions is the ability to log failures without immediately throwing an exception. This feature is especially valuable in Selenium WebDriver tests, where multiple assertions may be performed within a single test method.

When executing a test with traditional assertions (e.g., using assertEquals, assertTrue, etc.), the test execution is halted as soon as an assertion failure occurs. This can be problematic in scenarios where you want to capture all the failures and continue the test execution to gather a complete overview of the test's success or failure.

With soft assertions, failures are logged instead of immediately stopping the test. This allows the test to proceed and capture all the failures encountered during the execution. At the end of the test method, the assertAll() method is called, which checks if any assertions have failed. If there are failures, assertAll() throws an exception and provides a consolidated report of all the failures encountered during the test.

Code Explanation

In this example, we have a SoftAssert object initialized in the setUp method using SoftAssert softAssert = new SoftAssert(). This allows us to use soft assertions throughout the test.

In the testSoftAssertions method, we perform various assertions using softAssert instead of traditional assert statements. Each assertion captures failures but does not throw an exception immediately. This allows the test to continue executing even if an assertion fails.

After all the assertions have been performed, we call softAssert.assertAll(). This method checks if any of the assertions failed and throws an exception with a consolidated report of all the failures. If all the assertions pass, the test continues without any interruption. Finally, in the tearDown method, we clean up by quitting the WebDriver.

Difference between Assert and Verify

In the context of software testing frameworks like TestNG or JUnit, both "Assert" and "Verify" are methods used to validate expected conditions in test cases. However, there is a subtle difference between the two:

  • Assert: The assert method is commonly used in assertion frameworks like TestNG or JUnit. When an assertion fails, it throws an exception immediately, and the test case execution is halted. This means that if an assertion fails, the subsequent statements in the test method will not be executed.

  • Verify: The verify method, on the other hand, is not a built-in feature of assertion frameworks. It is typically associated with tools like Selenium WebDriver, which allows you to check the expected conditions without immediately stopping the test execution. If verification fails, it will log the failure but continue executing the test case. This allows you to capture multiple failures in a single test run.

The difference between Assert and Verify in Selenium can be summarized as follow:

CriteriaAssertVerify
PurposeValidates expected conditionsValidates expected conditions
ExecutionImmediately stops test executionLogs failures but continues test execution
FailureThrows an exceptionLogs failure without stopping test execution
UsageAssertion frameworks like TestNG, JUnitTools like Selenium WebDriver
ImpactHalts test execution on failureContinues test execution on failure

Conclusion

  • Assertions are statements or expressions used in software testing to validate expected outcomes or conditions. There are two types of assertions: hard assertions (asserts) and soft assertions (verify).
  • They help verify if the actual results or system state match the expected values or conditions defined in the test cases.
  • Hard assertions immediately halt the test execution when a failure occurs, marking the test as unsuccessful.
  • Soft assertions log failures but allow the test execution to continue, capturing multiple failures in a single test run. Soft assertions are useful when you want to perform multiple validations within a test case and gather a comprehensive list of failures.
  • Hard assertions are commonly used in testing frameworks to validate critical conditions that indicate test failure.
  • Assertions provide a means to automatically check the correctness of the application being tested and help identify defects or unexpected behaviors.