Selenium WebDriver – Software QA, Functional, Automation & Load Testing with Arif TesterLogic Thu, 13 Sep 2018 04:58:23 +0000 en-US hourly 1 https://wordpress.org/?v=4.9.12 https://i1.wp.com/www.testerlogic.com/wp-content/uploads/2014/09/puzzle-logic-mini-logo-v2.png?fit=32,30 Selenium WebDriver – Software QA, Functional, Automation & Load Testing with Arif 32 32 Implementing Assert and Verify logic in Selenium WebDriver /assert-verify-logic-selenium-webdriver/ /assert-verify-logic-selenium-webdriver/#comments Mon, 08 Sep 2014 19:24:17 +0000 /?p=901 One of the actions that we always need to perform while doing test automation is to apply different assertions or verification to check results of automated tests. Selenium IDE has built in VerifyTextPresent, AssertElementPresent commands. But how we do it in Selenium WebDriver? There are definitely many ways to do it in Slenium WebDriver with support of […]

The post Implementing Assert and Verify logic in Selenium WebDriver appeared first on Software QA, Functional, Automation & Load Testing with Arif.

]]>
One of the actions that we always need to perform while doing test automation is to apply different assertions or verification to check results of automated tests. Selenium IDE has built in VerifyTextPresent, AssertElementPresent commands. But how we do it in Selenium WebDriver? There are definitely many ways to do it in Slenium WebDriver with support of testing frameworks like JUnit or TestNG etc. In this blog, I will discuss few of those approaches that can be used with Selenium WebDriver to..

  1. Verify/Assert Text Present
  2. Verify/Assert Element Present

1.1 Verify Text Present

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());
}

1.2 Assert Text Present

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");


2.1 Verify Element Present

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());
}

2.2 Assert Element Present

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.

]]>
/assert-verify-logic-selenium-webdriver/feed/ 1