Home Guide Assert and Verify Methods in Selenium

Assert and Verify Methods in Selenium

By Chaitanya Pujari, Community Contributor -

Asserts and Verify methods are commonly used in Selenium for verifying or validating applications. In this tutorial, we will learn the why, when and how of using these methods to make Selenium tests more efficient.

Assertions (also known as Asserts)

The word Assert means to state a fact or belief confidently or forcefully. In Selenium, Asserts are validations or checkpoints for an application. Assertions state confidently that application behavior is working as expected. One can say that Asserts in Selenium are used to validate the test cases. They help testers understand if tests have passed or failed.

Types of Assertions

  • Hard Assertions
  • Soft Assertions (Verify Method)

Hard Assertions

Hard Assertions are ones in which test execution is aborted if the test does not meet the assertion condition. The test case is marked as failed. In case of an assertion error, it will throw the “java.lang.AssertionError” exception.

Let’s explore different types of hard assertions with examples.

  • assertEquals() is a method that takes a minimum of 2 arguments and compares actual results with expected results. If both match, then the assertion is passed and the test case is marked as passed. assertEquals() can compare Strings, Integers, Doubles and many more variables, as shown in the image below.

Asserts in Selenium

Below is an example of assertEquals().

Assert methods in Selenium

Code Snippet for assertEquals() in Selenium

package com.tests;
import org.junit.Assert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class BrowserStackTutorials {
@Test
public void testAssertFunctions() {
System.setProperty("webdriver.chrome.driver", "C:\\I2EWebsiteTest\\Driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.navigate().to("https://www.browserstack.com/");
String ActualTitle = driver.getTitle();
String ExpectedTitle = "Most Reliable App & Cross Browser Testing Platform | BrowserStack";
Assert.assertEquals(ExpectedTitle, ActualTitle);
}
}

Explanation of Code

Code Line-14 to 17: It verifies the title of the website by navigating to the website and getting the actual website title. Then it is matched with the expected title. If the assertion condition is not met (the titles matching) then it will throw the “org.junit.ComparisonFailure” exception.


How to run Selenium tests on Chrome using ChromeDriver: Tutorial


  • assertNotEquals() is a method that does the opposite of the assertEquals() method. In this case, the method compares the actual and expected result. But if the assertion condition is met if the two are not identical. If actual and expected results are not the same, the test case is marked as passed.

Asserts in Selenium Example

Code Snippet For assertNotEquals() in Selenium

package com.tests;
import org.junit.Assert;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class BrowserStackTutorials {
@Test
public void testAssertFunctions() {
System.setProperty("webdriver.chrome.driver", "C:\\I2EWebsiteTest\\Driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.navigate().to("https://www.browserstack.com/");
String ActualTitle = driver.getTitle();
String ExpectedTitle = "Most Reliable App & Cross Browser Testing Platform | browserstack";
Assert.assertNotEquals(ActualTitle, ExpectedTitle);
}
}

Explanation of Code

Code Line-14 to 17: It verifies the title of the website by navigating to the website and getting the actual website title. Then it compares it with the expected title. The assertNotEquals() method expects the result to not be identical. If the assertion condition is not met then it will throw “org.junit.ComparisonFailure” exception.

  • assertTrue(): This Assertion verifies the Boolean value returned by the condition. If the Boolean value is true, then the assertion passes the test case.

Code Snippet For assertTrue() in Selenium

package com.tests;
import static org.testng.Assert.assertTrue;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class BrowserStackTutorials {
@Test
public void testAssertFunctions() {
System.setProperty("webdriver.chrome.driver", "C:\\I2EWebsiteTest\\Driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.navigate().to("https://www.browserstack.com/");
Boolean verifyTitle = driver.getTitle().equalsIgnoreCase("Most Reliable App & Cross Browser Testing Platform | BrowserStack");
assertTrue(verifyTitle);
}
}

Explanation of Code

Code Line-14 to 16: It verifies the title from www.browserstack.com and the assertTrue() Method will verify if the Boolean condition is set to True.

  • assertFalse(): This method works opposite of that of assertTrue(). The Assertion verifies the Boolean value returned by the condition. If the Boolean value is false, then the assertion passes the test case.

Assert method example in selenium

Code Snippet For assertFalse() in Selenium

package com.tests;
import static org.testng.Assert.assertFalse;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class BrowserStackTutorials {
@Test
public void testAssertFunctions() {
System.setProperty("webdriver.chrome.driver", "C:\\I2EWebsiteTest\\Driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.navigate().to("https://www.browserstack.com/");
Boolean verifyTitle = driver.getTitle().equalsIgnoreCase("Most Reliable App & Cross Browser Testing Platform");
assertFalse(verifyTitle);
}
}

Explanation of Code

Code Line-14 to 17: It retrieves the title from www.browserstack.com and the assertFalse() Method will verify the Boolean condition. If the Boolean value is “False” the condition is met and the test is marked as passed. If the condition is not met then it will throw the “java.lang.AssertionError” error.

  • assertNull(): This method verifies if the expected output is null. If not, the value returned is false.

Assert in Selenium Example

Code Snippet For assertNull() in Selenium

package com.tests;
import static org.testng.Assert.assertNull;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class BrowserStackTutorials {
@Test
public void testAssertFunctions() {
System.setProperty("webdriver.chrome.driver", "C:\\I2EWebsiteTest\\Driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.navigate().to("https://www.browserstack.com/");
String verifyAssertNull = null;
assertNull(verifyAssertNull);
}
}

Explanation of Code

Code Line-13 to 15: The assertNull() method verifies if the title of the page www.browserstack.com is null or empty from. If the condition is not met then it will throw “java.lang.AssertionError” error as shown in the example.

  • assertNotNull(): This method works opposite to that of the assertNull() method. The assertion condition is met when the method validates the expected output to be not null.

Code Snippet For assertNotNull() in Selenium

package com.tests;
import static org.testng.Assert.assertNotNull;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class BrowserStackTutorials {
// Author: Chaitanya Pujari
@Test
public void testAssertFunctions() {
System.setProperty("webdriver.chrome.driver", "C:\\I2EWebsiteTest\\Driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.navigate().to("https://www.browserstack.com/");
Boolean verifyTitle = driver.getTitle().equalsIgnoreCase("Most Reliable App & Cross Browser Testing Platform");
assertNotNull(verifyTitle);
}
}

Explanation of Code

Code Line-14 to 16: The assertNotNull() method verifies if the title of the page www.browserstack.com is null or empty.

Example of Hard Assert in Selenium

package com.tests;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

public class BrowserStackTutorials {
@Test
public void testAssertFunctions() {
System.setProperty("webdriver.chrome.driver", "C:\\I2EWebsiteTest\\Driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.navigate().to("https://www.browserstack.com/");
String ActualTitle = driver.getTitle();
String verifyAssertNull=null;
String ExpectedTitle = "Most Reliable App & Cross Browser Testing Platform | BrowserStack";
Boolean verifyTitleIsPresent=driver.getTitle().equalsIgnoreCase("Most Reliable App & Cross Browser Testing Platform | BrowserStack");
Boolean verifyTitleIsChanged=driver.getTitle().equalsIgnoreCase("Testing Platform | BrowserStack");
assertEquals(ExpectedTitle, ActualTitle);
assertNotEquals(ExpectedTitle, "browserstack");
assertTrue(verifyTitleIsPresent);
assertFalse(verifyTitleIsChanged);
assertNotNull(verifyTitleIsPresent);
assertNull(verifyAssertNull);
}
}

Verify in Selenium (also known as Soft Assertion)

In a hard assertion, when the assertion fails, it terminates or aborts the test. If the tester does not want to terminate the script they cannot use hard assertions. To overcome this, one can use soft assertions.

Let’s explore the different types of soft assertions with examples (verify).

Soft Assertion Example in Selenium

Explanation of Code

Code Line-19 to 20: Navigating to www.browserstack.com to get the title into a String variable of the website and to verify the title of the website using a Boolean variable.

  • The code below verifies the title of the website. It compares actual and expected results. If both are the same, the assertion is passed and the test case is marked as passed.

Code Line-21:  softAssert.assertEquals(getActualTitle, “Most Reliable App & Cross Browser Testing Platform | BrowserStack”);

  • The code below verifies the title of the website. It compares actual and expected results. If both are the same, the assertion is passed and the test case is marked as passed.

Code Line-22: softAssert.assertNotEquals(getActualTitle, “Most Reliable App & Cross Browser Testing Platform | BrowserStack”);

  • The code below uses assertNull() to validate if the expected output is null

Code Line-23: softAssert.assertNull(verifyTitle);

  • The code below uses assertNotNull() to validate if the expected output is not null

Code Line-24: softAssert.assertNotNull(verifyTitle);

  • The code below verifies the Boolean value returned by the condition. If the Boolean value is true, then the assertion passes the test case

Code Line-25: softAssert.assertTrue(“BrowserStack”.equals(“Browserstack”), “First soft assert failed”);

  • The code below verifies the Boolean value returned by the condition. If the Boolean value is false, then the assertion passes the test case

Code Line-26: softAssert.assertFalse(“BrowserStack”.equals(“BrowserStack”), “Second soft assert failed”);

  • The code below uses assertAll() to see assertion results at the end of the test

Code Line-27: softAssert.assertAll();

Example of Soft Assert in Selenium (or Verify in Selenium)

package com.tests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class BrowserStackTutorials {
@Test
public void softAssert() {
System.setProperty("webdriver.chrome.driver", "C:\\I2EWebsiteTest\\Driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
SoftAssert softAssert = new SoftAssert();
driver.navigate().to("https://www.browserstack.com/");
String getActualTitle = driver.getTitle();
Boolean verifyTitle = driver.getTitle().equalsIgnoreCase("Most Reliable App & Cross Browser Testing Platform | BrowserStack");
softAssert.assertEquals(getActualTitle, "Most Reliable App & Cross Browser Testing Platform | BrowserStack");
softAssert.assertNotEquals(getActualTitle, "Most Reliable App & Cross Browser Testing Platform | BrowserStack");
softAssert.assertNull(verifyTitle);
softAssert.assertNotNull(verifyTitle);
softAssert.assertTrue("BrowserStack".equals("Browserstack"), "First soft assert failed");
softAssert.assertFalse("BrowserStack".equals("BrowserStack"), "Second soft assert failed");
softAssert.assertAll(); 
}
}

Hard vs Soft Asserts in Selenium

Hard AssertionsSoft Assertions
Test Execution will be aborted if assert condition is not metTest execution will continue till the end of the test case even if assert condition is not met
Does not have to invoke any methods to capture the assertionsTo view assertions result at the end of the test, tester has to invoke assertAll()

Difference between Assert and Verify in selenium

  • In the case of assertions, if the assert condition is not met, test case execution will be aborted. The remaining tests are skipped, and the test case is marked as failed. These assertions are used as checkpoints for testing or validating business-critical transactions.
  • In case of verify, tests will continue to run until the last test is executed even if assert conditions are not met. Verify or Soft Asserts will report the errors at the end of the test. Simply put, tests will not be aborted if any condition is not met. Testers need to invoke the assertAll() method to view the results.

Both Hard and Soft Assertions are very important for designing and running Selenium webdriver tests. They are instrumental in verifying application behavior at critical stages. By using assertions, testing teams can determine if an application is working as it is expected to. They can also save teams the trouble of running tests that don’t need to be run if a condition is not met.

BrowserStack Logo Run Selenium Tests on 2000+ Browsers & Devices Get Started Free