DEV Community

Saras Growth Space
Saras Growth Space

Posted on

Selenium Simplified — Assertions in Selenium (Validating Your Tests)

Introduction

In the previous articles, we learned how to:

  • Launch browsers with Selenium
  • Locate elements on a webpage
  • Handle waits and timing issues
  • Work with alerts, frames, and multiple windows
  • Perform advanced mouse and keyboard interactions

But there is an important question:

How do we verify that our test actually passed?

Simply clicking buttons or entering text does not confirm whether the application behaved correctly.

This is where assertions come in.


What Are Assertions?

Assertions are used to validate expected results during a test.

They help confirm whether the application behaves as intended.

For example:

  • Checking if a login was successful
  • Verifying that the page title is correct
  • Confirming that an error message appears

If the expected condition is true, the test passes.

If the condition is false, the test fails.


Why Assertions Are Important

Without assertions, an automation script would only perform actions but never verify outcomes.

Example automation without assertions:

driver.findElement(By.id("username")).sendKeys("user");
driver.findElement(By.id("password")).sendKeys("password");
driver.findElement(By.id("loginButton")).click();
Enter fullscreen mode Exit fullscreen mode

This script logs in but does not check if login succeeded.

A proper test should validate the result.


Common Assertion Example

Example: Verify page title after login.

String expectedTitle = "Dashboard";
String actualTitle = driver.getTitle();

Assert.assertEquals(actualTitle, expectedTitle);
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • expectedTitle → what we expect
  • actualTitle → what the application returns
  • If they match, the test passes.

Verifying Element Text

Sometimes we need to validate text displayed on the page.

Example: Verify success message.

String actualMessage = driver.findElement(By.id("message")).getText();

Assert.assertEquals(actualMessage, "Login successful");
Enter fullscreen mode Exit fullscreen mode

This confirms that the application shows the correct message.


Checking Element Visibility

Another common validation is checking whether an element is visible.

Example:

WebElement dashboard = driver.findElement(By.id("dashboard"));

Assert.assertTrue(dashboard.isDisplayed());
Enter fullscreen mode Exit fullscreen mode

If the dashboard is visible, the login was likely successful.


Types of Assertions

Most test frameworks support two main types of assertions.

Hard Assertions

Hard assertions immediately stop test execution if they fail.

Example:

Assert.assertEquals(actualTitle, expectedTitle);
Enter fullscreen mode Exit fullscreen mode

If this fails, the rest of the test will not run.


Soft Assertions

Soft assertions allow the test to continue execution even if an assertion fails.

All results are reported at the end of the test.

Example concept:

SoftAssert softAssert = new SoftAssert();

softAssert.assertEquals(actualTitle, expectedTitle);
softAssert.assertTrue(element.isDisplayed());

softAssert.assertAll();
Enter fullscreen mode Exit fullscreen mode

Soft assertions are useful when you want to collect multiple validation results in one test.


A Simple Login Test Example

Here is a simple test flow with an assertion:

driver.get("https://example.com/login");

driver.findElement(By.id("username")).sendKeys("user");
driver.findElement(By.id("password")).sendKeys("password");
driver.findElement(By.id("loginButton")).click();

String title = driver.getTitle();

Assert.assertEquals(title, "Dashboard");
Enter fullscreen mode Exit fullscreen mode

If the page title is Dashboard, the login test passes.


Good Assertion Practices

When writing automation tests, keep these best practices in mind:

  • Always validate important outcomes
  • Keep assertions clear and meaningful
  • Avoid unnecessary assertions
  • Validate user-visible results when possible

Assertions should focus on what matters for the test scenario.


Final Thoughts

Automation scripts perform actions, but assertions verify results.

Without assertions, automation would only simulate user behavior without confirming whether the application works correctly.

Using assertions properly helps ensure that your tests are:

  • Reliable
  • Meaningful
  • Useful for detecting bugs

This is what turns simple automation scripts into real automated tests.


Up Next in This Series

So far in this series we have covered:

  • Selenium architecture
  • Locators
  • Waits
  • Alerts, frames, and windows
  • Mouse and keyboard actions
  • Assertions

In the final article of this series, we will explore an important design pattern used in automation frameworks.

Next article:
Selenium Simplified — Page Object Model (POM) Explained

This concept helps make test automation cleaner, more maintainable, and easier to scale.

Top comments (0)