The post Implementing Assert and Verify logic in Selenium WebDriver appeared first on Software QA, Functional, Automation & Load Testing with Arif.
]]>In many test automation scenarios, before or after the test step we need to check if the specific text is displaying or not. We donot our test scripts to stop irrespective the result (text found or not). To achieve this objective, we have various approaches that can be used. In below I have shared some of the java code examples that can be used to implement this strategy.
if(driver.getPageSource().contains("Text - Testing with Arif")) { System.out.println("Text is Present"); } else { System.out.println("Text is not Present"); }
OR
try { assertTrue(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*verify text is present[\\s\\S]*$")); } catch (Error e) { verificationErrors.append(e.toString()); }
OR
driver.findElement(By.xpath("//span[contains(.,'Transaction was added successfully')]")); System.out.println("Transaction successful");
OR
try { assertEquals("VerifyText in Element", driver.findElement(By.cssSelector("div.bbMargin")).getText()); } catch (Error e) { verificationErrors.append(e.toString()); }
Similarly, in testing scenarios where we want our scripts to stop if certain assertion fails, we can use assert methods from Junit, TestNG (and other alternative frameworks) to implement the assertion strategy. In below are some examples of assertions on text.
assertTrue(driver.getPageSource().contains("1,500.00")); System.out.println("1500.00 found in page source");
OR
assertTrue(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*assert text is present[\\s\\S]*$"));
OR
assertEquals("1,500.00", driver.findElement(By.cssSelector("div.eoh.")).getText(); System.out.println("1500 found written in div");
OR
assertTrue(driver.findElement(By.cssSelector("BODY")).getText().matches("^[\\s\\S]*Record found successfully[\\s\\S]*$"));
OR
assertTrue(findElement(By.id("myElement")).getText().equals("foo");
Selenium WebDriver has some built-in methods that can be used with TestNG or Junit methods to implement Element verification logic. Few code examples are given below.
!driver.findElements(By.id("xyz")).isEmpty();
OR
if(isElementPresent(By.linkText("Submit"))) { System.out.println("SUBMIT Link/Button found"); } else { System.out.println("SUBMIT Link/Button not found"); }
OR
try { assertTrue(isElementPresent(By.cssSelector("div.bbMargin"))); } catch (Error e) { verificationErrors.append(e.toString()); }
IsElementPresent method from Selenium WebDriver API can be used together with AssertTrue method to implement Element assertion logic as demonstrated in code below.
assertTrue(isElementPresent(By.cssSelector("div.bbMargin")));
OR
Alternatively, If you want to assert Element Not Present, then you can use following example, which verifies that there are NO matching elements present in DOM and returns the value of zero. So when zero value is returned, assertion will pass. On other hand if there is any matching element present then zero will not be returned and assertion will fail.
Assert.assertEquals(0, wd.findElements(By.locator("locator")).size());
End Note: These are just few of the verification/assertion methods. There are many other approaches as well to do it. Have you ever used these in your test automation scripts? Or would like to recommend a better approach to our readers! Please share your thoughts and experiences in comments section.
The post Implementing Assert and Verify logic in Selenium WebDriver appeared first on Software QA, Functional, Automation & Load Testing with Arif.
]]>