Assert and Verify Methods in Selenium
Related Product On This Page Difference between Assert and Verify in SeleniumMay 04, 2026 · 12 min read · Tool Comparison
Selenium is a rich browser mechanisation tool. However, it doesn ’ t have potent statement methods. Assertions are necessary to verify test solution against wait result. So, they are vital for test automation. In, since there is no built-in assert method. The workaround to perform assertion in Selenium is to use a testing framework such as TestNG, JUnit or Python ’ s unittest. Difference between Hard and Soft Assert in Selenium Difficult Assertions in Selenium If condition fails →trial aborts directly. Soft Assertions (Verify) in Selenium Example Use Cases of Hard and Soft Assert in Selenium In this tutorial, learn the difference between assert and verify and the why, when, and how of using these methods to do Selenium test more efficient. The word Assertagency to state a fact or opinion confidently or forcefully. In Selenium, Asserts are establishment or checkpoints for an application. Assertions state confidently that application behavior is working as expected. Asserts in Selenium validate the that help quizzer realise if tests have legislate or failed. Types of Assertions Difficult Assertions are one in which test execution is aborted if the tryout does not encounter the assertion condition. The test case is marked as failed. In lawsuit of an assertion error, it will throw the “java.lang.AssertionError” exception. Let ’ s explore different types of difficult asseveration with example. assertEquals ()is a method that takes a minimum of 2 controversy and compares actual results with expected result. If both lucifer, the assertion is passed, and the test instance is marked as passed.assertEquals ()can compare Strings, Integers, Doubles, and many more variables, as shown in the ikon below. Below is an example ofassertEquals (). assertEquals (actualTitle, expectedTitle, & # 8220; Page rubric execute not match the expected value & # 8221;);: assertNotEquals ()is a method that does the opponent of theassertEquals ()method. In this cause, the method equate the actual and expected result. But if the assertion condition is met if the two are not identical. The tryout event is marked as passed if actual and expected resultant are not the same. Pro tip: Tools like SUSA can handle this autonomously — upload your app and get results without writing a single test script. Code Snippet for assertNotEquals () in Selenium assertNotEquals (actualTitle, expectedTitle, & # 8220; Titles should not match, but they do & # 8221;);: This Assertion verifies the Boolean value regress by the condition. If the Boolean value is true, then the affirmation passes the test case. Code Snippet for assertTrue () in Selenium assertTrue (verifyTitle, & # 8220; The page rubric is not as expected & # 8221;);: This method works the opposite ofassertTrue (). The Assertion verifies the Boolean value revert by the condition. If the Boolean value is mistaken, the asseveration surpass the exam case. Code Snippet for assertFalse () in Selenium assertFalse (verifyTitle, & # 8220; The rubric fit an incorrect value & # 8221;);: Code Tip: Want to run a quick test of the above code on our cloud infrastructure? This method verifies if the look yield is null. If not, the value returned is mistaken. Code Snippet for assertNull () in Selenium assertNull (verifyAssertNull, & # 8220; Expected value is not null & # 8221;);: This method works opposite to the assertNull () method. The assertion stipulation is met when the method validates the expected output to be not void. Code Snippet for assertNotNull () in Selenium assertNotNull (actualTitle, & # 8220; Page rubric should not be null & # 8221;);: In a hard averment, when the assertion fails, it terminates or aborts the examination. If the quizzer execute not want to terminate the script, they can not use difficult asseveration. To overcome this, one can use soft assertions. Code Snippet for Soft Assertions in Selenium It is possible to enforce soft assertions without relying on library like TestNG or JUnit. The thought hither is to manually becharm assertion results during test execution to account all of them at the end. Now, even after an asseveration fails, you must continue test execution. At the end of the test, control all the asseveration and describe them. Here is how soft assertions can be implemented manually: Example of Manual Soft Assertion Implementation Here, you must manually implement each assertion and effect collection, which increases code complexity. Also, if you forget to check the results at the end, the failed assertions may not stop the tryout. So, review the effect cautiously. It is recommended to run tests on for better accuracy as it takes into report. BrowserStack gives you access to 3000+ existent devices and browsers to test on. It allows you to run examination on multiple browser-device combinations simultaneously employ on its. # Ask-and-Contributeabout this theme with our Discord community. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed. Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.Related Product
Assert and Verify Methods in Selenium
Overview
Difference between Assert and Verify in Selenium
Assertions (also known as Asserts)
Hard Asserts vs Soft Asserts
Hard Assertions Soft Assertions Test Execution will be aborted if the assert condition is not met Test execution will keep till the end of the test case even if the assert condition is not met Does not have to invoke any method to capture the assertions To view assertions result at the end of the test, the tester has to invokeassertAll () Difficult Assertions
package com.tests;
import inactive org.testng.Assert.assertEquals;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
importation org.testng.annotations.Test;
public class BrowserStackTutorials {
@Test
public void testAssertEquals () {
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 & amp; Cross Browser Testing Platform | BrowserStack '';
assertEquals (actualTitle, expectedTitle, `` Page title do not match the expected value '');
driver.quit ();
}
}
assertNotEquals ()
software com.tests;
import unchanging org.testng.Assert.assertNotEquals;
meaning org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public form BrowserStackTutorials {
@Test
public void testAssertNotEquals () {
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 = `` Wrong Title '';
assertNotEquals (actualTitle, expectedTitle, `` Titles should not agree, but they do '');
driver.quit ();
}
}
assertTrue ()
software com.tests;
import static org.testng.Assert.assertTrue;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
signification org.testng.annotations.Test;
public grade BrowserStackTutorials {
@Test
public void testAssertTrue () {
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 Honest App & amp; Cross Browser Testing Platform | BrowserStack '');
assertTrue (verifyTitle, `` The page title is not as expected '');
driver.quit ();
}
}
assertFalse ()
package com.tests;
import stable org.testng.Assert.assertFalse;
import org.openqa.selenium.WebDriver;
signification org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class BrowserStackTutorials {
@Test
public void testAssertFalse () {
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 (`` Incorrect Title '');
assertFalse (verifyTitle, `` The rubric matched an incorrect value '');
driver.quit ();
}
}
assertNull ()
package com.tests;
meaning static org.testng.Assert.assertNull;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public family BrowserStackTutorials {
@Test
public void testAssertNull () {
System.setProperty (`` webdriver.chrome.driver '', `` C: \\I2EWebsiteTest\\Driver\\chromedriver.exe '');
WebDriver driver = new ChromeDriver ();
String verifyAssertNull = null;
assertNull (verifyAssertNull, `` Expected value is not null '');
driver.quit ();
}
}
assertNotNull ()
package com.tests;
meaning electrostatic org.testng.Assert.assertNotNull;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class BrowserStackTutorials {
@Test
public void testAssertNotNull () {
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 ();
assertNotNull (actualTitle, `` Page rubric should not be null '');
driver.quit ();
}
}
Soft Assertions
package com.tests;
import org.openqa.selenium.WebDriver;
signification org.openqa.selenium.chrome.ChromeDriver;
signification org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public grade BrowserStackTutorials {
@Test
public nothingness testSoftAssert () {
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 actualTitle = driver.getTitle ();
softAssert.assertEquals (actualTitle, `` Most Reliable App & amp; Cross Browser Testing Platform | BrowserStack '', `` Title does not twin '');
softAssert.assertNotEquals (actualTitle, `` Incorrect Title '', `` Title matches an incorrect value '');
softAssert.assertNotNull (actualTitle, `` Page title should not be null '');
boolean titleMatch = actualTitle.equalsIgnoreCase (`` Most Reliable App & amp; Cross Browser Testing Platform | BrowserStack '');
softAssert.assertTrue (titleMatch, `` Title does not pair in case-insensitive comparison '');
softAssert.assertAll (); // Reports all assertion results
driver.quit ();
}
}
Implementing Soft Assertions Without a Library
package com.tests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import java.util.ArrayList;
meaning java.util.List;
public category BrowserStackTutorials {
// Custom class to hold assertion result info
static class AssertionResult {
boolean passed;
String substance;
AssertionResult (boolean passed, String message) {
this.passed = legislate;
this.message = message;
}
}
@Test
public void testManualSoftAssert () {
System.setProperty (`` webdriver.chrome.driver '', `` C: \\I2EWebsiteTest\\Driver\\chromedriver.exe '');
WebDriver driver = new ChromeDriver ();
driver.navigate () .to (`` https: //www.browserstack.com/ '');
// List to collect assertion event
List & lt; AssertionResult & gt; assertionResults = new ArrayList & lt; & gt; ();
// Manually execute assertions and cod termination
String actualTitle = driver.getTitle ();
String expectedTitle = `` Most Reliable App & amp; Cross Browser Testing Platform | BrowserStack '';
// Check if title is as look (assertion)
if (actualTitle.equals (expectedTitle)) {
assertionResults.add (new AssertionResult (true, `` Title match as expected ''));
} else {
assertionResults.add (new AssertionResult (false, `` Title does not mate the expected value ''));
}
// Check if rubric is not void (assertion)
if (actualTitle! = null) {
assertionResults.add (new AssertionResult (true, `` Title is not null ''));
} else {
assertionResults.add (new AssertionResult (false, `` Title is null ''));
}
// Additional checks can be add as needed
boolean isTitleContainsKeyword = actualTitle.contains (`` BrowserStack '');
if (isTitleContainsKeyword) {
assertionResults.add (new AssertionResult (true, `` Title incorporate 'BrowserStack ' ''));
} else {
assertionResults.add (new AssertionResult (false, `` Title does not contain 'BrowserStack ' ''));
}
// At the end of the test, report all assertion results
for (AssertionResult termination: assertionResults) {
if (result.passed) {
System.out.println (`` PASSED: `` + result.message);
} else {
System.out.println (`` FAILED: `` + result.message);
}
}
driver.quit ();
}
}
Conclusion
Related Guides
Automate This With SUSA
Test Your App Autonomously