CS/IT Turotials

Handling Assertions in Selenium WebDriver with TestNG

Google+ Pinterest LinkedIn Tumblr

In terms of software testing, assertion means the validating or verifying any kind of test. Assertions allow you to verify or validate the test based on the expected or actual results. It will compare the two values expected value and actual value and returns the Boolean value true or false based on the comparison. The test will be passed or failed only when the assertion returns the value without throwing any exceptions.TestNG framework in selenium WebDriver provides a convenient way of handling assertions. In this tutorial, we will discuss handling assertions in selenium WebDriver with TestNG.

If you have just landed on this article and reading this website the first time, may need to know more about TestNG framework, recommends you to read the post about installing TestNG in Eclipse for Selenium WebDriver.

The different types of assertions that can be used with TestNG framework in selenium WebDriver are as follows.

Assert Equals

Assert equals compares the expected and actual conditions based on the test results. Following are the different syntax of using assertEquals statement in TestNG.

assert.assertEquals(string actual, string expected);
assert.assertEquals(string actual, string expected, string message);
assert.assertEquals(boolean actual, boolean expected);

The first statement accepts two string arguments as the actual and expected value which will check whether the actual and expected values are equal or not. Similarly, the second statement accepts three string arguments which throw a message that we provide. The last statement accepts boolean values for both the actual and expected values.

Following is an example of the uses of assert equals which verifies the title of the page.

package testngpackage;
import org.testng.annotations.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;

public class TestNGTest {
	public String testurl="https://www.siteforinfotech.com/";
	String driverPath="E:\\geckodriver.exe";
	public WebDriver driver;
  @BeforeTest
  public void beforeTest() {
	System.out.println("Browser was opening now");  
	System.setProperty("webdriver.gecko.driver", driverPath);
	driver= new FirefoxDriver();
	driver.get(testurl);
  }
  @Test
  public void PrintTitle() {
	String  Title=driver.getTitle();
	System.out.println(Title); 
  }
  @Test
 public void VerifyTitle() {
	 String ExpectedTitle="InfoTechSite | CS/IT Tutorials, MCQs, Guides And Reviews";
	 String ActualTitle=driver.getTitle();
	 Assert.assertEquals(ActualTitle, ExpectedTitle);
 }
  @AfterTest
  public void afterTest() {
	  driver.quit();  
  }
}

Assert Not Equal

The assertion command “Assert Not Equal” used for negation testing by the testers. The test script will passes if the actual and expected result, no matches. Here is the sample code which shows the uses of assert.NotEquals command for verifying the title of the page.

@Test
public void VerifyTitle() {
String ExpectedTitle="InfoTechSite";
String ActualTitle=driver.getTitle();
Assert.assertNotEquals(ActualTitle, ExpectedTitle);

Assert True

The assert true assertion verifies whether the given statement returns Boolean value True and passes the test step. The assertTrue method accepts two types of syntax, which are as follows.

assert.assertTrue(<condition>);
assert.assertTrue(<condition>, message);

In the following example, the user validates whether the user agreement checkbox already checked and click on the submit button.

package testngpackage;
import org.testng.Assert;
import org.testng.annotations.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestNG2 {
public String testurl="http://localhost/test/sample-register.html";
String driverPath="E:\\geckodriver.exe";
public WebDriver driver;
@BeforeTest
public void beforeTest() {
System.out.println("Browser was opening now");  
System.setProperty("webdriver.gecko.driver", driverPath);
driver= new FirefoxDriver();
driver.get(testurl);
  }
@Test
public void submitform() {
WebElement agreeChk=driver.findElement(By.id("agree"));
agreeChk.click();
Assert.assertTrue(agreeChk.isSelected());
driver.findElement(By.name("submit")).click();
 }
@AfterTest
public void afterTest() {
driver.quit();  
  }
}

Assert False

It was the negation command of the assert True command. When the test step returned the Boolean value “False”, assert.AssertFalse() method will pass the test step. The assertFalse method will also accept two types of syntax, which are as follows.

assert.assertFalse(<condition>);
assert.assertFalse(<condition>, message);

Following is a sample program code that shows the uses of “Assert False” command for validating whether the check box selected or not.

@Test
public void submitform() {
WebElement agreeChk=driver.findElement(By.id("agree"));
Assert.assertFalse(agreeChk.isSelected());
agreeChk.click();
driver.findElement(By.name("submit")).click();
}

Assert Null

The assert null assertion will verify the test and passes the object under test is null. The following example illustrates that the user verifies whether the email field has “disabled” attribute. It will return true since there is no attribute named “disabled” on the email field.

package testngpackage;
import org.testng.Assert;
import org.testng.annotations.*;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestNG3 {
public String testurl="http://localhost/test/sample-register.html";
String driverPath="E:\\geckodriver.exe";
public WebDriver driver;
@BeforeTest
public void beforeTest() {
System.out.println("Browser was opening now");  
System.setProperty("webdriver.gecko.driver", driverPath);
driver= new FirefoxDriver();
driver.get(testurl);
  }
@Test
public void enabled() {
WebElement Email=driver.findElement(By.id("email"));Assert.assertNull(Email.getAttribute("disabled"));
System.out.println("Email was enabled");
 }
@AfterTest
public void afterTest() {
driver.quit();  
  }
}

Assert Not Null

As the name suggested that the “Assert Not Null” will verify whether the HTML element under the test is not null and passes the test result if it was not null. Following is an example which verifies whether the address field uses “disabled” attribute. It will return true since there is an attribute named “disabled” on address field.

@Test
public void disabled() {
WebElement Address=driver.findElement(By.id("address"));Assert.assertNotNull(Address.getAttribute("disabled"));
System.out.println("Address was disabled");
 }

Shuseel Baral is a web programmer and the founder of InfoTechSite has over 5 years of experience in software development, internet, SEO, blogging and marketing digital products and services is passionate about exceeding your expectations.

Write A Comment