Assertions in Selenium: A Comprehensive Guide
In Selenium, assertions are used to verify whether a
certain condition is true or false during the execution of a test. If the
condition is true, the test continues, but if it’s false, the test will fail,
and the execution will stop. Assertions are useful to validate the expected behaviour
of a web application.
v There
are two types of assertions commonly used in Selenium:
- Hard
Assertions (Built-in assert statements):
- These
assertions immediately stop the test execution if the condition fails.
- If
the assertion fails, an exception is thrown, and the subsequent steps are
not executed.
- Hard
assertions are typically used to check if specific conditions (e.g., page
title, element visibility) are met.
- Soft
Assertions (from TestNG or JUnit):
- These
allow the test to continue even if an assertion fails. All soft
assertions are collected and reported at the end of the test execution.
- Soft
assertions are helpful when you want to collect multiple validation
results in a single run.
Using Hard Assertions in Selenium
Here is an example using JUnit (common assertion approach
in Java for Selenium):
1. Hard Assertion Example with JUnit:
java
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class HardAssertionExample {
public static
void main(String[] args) {
WebDriver
driver = new ChromeDriver();
driver.get("http://example.com");
// Hard
assertion: Check if the page title is correct
String expectedTitle
= "Example Domain";
String actualTitle
= driver.getTitle();
// This
will throw an AssertionError if the titles don't match
Assert.assertEquals(expectedTitle, actualTitle);
// More
steps will not be executed if the assertion fails
driver.quit();
}
}
Explanation:
- The
assertEquals assertion checks if the actual page title matches the
expected title.
- If
the assertion fails, it throws an error and stops further test execution.
Using Soft Assertions in Selenium (TestNG Example)
In TestNG, you can use soft assertions. These allow tests
to continue running even if an assertion fails.
2. Soft Assertion Example with TestNG:
java
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.chrome.ChromeDriver;
import
org.testng.asserts.SoftAssert;
public class SoftAssertionExample
{
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("http://example.com");
SoftAssert softAssert = new SoftAssert();
// Soft assertion: Check if the page
title is correct
String expectedTitle = "Example
Domain";
String actualTitle = driver.getTitle();
// This will not stop the test
execution if it fails
softAssert.assertEquals(actualTitle,
expectedTitle);
// You can perform more actions even if
the assertion fails
softAssert.assertTrue(driver.findElement(By.id("someElement")).isDisplayed());
// Assert all at the end of the test
softAssert.assertAll();
driver.quit();
}
}
Explanation:
- SoftAssert
allows assertions to be executed even if the condition fails.
- The
assertAll() method is called at the end to gather all assertion results
and determine whether the test passed or failed.
- If
any assertion fails, it will be reported, but the test will continue to
execute the remaining steps.
v Common
Assertion Methods in Selenium (Using JUnit and TestNG)
1. JUnit
Assertions (Hard Assertions):
- assertTrue(condition):
Verifies if the condition is true.
- assertFalse(condition):
Verifies if the condition is false.
- assertEquals(expected,
actual): Verifies if the expected value matches the actual value.
- assertNotEquals(expected,
actual): Verifies if the expected value does not match the actual value.
- assertNull(object):
Verifies if the object is null.
- assertNotNull(object):
Verifies if the object is not null.
2. TestNG Soft
Assertions:
- assertTrue(condition):
Verifies if the condition is true.
- assertFalse(condition):
Verifies if the condition is false.
- assertEquals(expected,
actual): Verifies if the expected value matches the actual value.
- assertNotEquals(expected,
actual): Verifies if the expected value does not match the actual value.
- assertNull(object):
Verifies if the object is null.
- assertNotNull(object):
Verifies if the object is not null.
v When
to Use Assertions in Selenium:
- Validation:
To validate the page title, element visibility, or other attributes during
the test.
- Form
Validation: To confirm that the correct data appears after form
submission.
- Error
Checking: To ensure that error messages, if any, are correctly displayed.
- State
Validation: To check if specific elements are enabled, disabled, selected,
or deselected.
In conclusion, assertions are an essential part of test automation in Selenium. You can use hard assertions for strict validation and soft assertions when you want to continue executing the test even after a failure.