Assertions in Selenium

Assertions are used to compare the actual and expected results. It is an important part of test automation since it helps in the early identification of defects, saving time and effort. Assertions are used in Selenium to validate web components such as text, dropdowns, checkboxes, and so on on a web page.

In this blog, we will discuss all about assertions in Selenium using Java.

What are Assertions in Selenium?

In test automation, assertions are used to compare the actual result with the expected result. It is an important part of automation testing because it helps us in the early identification of defects, saving time and effort. Assertions also help us in determining if the test cases passed or failed.

Types of Assertions in Selenium?

There are two types of assertions available in Selenium, that includes:

I. Hard Assertion

Hard Assertion is a type of assertion that throws an error immediately when the actual result does not match the expected result. This means that if a hard assertion fails, the test case will fail, and the execution will stop.

In Selenium, assertEquals is the method of Hard Assertion. It is used to compare two values and check if they are equal. If the values are not equal, the test case will fail.
Syntax:

Assert.assertEquals(ActualResult, ExpectedResult);

Programmatic Demonstration of Hard Assertion using TestNG Framework in Selenium

import org.openqa.selenium.WebDriver;
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;

public class HardAssertionExample {
	WebDriver driver;

	@BeforeTest
	public void setUp() {
		driver = new ChromeDriver();
		driver.manage().window().maximize();
		// Navigate to the website
		driver.get("https://testingmint.com");
	}

	@Test
	public void testTitle() {
		String expectedTitle = "Testingmint: One Stop Solution for Sofware Tester";
		String actualTitle = driver.getTitle();
		Assert.assertEquals(actualTitle, expectedTitle);
	}

	@AfterTest
	public void closeBrowser() {
		driver.quit();
	}
}

Output:

hardassertion - testingmint.com

II. Soft Assertion

Soft Assertion is a type of assertion that does not throw an error immediately when the actual result does not match the expected result. Instead, it collects all the errors and reports them at the end of the test execution.

In Selenium, assertAll is the method of Soft Assertion which is called at the end of all the assertions. It is used to check all the assertions added in the script and report all the errors at the end of the test execution. This means that even if one assertion fails, the test case will continue to execute, and all the errors will be reported at the end.
Syntax:

softAssert.assertAll();

The assertAll method is used in combination with the assertTrue, assertFalse, assertNotNull, assertNull, assertSame, assertNotSame, and assertArrayEquals assertion methods.
Syntax:

softAssert.assertTrue(condition);
softAssert.assertFalse(condition);
softAssert.assertEquals(actual, expected);
softAssert.assertNotEquals(actual, expected);
softAssert.assertNull(object);
softAssert.assertNotNull(object);
softAssert.assertSame(actual, expected);
softAssert.assertNotSame(actual, expected);
softAssert.assertArrayEquals(actual, expected);

Soft assertions are useful in scenarios where we want to verify multiple conditions and report all the errors together. It is also useful when we want to execute all the test cases and report all the errors at the end, instead of stopping the execution after the first error.

Programmatic Demonstration of Soft Assertion using TestNG Framework in Selenium

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;

public class SoftAssertionExample {
	WebDriver driver;
	SoftAssert softAssert;

	@BeforeTest
	public void setUp() {
		driver = new ChromeDriver();
		softAssert = new SoftAssert();
       // Navigate to the website
		driver.get("https://testingmint.com");
	}

	@Test
	public void testSoftAssertion() {

		// Find the elements and perform assertions
		String ExpectedResult = "Software Testing Blog";
		String BannerText = driver.findElement(By.tagName("h1")).getText();
		softAssert.assertEquals(BannerText, ExpectedResult);

		WebElement paragraph = driver.findElement(By.tagName("p"));
		softAssert.assertTrue(paragraph.getText().contains("Not Found"));

		WebElement link = driver.findElement(By.linkText("Automation Testing"));
		softAssert.assertTrue(link.isDisplayed());

		// End the test with Soft Assertion
		softAssert.assertAll();
	}

	@AfterTest
	public void closeBrowser() {
		// Close the browser
		driver.quit();
	}
}

Output:

soft assertion - testingmint.com

Importance of Assertion in Selenium

Following are some of the key reasons why assertions are important in Selenium:

  1. Helps to detect defects early:
    Assertion helps in the detection of defects early in the testing cycle. It helps in the early identification of issues, saving time and resources.
  2. Improves the reliability of tests:
    Assertions are used to verify that a web page or application is performing correctly. This improves test reliability by assuring that the tests produce consistent results each time they are run.
  3. Reduces maintenance costs:
    Assertion decreases the expense of maintaining test scripts. By confirming that the web page or application is functioning as intended, it reduces the need for manual testing and the time necessary to maintain the test scripts.
  4. Increases test coverage:
    Assertion improves test coverage by validating various parts of a web page or application, such as text, images, links, forms, and so on. This helps to ensure that all of the web page’s or application’s important functions are tested.
  5. Provides better feedback:
    Assertion improves feedback on test results by highlighting problems with the web page or application. This helps identify problematic areas and makes it easier to resolve them.

Conclusion

In Conclusion, assertions are an important part of automated testing in Selenium. They help in validating a web page’s or application’s expected behavior and detect issues early in the testing cycle. We can improve the reliability of our tests, lower maintenance costs, and increase test coverage by using assertions. Soft Assertion is especially useful when we wanted to reveal all defects at the end of the test run without ending the test immediately.

We can find errors earlier and verify that our web pages or applications are functioning correctly by including assertions in our test scripts. This can save time and money while also providing us with more accurate feedback on the quality of our code. Ultimately, it is essential that we include assertions in our test scripts to guarantee that our apps function properly and that our tests generate correct and reliable results.

You Might Like: