Assert and Verify in Selenium:
- Both assert and Verify in Selenium are used to find out if the given input on the web page exists or not.
Two things we Assert / Verify using Selenium:
- Verify / Assert Text Present in Web Page.
- Verify / Assert Element Present in Web Page.
Verify Text Present :
- Before or after the testing phase, we need to see if specific text is displayed or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | if(driver.getPageSource().contains("stqatools")) { System.out.println("Name is Present"); } else { System.out.println("Name is not Present"); } |
Assert Text Present :
- We need to stop scripts to stop if certain assertion fails.
1 | assertTrue(driver.getPageSource().contains("stqatools")); |
Verify Element Present :
- Verify element is present or not in Web Page using in-Built methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | !driver.findElements(By.id("id")).isEmpty(); // OR if(isElementPresent(By.linkText("Submit_Button"))) { System.out.println("Submit_Button Link/Button found"); } else { System.out.println("Submit_Button Link/Button not found"); } |
Assert Element Present :
- Assert element is present or not, If not present then then Halt process.
1 | assertTrue(isElementPresent(By.cssSelector("div.stqatools"))); |
Assert:
- When the “assert” command fails, the test execution will be canceled. So when the Assertion fails, then all test steps are skipped / omitted after that line of code.
- The solution to overcome this issue is to use a try-catch block.
- We use try-catch in the block.
- Mostly, the assurance order is used when the final result of the check value should be passed to continue in the next step.
- In simple terms, if the assert condition is true then the program control will execute the next testing phase, but if the situation is wrong, the execution will stop and further testing phase will not be executed.
- Failure of verification The execution of that particular test method is stopped and the test method has been marked as failed.
- When an “assert” fails, the test will be aborted / stopped.
Example: Verify string using assertTrue.
1 2 3 | String str = driver.findElement(By.xpath("xpath")).getText(); assertTrue(actualString.contains("specific text")); |
Example of Assert:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import static org.testng.Assert.assertEquals; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; public class Assert_Example { public static void main(String args[]) { String Actual = "Actual"; String Expected = "Expected"; // Assertion Passing Assert.assertTrue(Actual == Actual); System.out.println("Passing 1"); // Assertion failing Assert.fail("Failing 2"); System.out.println("Failing 2"); // Check Assert Equals assertEquals(Actual, Expected); } } |
Verify:
- When the “Verify” command fails, then execution will continue and logging the failure.
- Mostly, the verification command is used to view non-critical things.
- In cases where we move forward, though the end result of the check value has failed.
- In simple words, there will be no interruption in test execution, even if the verification status is true or false.
- Verify has been verified using the SoftAssert class.
Example of Verify:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import org.testng.asserts.SoftAssert; public class Verify_Example { public static void main(String args[]) { // Creating softAssert object SoftAssert softAssert = new SoftAssert(); // Assertion failing softAssert.fail("Failing first assertion"); System.out.println("Failing 1"); } } |
Output:
1 | Failing 1 |
What Is The Difference Between Assert And Verify In Selenium and Selenium Assert and Verify in selenium WebDriver and verify and assert methods in selenium