Junior — Middle
What is the difference between Assert and Verify commands when writing tests in Selenium?
sobes.tech AI
Answer from AI
In Selenium, when writing tests, the Assert and Verify commands are used to check conditions, but they differ in behavior upon failure:
-
Assert — if the condition is not met, the test is immediately interrupted, and further execution does not continue. This is useful when subsequent test steps are meaningless without a successful check.
-
Verify — if the check fails, the test continues to run, recording the error but not stopping the entire test. This is convenient when multiple conditions need to be checked within a single test, and all errors should be collected in one run.
Example in Java using TestNG:
import org.testng.Assert;
import org.testng.Reporter;
// Assert example
Assert.assertEquals(actualTitle, expectedTitle, "Page title does not match");
// Verify example (using try-catch to continue)
try {
Assert.assertEquals(actualTitle, expectedTitle, "Page title does not match");
} catch (AssertionError e) {
Reporter.log("Check error: " + e.getMessage());
// the test will continue
}