Assert and Verify are both used for validation in Selenium, but they differ in how they handle test execution. I have explained Assert and Verify in detail, along with example code, below:
Assert:
Assert immediately stops the test when a condition fails by throwing an exception, marking the test as failed.
Example Code:
String expectedText= “Google”;
WebDriver driver = new ChromeDriver();
driver.get(“https://www.google.com”);
String actualText = driver.getTitle();
Assert.AssertEquals(expectedText,actualText );
Verify:
Verify (commonly implemented using Soft Assertions) allows the test to continue even if a validation fails, collecting all failures and reporting them at the end of the execution.
Example Code:
String expectedText= “Google”;
WebDriver driver = new ChromeDriver();
driver.get(“https://www.google.com”);
String actualText = driver.getTitle();
If(expectedText.Equals(actualText)){
System.out.println(“Title matches with expected value”+expectedText);
}
else{
System.out.println(“Mismatch with title. Expected value”+expectedText+”Actual Value”+actualText );
}
For YouTube Video Click on: What is the difference between Assert and Verify in Selenium