Guide To TestNG Assertions in Selenium Based Test Automation

What are Assertions in TestNG?

Assert.methodName(actual, expected);
  • Assert: This is the class inbuilt in the TestNG framework
  • methodName: This is the name of the Assert class method
  • actual: This is the first parameter of the assert method in which the value is passed that the user gets from the application under test
  • expected: This is the second parameter of the assert method in which the user passes the expected value
  1. Open the login page
  2. Enter username and password
  3. Click submit
  4. Assert the title of the landing page after logging into the system

Types of Assertions in TestNG

  1. Hard Assertion — Whenever a hard assertion is applied and an assertion statement fails, the assertion in testng throws an exception immediately and terminates the further execution of the same test case, and simply continues with the execution of the next test case in the test suite. As soon as the hard assertion condition fails, the test case is marked as failed.
import java.util.concurrent.TimeUnit;
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;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;
public class TestLogin {WebDriver driver; @BeforeTest
public void setup() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.pcloudy.com/");
}
@Test(priority=0)
public void testPCloudyLogin(){
WebElement loginHeader = driver.findElement(By.xpath("//a[text()='Login']"));
loginHeader.click();

WebElement username = driver.findElement(By.id("userId"));
username.sendKeys("ramit.dhamija@gmail.com");
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("ramit9876");
WebElement loginButton = driver.findElement(By.id("loginSubmitBtn"));
loginButton.click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

String expectedTitle = "Mobile App Testing, Continuous Testing Cloud, Mobile Testing Tools | pCloudy";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle,expectedTitle, "pCloudy Login Test Failed");
}

@AfterTest
public void tearDown() {

if(driver!=null)
{
driver.quit();
}
}
}
  1. Soft Assertions — These are opposite to hard assertions in which testng continues to the next step of the test case even if the assertion condition fails.
import java.util.concurrent.TimeUnit;import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
import io.github.bonigarcia.wdm.WebDriverManager;public class TestLogin {WebDriver driver;
SoftAssert softassert;
@BeforeTest
public void setup() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
softassert = new SoftAssert();
driver.manage().window().maximize();
driver.get("https://www.pcloudy.com/");
}
@Test(priority=0)
public void testPCloudyLogin(){
WebElement loginHeader = driver.findElement(By.xpath("//a[text()='Login']"));
loginHeader.click();

WebElement username = driver.findElement(By.id("userId"));
username.sendKeys("ramit.dhamija@gmail.com");
WebElement password = driver.findElement(By.name("password"));
password.sendKeys("ramit9876");
WebElement loginButton = driver.findElement(By.id("loginSubmitBtn"));
loginButton.click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

String expectedTitle = "Mobile App Testing, Continuous Testing Cloud, Mobile Testing Tools | pCloudy";
String actualTitle = driver.getTitle();
softassert.assertEquals(actualTitle,expectedTitle, "pCloudy Login Test Failed");
System.out.println("Soft Assertion statement is executed");

softassert.assertAll();
}

@AfterTest
public void tearDown() {

if(driver!=null)
{
driver.quit();
}
}
}

When to use Hard and Soft Assertion?

TestNG Assert Methods

  1. Assert.assertEqual(String actual, String expected): This assertion method accepts two parameters i.e. the actual value and expected value to validate if the actual string is equal to the expected string or not. The assertion exception is thrown if both the strings are not equal.
  2. Assert.assertEqual(String actual, String expected, String message): This assertion method is similar to the assertion method discussed above, the only difference is that this method can accept one more string parameter as a message. In case the assertion condition is not met, the assertion error is thrown along with a message passed here.
  3. Assert.assertEquals(boolean actual, boolean expected): This assertion method accepts two boolean values and validates if both are equal or not.
  4. Assert.assertTrue(condition): This assertion method is used to assert whether the condition passed in a parameter returns true or not. If the condition returns false, the assertion error is thrown.
  5. Assert.assertTrue(condition, message): This assertion method is similar to the assertion method discussed in the previous one, the only difference is that this method can accept one more string parameter as a message. In case the assertion condition is passed as false, the assertion error is thrown along with a message passed here.
  6. Assert.assertFalse(condition): This assertion method is used to assert whether the condition passed in a parameter returns false or not. If the condition returns true, the assertion error is thrown.
  7. Assert.assertFalse(condition, message): This assertion method is similar to the assertion method discussed in the previous one, the only difference is that this method can accept one more string parameter as a message. In case the assertion condition is passed as true, the assertion error is thrown along with a message passed here.
  8. Assert.assertNull(condition): This assertion method is used to assert whether the condition passed in a parameter returns null or not. If the condition doesn’t return null, the assertion error is thrown.
  9. Assert.assertNotNull(condition): This assertion method is used to assert whether the condition passed in a parameter returns value except null or not. If the condition returns null, the assertion error is thrown.

Conclusion

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
pCloudy

pCloudy

pCloudy is the most powerful cloud-based App Testing Platform. Brand Marketing @ pCloudy (www.pcloudy.com)