Contents

    Guides

    Understanding Selenium Assertions

    Published on

    September 10, 2025
    Understanding Selenium Assertions

    In the world of automated testing, especially when working with Selenium WebDriver, assertions are essential tools for validating test outcomes. 

    They allow testers to compare expected results against actual outcomes, ensuring the functionality of web applications. Selenium itself does not have built-in assertion mechanisms, so testers often rely on external libraries such as TestNG, JUnit, or even Python’s unittest to implement assertions.

    This article explores the differences between assert and verify in Selenium, dives into hard and soft assertions, and provides practical examples to demonstrate how these concepts can be implemented to enhance your test automation strategy.

    Difference Between Assert and Verify in Selenium

    In Selenium, the terms assert and verify are used frequently when checking conditions during test execution. Although they might seem similar, they serve different purposes.

    Assert:

    • Assert is used to perform a mandatory check within the test script.
    • When an assertion fails, the test is immediately stopped, and the failure is reported.
    • It's best suited for critical checks where a failure means the rest of the test is not relevant and should not continue.

    Verify:

    • Verify is used when the condition being tested is not crucial for the continuation of the test.
    • If a verification fails, the test continues, and all verification failures are collected for reporting at the end.
    • This is helpful when you want to gather multiple errors before halting the test.

    In essence, assertions are stricter checks that immediately impact the flow of test execution, while verifications allow the test to continue, enabling a more lenient approach for gathering failures.

    Assertions in Selenium

    Assertions in Selenium are crucial for validating if the actual output of the application under test matches the expected output. As mentioned, they help confirm the correctness of the application’s behavior.

    Let’s break down the different types of assertions used in Selenium WebDriver.

    Hard Assertions vs Soft Assertions

    Assertions in Selenium can be broadly divided into hard assertions and soft assertions. Each serves a different purpose, with distinct behaviors when the assertion fails.

    Hard Assertions

    Hard assertions are the most common form of assertions in Selenium. When a hard assertion fails, the test execution is stopped immediately. This is ideal for critical tests where the result of a failure means the test should not continue.

    Some common hard assertion methods include:

    • assertEquals(): Checks if two values are equal. If they aren’t, the test fails.
    • assertNotEquals(): Verifies that two values are not equal. If they are, the test fails.
    • assertTrue(): Verifies that a given condition is true. If the condition is false, the test fails.
    • assertFalse(): Verifies that a condition is false. If the condition is true, the test fails.
    • assertNull(): Checks if a value is null. If it isn’t, the test fails.
    • assertNotNull(): Verifies that a value is not null. If it is, the test fails.

    Here’s an example in Java demonstrating the use of these hard assertions:

    import org.testng.Assert;

    import org.testng.annotations.Test;

    public class HardAssertionsExample {

        @Test

        public void testHardAssertions() {

            String actual = "Selenium";

            String expected = "Selenium";

            // assertEquals

            Assert.assertEquals(actual, expected, "Test failed: Strings are not equal.");

                    // assertNotEquals

            Assert.assertNotEquals(actual, "Test", "Test failed: Strings are equal.");

            // assertTrue

            Assert.assertTrue(actual.equals("Selenium"), "Test failed: Condition is not true.");

            // assertFalse

            Assert.assertFalse(actual.equals("Test"), "Test failed: Condition is true.");

            // assertNull

            String value = null;

            Assert.assertNull(value, "Test failed: Value is not null.");

            // assertNotNull

            Assert.assertNotNull(actual, "Test failed: Value is null.");

        }

    }

    Soft Assertions

    Soft assertions, on the other hand, do not halt the test execution when a failure occurs. This allows the test to continue running and gather all assertion failures in a report at the end of the test.

    In Selenium, soft assertions are often implemented using libraries like TestNG or JUnit. TestNG has a built-in mechanism for soft assertions through the SoftAssert class.

    Implementing Soft Assertions Without a Library

    In some cases, you might want to implement soft assertions without relying on any external libraries. Below is a custom implementation of soft assertions in Java:

    import org.testng.Assert;

    import java.util.ArrayList;

    import java.util.List;

    public class SoftAssertionsExample {

        List<String> failureMessages = new ArrayList<>();

        public void softAssertEquals(String actual, String expected, String message) {

            if (!actual.equals(expected)) {

                failureMessages.add(message);

            }

        }

        public void assertAll() {

            if (!failureMessages.isEmpty()) {

                for (String message : failureMessages) {

                    System.out.println("Assertion Failed: " + message);

                }

                throw new AssertionError("Test failed due to soft assertions.");

            }

        }

        public static void main(String[] args) {

            SoftAssertionsExample example = new SoftAssertionsExample();

            // Performing soft assertions

            example.softAssertEquals("Selenium", "Selenium", "Test failed: Strings are not equal.");

            example.softAssertEquals("Test", "TestNG", "Test failed: Strings are not equal.");

            

            // Checking all assertions at the end

            example.assertAll();

        }

    }

    Hard Assertions vs Soft Assertions

    The main difference between hard and soft assertions is how they handle failures:

    • Hard Assertions: If a condition fails, the test is immediately stopped.
    • Soft Assertions: The test continues to execute, and all failures are collected until assertAll() is called.

    Hard assertions are ideal for critical tests where the failure of a condition means that the test cannot continue. Soft assertions, on the other hand, are useful when you want to run a test to completion and report all failures at the end.

    Benefits of Testing Selenium Assertions on BrowserStack

    When testing Selenium assertions, BrowserStack Automate offers an unmatched environment to enhance your automated testing process. Here's why:

    • Cross-Browser Compatibility: Test Selenium assertions across multiple real browsers and devices to ensure consistent results.
    • Real Device Testing: Run tests on real devices rather than emulators, ensuring accurate results for assertions like assertTrue or assertNull.
    • Parallel Testing for Faster Feedback: Execute tests simultaneously across multiple environments, speeding up feedback on assertion failures.
    • Scalability: Scale your Selenium tests to handle large test suites without worrying about infrastructure.
    • Access to Latest Browsers and Devices: Test on the most up-to-date browsers and devices, ensuring your assertions work across the latest environments.
    • Screenshots and Videos for Debugging: Use screenshots and video recordings of failed tests to quickly debug assertion issues.
    • CI/CD Integration: Integrate with your CI/CD pipeline to automatically run tests and validate assertions with every code change.

    Conclusion

    Assertions are indispensable in Selenium WebDriver automation. They help ensure that the web application behaves as expected by comparing actual and expected values during test execution. While hard assertions stop the test immediately upon failure, soft assertions allow the test to continue, giving a broader view of potential issues in the application.

    By mastering both hard and soft assertions, and understanding when to use each, you can significantly improve the robustness and effectiveness of your Selenium test automation. Whether you’re running tests locally or leveraging cloud platforms like BrowserStack, having a solid understanding of assertions will help you create more reliable, maintainable, and efficient test scripts.

    Run Selenium Tests on Cloud

    Data-rich bug reports loved by everyone

    Get visual proof, steps to reproduce and technical logs with one click

    Make bug reporting 50% faster and 100% less painful

    Rating LogosStars
    4.6
    |
    Category leader

    Liked the article? Spread the word

    Put your knowledge to practice

    Try Bird on your next bug - you’ll love it

    “Game changer”

    Julie, Head of QA

    star-ratingstar-ratingstar-ratingstar-ratingstar-rating

    Overall rating: 4.7/5

    Try Bird later, from your desktop