Assert and Verify Methods in Selenium

Understand the difference between assert and verify in Selenium with command examples to execute types of Assert methods.

Get Started free
Guide Banner Image
Home Guide Assert and Verify Methods in Selenium

Assert and Verify Methods in Selenium

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

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.
  • Tests will continue to run in case of verification 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.

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. Asserts in Selenium validate the automated test cases that help testers understand if tests have passed or failed.

Types of Assertions

  • Hard Assertions
  • Soft Assertions (Verify Method)

Hard Asserts vs Soft Asserts

Hard AssertionsSoft Assertions
Test Execution will be aborted if the assert condition is not metTest execution will continue till the end of the test case even if the 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, the tester has to invoke assertAll()

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, 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.

assertsEqual - Assert vs Verify in Selenium

Below is an example of assertEquals().

assertsEqual - Assert vs Verify 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);
}
}

Code Line-14 to 17: It verifies the website’s title 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), it will throw the “org.junit.ComparisonFailure” exception.

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. The test case is marked as passed if actual and expected results are not the same.

assertNotEquals() - Assert vs verify in Selenium

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);
}
}

Code Line-14 to 17: It verifies the website’s title 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 not to 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.

assertTrue - Assert vs Verify Methods in Selenium

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);
}
}

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 the opposite of assertTrue(). The Assertion verifies the Boolean value returned by the condition. If the Boolean value is false, 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);
}
}

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, it will throw the “java.lang.AssertionError” error.

Code Tip: Want to run a quick test of the above code on our cloud infrastructure?

Try BrowserStack

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);
}
}

Code Line-13 to 15: The assertNull() method verifies if the title of the page www.browserstack.com is null or empty. 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 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);
}
}

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);
}
}

Run Selenium Tests on Cloud for Free

Soft Assertions

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: Navigate to www.browserstack.com to get the title into a String variable of the website and 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  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(); 
}
}

Closing Notes

  • 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 expected.
  • They can also save teams the trouble of running tests that don’t need to be run if a condition is not met.

It is recommended to run tests on real devices for better accuracy as it takes real user conditions into account. BrowserStack gives you access to 3000+ real devices and browsers to test on. It allows you to run selenium browser automation tests on multiple browser-device combinations simultaneously using parallel testing on its Cloud Selenium Grid.

Try BrowserStack Now

Tags
Automation Testing Selenium Webdriver