Learning Outcome
5
Validate page title, URL, and element text during testing
4
Apply SoftAssert to verify multiple conditions in a test
3
Use assert methods to validate application behavior
2
Differentiate between Hard Assertions and Soft Assertions
1
Understand the role of assertions in test automation
Hard vs. Soft Assertions
Type
Behavior
Description
Hard Assert
Soft Assert
Continues execution even after failures
Stops test immediately if condition fails
Execution is aborted
All failures reported at the end
org.testng.Assert classorg.testng.asserts.SoftAssert classUsing Assert Class for Critical Checks
@Test
public void testTitle() { S
tring expected = "Dashboard";
String actual = driver.getTitle();
Assert.assertEquals(actual, expected);
}assertTrue(condition)
Verifies a condition evaluates to true
Verifies a condition evaluates to false
Confirms an object reference is not null
Soft Assertions: Check Everything, Then Fail
SoftAssert soft = new SoftAssert();
Create SoftAssert Instance
Add Multiple Assertions
soft.assertEquals(driver.getTitle(), "Home");
soft.assertEquals(driver.getCurrentUrl(), "https://example.com");
Report All Failures
soft.assertAll(); // Required to report failures
Assert Title, URL, and Element Text
1. Verify Page Title
Assert.assertEquals(driver.getTitle(), "Dashboard");
2. Check Current URL
Assert.assertTrue(driver.getCurrentUrl().contains("dashboard"));
3. Validate Element Text
String text = driver.findElement(By.id("greet")).getText();
Assert.assertEquals(text, "Welcome User");
Summary
5
Without assertions, tests may run successfully but verify nothing
4
Assertions help validate title, URL, and page elements
3
Soft assertions continue test execution and report failures later
2
Hard assertions stop the test immediately when a failure occurs
1
Assertions verify whether expected results match actual results
Quiz
Which assertion type reports failures at the end of execution?
A. Hard Assertion
B. Static Assertion
C. Soft Assertion
D. Manual Assertion
Quiz-Answer
Which assertion type reports failures at the end of execution?
A. Hard Assertion
B. Static Assertion
C. Soft Assertion
D. Manual Assertion