• Articles

What is Assert and Verify in Selenium Webdriver?

What is Assert and Verify in Selenium Webdriver?

In this blog, we will explore assert and verify in Selenium WebDriver, starting with the basics and gradually diving into more advanced concepts. Whether you’re new to Selenium or seeking a refresher, this blog will provide you with all the necessary knowledge to utilize assert in Selenium and verify effectively in your test automation journey.

Check out this video on Selenium Tutorial For Beginners to learn all its concepts:

Understanding Assertions and Verifications

In the world of test automation, assertions and verifications play a crucial role in ensuring the accuracy and reliability of tests. They allow us to validate expected outcomes and compare them with actual results. Also, it helps us identify potential issues and bugs in our application under test.

In this section, we will explore the concepts of assertions and verifications, their importance in test automation, and how to write effective assertions in Selenium WebDriver.

The Importance of Assertions in Test Automation

Assertions are critical components of test automation frameworks as they enable us to validate the correctness of our tests. They assist us to confirm the expected behavior of our application is consistent with the actual behavior during test execution.

By incorporating assertions into our test scripts, we can ensure that the application under test meets the desired criteria. This includes displaying the correct page, showing the expected content, or performing the intended actions.

Assertions serve as a safety net for our tests, allowing us to catch potential issues early in the development process. They act as checkpoints, alerting us to any unexpected behavior or deviations from the expected results. By detecting failures, assertions help identify bugs, compatibility issues, or regressions, providing developers and testers with valuable feedback to fix the problems promptly.

Do you want to become a professional in the field of Selenium!! We recommend you enroll in our Selenium Certification Training.

Writing Assertions in Selenium WebDriver

Writing Assertions in Selenium WebDriver

When writing assertions in Selenium WebDriver, we have various options and methods at our disposal to verify specific conditions and elements on a web page.

Let’s explore the basic syntax and usage of assertions, commonly used assertion methods and techniques for asserting element state and properties.

  • Basic Syntax and Usage: Assertions are commonly created in Selenium WebDriver using assertion libraries like TestNG, JUnit, or AssertJ. We can compare expected and actual values using the assertion techniques that are provided by these libraries.

    Before using assertions, we must import the necessary assertion class and create an instance of it. The assertion methods on this instance can then be used to do the desired comparisons. The assertion methods use expected and actual values as inputs, and they trigger an exception if the assertion is false.

For example, using TestNG assertions, we can write an assertion to compare two strings

import org.testng.Assert;
public class MyTestClass {
    public void testStringComparison() {
        String expected = "Hello";
        String actual = "Hello";
        Assert.assertEquals(actual, expected, "Strings do not match");

In this example, the assertEquals() method from the TestNG Assert class is used to compare the actual and expected strings. If the assertion fails, an exception is thrown with the specified error message.

  • Commonly Used Assertion Methods: Selenium WebDriver provides a range of commonly used assertion methods to cater for different verification scenarios. Some frequently used assertion methods include:
    • assertEquals(): Compares two values for equality.
    • assertTrue()/assertFalse(): Verifies a condition is true/false.
    • assertNotNull()/assertNull(): Verifies if an object is null/not null.
    • assertArrayEquals(): Compares two arrays for equality.
    • assertGreaterThan()/assertLessThan(): Verifies if a value is greater/less than another value.

      These methods, along with others available in assertion libraries, allow us to assert various conditions and make our tests more robust and reliable.
  • Asserting Element State and Properties: In Selenium WebDriver, we often need to verify the state or properties of web elements. For example, we may want to check if an element is displayed, enabled, or selected. WebDriver provides methods to retrieve and assert these element properties.

    For instance, to assert that a button is enabled, we can use the isEnabled() method:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class MyTestClass {
    public void testButtonState() {
        WebDriver driver = new ChromeDriver();
        WebElement button = driver.findElement(By.id("myButton"));
        Assert.assertTrue(button.isEnabled(), "Button is not enabled");

In this example, we locate a button element using its ID and then assert that it is enabled using the isEnabled() method.

Get 100% Hike!

Master Most in Demand Skills Now !

By combining element locating techniques with assertion methods, we can create powerful tests that verify the state and properties of various elements on a web page.

Check out the list of Selenium Interview Questions to prepare for your next interview.

Advantages and Disadvantages of Assert and Verify in Selenium

Both assertions and verifications have their advantages and disadvantages, making them suitable for different scenarios in test automation.

Advantages of Assertions

  1. Provides Immediate Feedback: Assertions immediately indicate the success or failure of a test, allowing for prompt debugging and issue identification.
  2. Focuses on Critical Functionality: Assertions are best suited for verifying critical functionality, where any failure requires immediate attention.
  3. Simplifies Troubleshooting: When an assertion fails, it provides a clear indication of the source of the problem, making troubleshooting and bug fixing easier.

Disadvantages of Assertions

  1. Terminates Test Execution: If an assertion fails, the test execution stops, preventing further validations. This can make the process of gathering information about multiple failures in a single test run challenging. 
  2. Limits Non-critical Verifications: Assertions are not ideal for non-critical validations or when you need to continue the test execution despite failures.

Advantages of Verifications

  1. Allows Non-critical Validations: Verifications are useful for checking non-critical conditions without halting the test execution. This enables gathering information about multiple failures in a single test run.
  2. Enhances Test Coverage: Verifications enable checking various conditions throughout the test, leading to broader test coverage.
  3. Supports Data Collection: Verifications can gather information about the system under test, such as the presence of elements or the state of certain variables.

Disadvantages of Verifications

  1. Can Lead to False Positives: Since verifications do not stop the test execution upon failure, multiple failures may occur without immediate notice. This can lead to false positives if not carefully managed.
  2. May Obscure the Root Cause: Verifications may not provide immediate insight into the root cause of a failure, making debugging, and issue identification more challenging.

Want to learn about Selenium with Python? Check out our blog on Selenium with Python.

Difference Between Assertion and Verification

PurposeValidate expected outcomesCheck if a condition is met
Behavior on FailureStops the test executionDoes not stop the test execution
ImportanceCritical validationsNon-critical validations
FeedbackImmediate feedback on failureFeedback provided, but test execution continues
Test ExecutionTerminates test execution upon failureContinues test execution even if verification fails
Multiple FailuresOnly reports the first failure encounteredAccumulates multiple failures for consolidated reporting
DebuggingProvides clear indication of the failure sourceMay not provide immediate insight into the root cause
Test CoverageFocuses on critical functionalityEnables checking multiple conditions for broader test coverage
Suitable forCritical functionality testingNon-critical or information-gathering validations

Also, check out the blog on how to use Selenium with Java.

Advanced Assertion Techniques

Advanced Assertion Techniques

In addition to standard assertions, there are advanced assertion techniques that can further enhance the effectiveness and flexibility of your tests:

  • Soft Assertions
    Soft assertions allow you to continue the test execution even if an assertion fails. They accumulate all the failures during the test and provide a consolidated report at the end. This technique is particularly useful when you want to validate multiple conditions without terminating the test on the first failure.
  • Custom Assertions
    Custom assertions enable you to create your own assertion methods tailored to your specific testing needs. By encapsulating complex verification logic into reusable methods, you can simplify test code, enhance readability, and promote maintainability.


Assertions and verifications serve distinct purposes in test automation. Assertions validate expected behavior, stopping the test execution upon failure, while verifications check conditions without halting the test. Choosing between assertions and verifications depends on the criticality of the validation and the desired test flow.

Drop any of your queries in our Selenium Community and start a discussion.

Course Schedule

Name Date Details
Testing Courses 20 Jul 2024(Sat-Sun) Weekend Batch
View Details
Testing Courses 27 Jul 2024(Sat-Sun) Weekend Batch
View Details
Testing Courses 03 Aug 2024(Sat-Sun) Weekend Batch
View Details