keepcalculator

06 Numbers and Powers

Assert vs Verify in Selenium WebDriver: Key Differences

Card No. 06·78930 · Updated · Standard formula, shown below

In Selenium WebDriver, an assert stops the test immediately when a condition fails, while a verify records the failure and lets the test continue running. You choose between them based on whether a failed check should halt the rest of the test or be logged for later review.

Background: Why Assertions and Verifications Exist

Automated tests do more than click buttons and type text – they check that the application behaves as expected. These checks are the core of what makes a test useful. Selenium provides two main ways to perform these checks: assertions and verifications. The concept comes from test automation frameworks like JUnit and TestNG, which introduced the idea of asserting that a condition is true. Selenium adopted this and added a softer version – verification – so testers could collect multiple failures in a single run instead of stopping at the first problem.

The distinction matters because it affects how quickly you find bugs and how much information you get from each test run. A hard stop (assert) is useful when the rest of the test would be meaningless if a prerequisite fails. A soft continue (verify) is useful when you want to see all the issues in one pass, even if later steps depend on earlier ones.

How Assert Works in Selenium WebDriver

An assertion is a check that, when it fails, throws an exception and immediately aborts the test method. In TestNG, you use Assert.assertEquals(), Assert.assertTrue(), Assert.assertFalse(), and similar methods. In JUnit, you use org.junit.Assert with the same style. When the condition fails, the test is marked as failed, and any code after the assertion line is skipped.

Example in TestNG:

Assert.assertEquals(driver.getTitle(), "Expected Title");
System.out.println("This line will not print if the assertion fails.");

If the title does not match, the test stops right there. This is ideal for checks that are critical to the test’s purpose – for example, verifying that you are on the login page before trying to log in. If you are not on the login page, the rest of the test would fail anyway, so stopping early saves time and makes the failure clear.

How Verify Works in Selenium WebDriver

Verification is a softer check. It records the failure but does not throw an exception, so the test continues to the next step. Selenium’s original verify commands (like verifyText and verifyElementPresent) were part of Selenium IDE. In WebDriver, you typically implement verification manually using a soft assertion library, such as TestNG’s SoftAssert or JUnit’s SoftAssertions (from AssertJ).

Example with TestNG’s SoftAssert:

SoftAssert softAssert = new SoftAssert();
softAssert.assertEquals(driver.getTitle(), "Expected Title");
softAssert.assertTrue(driver.findElement(By.id("submit")).isDisplayed());
// ... more checks ...
softAssert.assertAll(); // This throws an exception at the end if any check failed

Here, the test runs through all the checks, collects failures, and only reports them when you call assertAll(). This is useful when you want to see every broken element on a page in one run, rather than fixing one issue and re-running to find the next.

Choosing Between Assert and Verify

There is no universal rule, but a practical guideline is:

  • Use assert for checks that are essential for the test to proceed. If the check fails, the test cannot meaningfully continue.
  • Use verify for checks that are important but not blocking. You want to know if they fail, but you still want to see what happens later.
  • Use verify when you are checking multiple elements on a page (like all fields in a form) and you want a single report of all problems.
  • Use assert when a failure should stop the test immediately to avoid cascading errors that are hard to diagnose.

For example, if you are testing a checkout flow, you might assert that you are on the payment page before entering card details. If that fails, the test should stop. But you might verify that the shipping address, billing address, and total are all correct – you want to see all mismatches at once, so you use soft assertions.

Common Problems and Misconceptions

A frequent misconception is that Selenium WebDriver has built-in verify commands. It does not – WebDriver only provides findElement and similar low-level operations. The assert/verify distinction comes from the test framework you use (TestNG, JUnit, etc.). So when people say “Selenium assert” they usually mean “assertion using TestNG/JUnit inside a Selenium test.”

Another issue is mixing hard and soft assertions without realizing that soft assertions do not throw until you call assertAll(). If you forget to call it, the test will pass even if checks failed. Also, if you use a soft assertion and then a hard assertion fails, the soft failures collected so far are lost because the test aborts before assertAll() is reached.

Performance is another consideration. Soft assertions add a small overhead because they store results in memory. For a few checks this is negligible, but if you have thousands of verifications in a loop, it could slow down the test and consume memory.

Practical Examples: Assert vs Verify in Action

Consider a test that logs into a web application and checks the dashboard.

  1. Hard assert on login: After clicking the login button, assert that the dashboard heading is visible. If not, the test stops – there is no point checking dashboard elements if you are not on the dashboard.
  2. Soft verify on dashboard elements: Use soft assertions to check that the user name, account balance, and recent transactions are all displayed correctly. Even if one is wrong, you want to see the others.
  3. Hard assert on logout: At the end, assert that the logout button works and returns to the login page. If it fails, the test stops and reports the issue.

This combination gives you clear, actionable results: a failure in login stops the run early, while multiple dashboard issues are reported in one go.

Alternatives to Assert and Verify

Besides hard and soft assertions, there are other ways to check conditions in Selenium tests:

  • Explicit waits with expected conditions – These are not assertions but they help you wait for an element to appear before checking it. For example, WebDriverWait with ExpectedConditions.visibilityOfElementLocated() ensures the element is present before you assert on it.
  • Custom boolean checks – You can write a method that returns true/false and then use an assert or verify on that result. This is useful for complex conditions.
  • Hamcrest or AssertJ matchers – These libraries provide more readable assertions, like assertThat(actual, is(expected)). They work with JUnit and TestNG.
  • Conditional logic – Instead of asserting, you can use if statements to log a failure and continue. This is essentially manual verification.

Each approach has its place. The key is to be consistent and intentional about which you use for which check.

Frequently Asked Questions

Does Selenium WebDriver have a built-in verify command?

No. Selenium WebDriver does not include verify commands. The verify functionality comes from test frameworks like TestNG (SoftAssert) or AssertJ (SoftAssertions). If you see “verify” in Selenium documentation, it usually refers to Selenium IDE, which is a separate tool.

What happens if I use a soft assertion and never call assertAll()?

The test will pass even if the checks failed, because soft assertion failures are only thrown when you call assertAll(). Always call it at the end of your test to ensure failures are reported.

Can I mix hard and soft assertions in the same test?

Yes, you can and often should. Use hard assertions for critical prerequisites and soft assertions for non-critical checks. Just be aware that if a hard assertion fails, any soft failures collected up to that point are lost because the test stops.

Which is better for a large test suite?

It depends on your goal. Hard assertions fail fast, which can save time when a major feature is broken. Soft assertions give a full picture of all failures, which is useful for regression testing. Many teams use a mix: hard for smoke tests, soft for detailed regression suites.

Does using soft assertions slow down tests?

Yes, slightly, because each check stores a result in memory. For a handful of checks, the overhead is negligible. For thousands of checks in a loop, it could add noticeable time and memory usage. In such cases, consider using hard assertions or sampling checks.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →