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.
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.
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 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.
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 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:
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, 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.
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();
}
}
The main difference between hard and soft assertions is how they handle failures:
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.
When testing Selenium assertions, BrowserStack Automate offers an unmatched environment to enhance your automated testing process. Here's why:
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
Get visual proof, steps to reproduce and technical logs with one click
Continue reading
Try Bird on your next bug - you’ll love it
“Game changer”
Julie, Head of QA
Try Bird later, from your desktop