CS/IT Tutorials Collections

6 Best Ways of Handling Assertions in Selenium WebDriver with TestNG

Pinterest LinkedIn Tumblr
In terms of software testing, assertion means validating or verifying test results. WebDriver provides a convenient way of handling assertions. 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 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 for the first time, may need to know more about the TestNG framework, recommends you read the post about installing TestNG in Eclipse for Selenium WebDriver.

Handling Assertions in Selenium WebDriver

The different types of assertions that can be used with the TestNG framework for handling assertions 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 syntaxes of using assertEquals statements 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 that 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 use 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://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” is used for negation testing by the testers. The test script will passes if the actual and expected result, not 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 the 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 is already checked and clicks 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 the “Assert False” command for validating whether the check box is 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 as null. The following example illustrates that the user verifies whether the email field has a “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 that verifies whether the address field uses the “disabled” attribute. It will return true since there is an attribute named “disabled” on the address field.

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

Author

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

Comments are closed.