Table of contents
- Introduction to Asserts and Verify in Selenium
- What are Asserts in Selenium
- Types of Asserts in Selenium
- Hard Asserts (or Hard Assertions)
- Soft Asserts
- Difference Between Hard Asserts and Soft Asserts
- Asserts in TestNG for Selenium Automation Testing
- Hard Asserts in Selenium WebDriver using TestNG
- assertEquals
- Syntax
- Demonstration
- Code Walkthrough
- Execution
- assertNotEquals
- Syntax
- Demonstration
- Code Walkthrough
- Execution
- assertTrue
- Syntax
- Syntax
- Demonstration
- Code Walkthrough
- Execution
- assertFalse
- Syntax
- Syntax
- Demonstration
- Code Walkthrough
- Execution
- assertNotNull
- Syntax
- Demonstration
- Code Walkthrough
- Execution
- Soft Asserts in Selenium WebDriver using TestNG
- Code Walkthrough
- Execution
- Verify in Selenium Java
- Difference between Assert and Verify in Selenium
- It’s a Wrap
During the process of test automation, you would come across a number of scenarios where a decision needs to be taken regarding “What if the test(s) result in a failure?” If the error (or issue) being encountered is a minor one, you might want the test execution to continue. In case of serious errors, it is better to abort the execution of the test case (or test suite). This can be achieved using ‘Assert and Verify in Selenium WebDriver Tutorial.
Source-Media2giphy
Since there are different types of Asserts (Soft Assert and Hard Assert), it is essential to choose the best-suited Assert based on the design of the Selenium WebDriver tests. In this blog, we look at how to use Assert and Verify in Selenium WebDriver so that the QA team can take a decisive step on when to stop running the tests when a certain condition is not met.
Starting your journey with Selenium WebDriver? Check out this step-by-step guide to perform Automation testing using Selenium WebDriver.
Introduction to Asserts and Verify in Selenium
In Selenium WebDriver, both Assert and Verify in Selenium WebDriver are primarily used for validating web applications (or websites). Based on the scenario under test for Selenium test automation, a cordial decision has to be taken whether further test scenario (or test suite) execution is required or not.
This is where Assert in Selenium WebDriver and Verify in Selenium Java are instrumental in improving the efficiency of the Selenium WebDriver tests.
What are Asserts in Selenium
Assert in Selenium WebDriver is used for verifying or validating the scenario under test. Based on the result of the Assert, the outcome (i.e. pass/fail) of the test can be decided. A test scenario is considered as passed if the ‘achieved test result’ matches with the ‘expected test result’.
A simple example of an assert can be opening the web page under test, matching the Page Title with the expected title, and verifying if the required WebElements on the page are loaded or not. In case the Page URL is incorrect (i.e. does not match with the expected URL), an assert should be thrown since the test scenarios would definitely fail!
Some of the widely-used assert categories in Selenium are:
Assert Equals — assertEquals, assertNotEquals
Assert Boolean — assertTrue, assertFalse
Assert None — assertNull, assertNotNull
Assert Identical — assertSame, assertNotSame
In certain Selenium Test Automation scenarios, it is recommended to use Assert in Selenium WebDriver in the try..catch method so that appropriate Selenium WebDriver methods can be used for capturing the details of the failed test case. For instance, you might want to capture screenshot of the page using Selenium or WebElement (in Selenium 4) in case you intend to go to the nuts & bolts of the failed test.
Are you using Playwright for automation testing? Run your Playwright test scripts instantly on 50+ browser/OS combinations using the LambdaTest cloud.
Types of Asserts in Selenium
When performing web application (or website) testing, you may have certain test steps that do not have a major impact on the test scenario execution. As a QA Automation Engineer, you might be fine to proceed even if the assert for that test step results in a failure.
On the other hand, you might want to halt (or exit) the test execution if a critical test step results in a failure.
To cater to these different test requirements, Selenium supports two major types of asserts:
Hard Asserts (or Hard Assertions)
As the name indicates, test execution is halted when the condition as part of the assert is not met. Hard asserts usually throw an Assertion Error (i.e. java.lang.AssertionError) and the test scenario is marked as failed as soon as the hard assert condition fails. The Assertion Error should be handled in the try..catch block in Java.
Once the assert statement fails, the current test is skipped and the test suite continues with the next @Test.
Here are a few examples where Hard Asserts in Selenium WebDriver Java can be extremely helpful:
The customer is on the login page of the website and the login verification fails as the customer credentials are not correct. Here, a hard assertion can be thrown since the subsequent tests would be based on the condition that ‘customer login is successful’.
Hard assert should be thrown if the current page URL does not match with the expected URL.
Soft Asserts
In scenarios where you want the test execution to continue even after the failure of a test step, you should make use of Soft Assert in Selenium WebDriver. Unlike hard asserts; soft asserts do not throw any exception on the failure of the assert and continue to the next step even after encountering an assert.
Soft assert collects all the asserts encountered when running the @Test methods. The assertAll() method is called to throw all the exceptions caught during the process of Selenium test automation execution.
Soft Asserts are not included by default in the TestNG framework. You have to include the package org.testng.asserts.Softassert when soft assert has to be used in the tests.
Soft Assert in Selenium WebDriver should be used in scenarios where the failure of certain conditions (or test steps) does not hamper the execution of other test steps in that method.
This Playwright automation tutorial will guide you through the setup of the Playwright framework, which will enable you to write end-to-end tests for your future projects.
Difference Between Hard Asserts and Soft Asserts
By default, Asserts are ‘hard asserts’, irrespective of the underlying test automation framework (e.g. TestNG or JUnit) being used. Here are the key differences between Hard Asserts and Soft Asserts:
Program Execution
Hard Asserts are used when you want to halt the execution of the test script (or test method) when the assert condition does not match with the expected result.
Soft Asserts are used when the test script (or test method) need not be halted when the assertion condition does not meet the expected result.
Assertion Error
An assertion error (java.lang.AssertionError) is thrown when the conditions in the Hard Assert are met. Subsequent test steps (in the current method) after hard assert are skipped from execution and the execution proceeds with the next @Test in the test suite.
In the case of Soft Assert, the errors are accumulated in each @Test execution and the AssertAll() method throws asserts encountered during the process of Selenium Test Automation execution.
Default Asserts
By default, Assert in Selenium WebDriver are Hard Asserts. The org.testng.Assert package contains the methods used for throwing appropriate asserts.
You need to import the org.testng.asserts.SoftAssert package in order to use Soft Asserts.
Here is how you can create an instance of Hard Assert and Soft Assert in Selenium WebDriver:
/* Create an instance of Hard Assert */
Assertion hardAssert = new Assertion();
/* Create an instance of Soft Assert */
Assertion softAssert = new SoftAssert();
Asserts in TestNG for Selenium Automation Testing
As mentioned earlier, Hard Asserts (or Assertions) and Soft Asserts are the two major types of asserts in Selenium Java. In the case of Hard Asserts, an exception is thrown when the assert condition is not met.
Hard Asserts in Selenium WebDriver using TestNG
Asserts in the TestNG framework are provided by the class org.testng.Assert. By default, Asserts in Selenium WebDriver Java are Hard Asserts. For a quick recap on TestNG, you could refer to our blog on Annotations in TestNG to get started with the TestNG framework.
Apart from TestNG, many QA Engineers also prefer the JUnit framework. There is a difference in the way TestNG handles assertions in comparison to the JUnit framework. You can refer to our details to learn more about Assertions in JUnit for Selenium Testing.
TestNG provides a more sophisticated way of handling asserts — group tests, parameterized tests, and more. Though the basic execution structure of all Asserts in Selenium Java remains almost the same, it takes different parameters and performs validations based on the type of Assert.
In order to use Hard Asserts, the org.testng.asserts.Assertion package is imported at the start of the test code. The following pom.xml is used for downloading the required Maven dependencies needed to demonstrate assert in Selenium WebDriver:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.assertverify</groupId>
<artifactId>assertverify</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.28</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.0.0-alpha-7</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>4.0.0-alpha-7</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>4.0.0-alpha-7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>11</source>
<target>11</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Here are some of the popular TestNG Asserts that are used in Selenium Java:
assertEquals
The assertEquals method compares the actual object (or result) with the expected object (or result). The objects to be compared could be string, integer, byte, character, etc.
An assertion error is thrown if the actual result does not match with the expected result. On the occurrence of assertion error, the execution of the current test case (or method) is terminated and the execution proceeds with the next script (if any) in the test suite.
This Playwright browser testing tutorial will guide you through the setup of the Playwright framework, which will enable you to write end-to-end tests for your future projects.
Syntax
Assert. assertEquals(actual_object, expected_object, [Message])
Here are some of the popular forms of assertEquals method:
Assert.assertEquals(String Actual, String Expected)
Assert.assertEqual(String Actual, String Expected, String message)
Assert.assertEquals(boolean Actual, boolean Expected)
Assert.assertEquals(int Actual, int Expected)
Demonstration
Shown below is an example that demonstrates the usage of assertEquals assert in Selenium WebDriver:
package com.assertvar;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.asserts.Assertion;
import org.testng.asserts.SoftAssert;
public class test_assertverify
{
WebDriver driver;
String username = "user-name";
String access_key = "access-key";
@BeforeTest
public void init() throws InterruptedException, MalformedURLException
{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("build", "[Java] Assert and Verify in Selenium WebDriver");
capabilities.setCapability("name", "[Java] Assert and Verify in Selenium WebDriver");
capabilities.setCapability("platformName", "OS X Yosemite");
capabilities.setCapability("browserName", "MicrosoftEdge");
capabilities.setCapability("browserVersion","81.0");
capabilities.setCapability("tunnel",false);
capabilities.setCapability("network",true);
capabilities.setCapability("console",true);
capabilities.setCapability("visual",true);
driver = new RemoteWebDriver(new URL("http://" + username + ":" + access_key + "@hub.lambdatest.com/wd/hub"),
capabilities);
System.out.println("Started session");
}
@Test(description = "Demonstration of AssertEquals in Selenium Java", priority = 1, enabled = true)
public void Test_assert_equals() throws IOException, InterruptedException
{
String exp_title = "Most Powerful Cross Browser Testing Tool Online | LambdaTest";
String test_url = "https://www.lambdatest.com";
driver.manage().window().maximize();
driver.get(test_url);
Thread.sleep(3000);
String curr_window_title = driver.getTitle();
/* Hard Assert */
Assert.assertEquals(curr_window_title, exp_title);
System.out.println("AssertEquals Test Passed\n");
}
@AfterClass
public void tearDown()
{
if (driver != null)
{
driver.quit();
}
}
}
Code Walkthrough
Step (1)
The method implemented under the @BesforeTest annotation sets the Desired Browser Capabilities. The test is executed on the Selenium 4 Grid on LambdaTest and capabilities are generated using the LambdaTest Capabilities Generator.
The combination of user-name and access-key (which is available in the LambdaTest Profile Page) is used for creating an instance of RemoteWebDriver on the cloud-based LambdaTest Selenium Grid [@hub.lambdatest.com/wd/hub].
capabilities.setCapability("network",true);
capabilities.setCapability("console",true);
capabilities.setCapability("visual",true);
driver = new RemoteWebDriver(new URL("http://" + username + ":" + access_key + "@hub.lambdatest.com/wd/hub"), capabilities);
Step(2)
In the test method, we get the current window title using the getTitle() method of Selenium WebDriver.
String curr_window_title = driver.getTitle();
Step (3)
The assertEquals method is used for comparing the current window title with the expected window title. If the titles do not match, an Assertion Error is thrown.
Assert.assertEquals(curr_window_title, exp_title);
Execution
Here is the execution snapshot which indicates that no assert was thrown as the current window title matched with the expected window title.
assertNotEquals
The assertNotEquals method also compares the actual object (or result) with the expected object (or result). assertNotEquals is the opposite of assertEquals assertion.
It verifies whether the two objects being compared are equal or not. Assert is thrown if the compared objects are equal.
Syntax
Assert. assertNotEquals(actual_object, expected_object, [Message])
Here are some of the popular forms of assertNotEquals method:
Assert.assertNotEquals(String Actual, String Expected)
Assert.assertNotEquals(String Actual, String Expected, String message)
Demonstration
Shown below is an example that demonstrates the usage of assertNotEquals assert in Selenium WebDriver:
package com.assertvar;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.asserts.Assertion;
import org.testng.asserts.SoftAssert;
public class test_assertverify
{
WebDriver driver;
String username = "user-name";
String access_key = "access-key";
@BeforeTest
public void init() throws InterruptedException, MalformedURLException
{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("build", "[Java] Assert and Verify in Selenium WebDriver");
capabilities.setCapability("name", "[Java] Assert and Verify in Selenium WebDriver");
capabilities.setCapability("platformName", "OS X Yosemite");
capabilities.setCapability("browserName", "MicrosoftEdge");
capabilities.setCapability("browserVersion","81.0");
capabilities.setCapability("tunnel",false);
capabilities.setCapability("network",true);
capabilities.setCapability("console",true);
capabilities.setCapability("visual",true);
driver = new RemoteWebDriver(new URL("http://" + username + ":" + access_key + "@hub.lambdatest.com/wd/hub"),
capabilities);
System.out.println("Started session");
}
@Test (description = "Demonstration of AssertNotEquals in Selenium Java", priority = 2, enabled = true)
public void Test_assert_not_equals() throws IOException, InterruptedException
{
String test_url = "https://www.lambdatest.com";
By by_emailpath = By.cssSelector("form.blue_form > #useremail");
driver.manage().window().maximize();
driver.get(test_url);
Thread.sleep(3000);
String curr_window_title = driver.getTitle();
WebElement elem_email = driver.findElement(by_emailpath);
try
{
elem_email.sendKeys("testing123456@testing123456.com");
Thread.sleep(3000);
driver.findElement(By.xpath("//*[@id=\"testing_form\"]/div/button")).click();
/* In case the email is not registered, the URL would be
https://accounts.lambdatest.com/register?email=testing123456@testing123456.com
*/
Assert.assertNotEquals(driver.getCurrentUrl(), "https://www.lambdatest.com/");
Thread.sleep(3000);
}
catch (Exception ex)
{
System.out.print(ex.getMessage());
}
System.out.println("AssertNotEquals Test Passed\n");
}
@AfterClass
public void tearDown()
{
if (driver != null)
{
driver.quit();
}
}
}
It’s crucial to debug websites for Safari before pushing them live. In this article, we look at how to debug websites using dev tools in Safari.
Code Walkthrough
Step (1)
On LambdaTest’s home page, locate the WebElement where the email address has to be entered using the findElement method in Selenium WebDriver. The method uses the cssSelector property for locating the corresponding WebElement.
String test_url = "https://www.lambdatest.com";
By by_emailpath = By.cssSelector("form.blue_form > #useremail");
driver.manage().window().maximize();
driver.get(test_url);
Thread.sleep(3000);
String curr_window_title = driver.getTitle();
WebElement elem_email = driver.findElement(by_emailpath);
Step (2)
In the located Element, enter a random email address using the sendKeys method of Selenium WebDriver.
elem_email.sendKeys(“testing123456@testing123456.com”);
Step (3)
Click on the ‘Start Free Testing’ button located using the findElement method in Selenium. The Selenium find element by XPath is obtained using the ‘Inspect Tool’ in Chrome. Perform a click operation on the button element.
driver.findElement(By.xpath(“//*[@id=\”testing_form\”]/div/button”)).click();
Step (4)
Since there is no account with the random email-address, the execution proceeds to the page where the necessary details are asked for.
The assertNotEquals assert method throws an assert if the current URL is the LambdaTest homepage URL.
/* In case the email is not registered, the URL would be
https://accounts.lambdatest.com/register?email=testing123456@testing123456.com
*/
Assert.assertNotEquals(driver.getCurrentUrl(), "https://www.lambdatest.com/");
Execution
As seen in the execution snapshot, Assert is not thrown and the test executes successfully.
assertTrue
The assertTrue assert in Selenium WebDriver should be used in case you are dealing with Boolean conditions. There are two distinct ways in which you can make use of assertTrue for Selenium test automation:
a. By passing the condition as a Boolean parameter that is used to assert with the assertTrue method. An assert is thrown if the condition given in the Assert is not True.
Syntax
Assert.assertTrue(boolean condition);
b. This method of Assert in Selenium WebDriver takes two parameters — first parameter is the condition which if not satisfied leads to raising an assert and second parameter is the assertion error message that is displayed when the assert is thrown.
Syntax
Assert.assertTrue(boolean condition, String message);
Here are the two ways in which assertTrue method can be used in Selenium Java:
Demonstration
As part of the demonstration of the assertTrue method, we locate the ‘Resources’ menu on LambdaTest homepage. Inside the menu, we click on the link ‘Blog’ to navigate to the Lambdatest Blog.
The assertTrue method throws an assert if the expected URL after clicking on the ‘Blog’ link is not LambdaTest Blog.
package com.assertvar;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.asserts.Assertion;
import org.testng.asserts.SoftAssert;
public class test_assertverify
{
WebDriver driver;
String username = "user-name";
String access_key = "access-key";
@BeforeTest
public void init() throws InterruptedException, MalformedURLException
{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("build", "[Java] Assert and Verify in Selenium WebDriver");
capabilities.setCapability("name", "[Java] Assert and Verify in Selenium WebDriver");
capabilities.setCapability("platformName", "OS X Yosemite");
capabilities.setCapability("browserName", "MicrosoftEdge");
capabilities.setCapability("browserVersion","81.0");
capabilities.setCapability("tunnel",false);
capabilities.setCapability("network",true);
capabilities.setCapability("console",true);
capabilities.setCapability("visual",true);
driver = new RemoteWebDriver(new URL("http://" + username + ":" + access_key + "@hub.lambdatest.com/wd/hub"),
capabilities);
System.out.println("Started session");
}
@Test (description = "Demonstration of assertTrue in Selenium Java", priority = 3, enabled = true)
public void Test_assert_true() throws IOException, InterruptedException
{
String test_url = "https://www.lambdatest.com";
Actions actions = new Actions(driver);
driver.get(test_url);
WebElement elem_resources_menu = driver.findElement(By.xpath("//a[contains(.,'Resources')]"));
actions.moveToElement(elem_resources_menu).build().perform();
Thread.sleep(2000);
By elem_blog_link = By.xpath("//a[.='Blog']");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.presenceOfElementLocated(elem_blog_link)).click();
Thread.sleep(2000);
String expected_url = "https://www.lambdatest.com/blog/";
String current_url = driver.getCurrentUrl();
Assert.assertTrue(expected_url.equals(current_url), "URL does not match\n");
Thread.sleep(2000);
System.out.println("AssertTrue Test Passed\n");
}
@AfterClass
public void tearDown()
{
if (driver != null)
{
driver.quit();
}
}
}
Code Walkthrough
Step (1)
After creating a RemoteWebDriver instance, navigate to the URL under test (i.e. LambdaTest home page). Create an instance of the Action class in Selenium that provides intuitive methods for performing keyboard and mouse actions in Selenium WebDriver.
String test_url = "https://www.lambdatest.com";
Actions actions = new Actions(driver);
driver.get(test_url);
Watch this video to learn what the Actions Class is in Selenium and how to use it.
Step (2)
Locate the ‘Resources’ menu WebElement using the findElement method in Selenium WebDriver. We used the ‘Inspect Tool’ to get the XPath property of the WebElement.
WebElement elem_resources_menu = driver.findElement(By.xpath(“//a[contains(.,’Resources’)]”));
Step (3)
Using the movetoElement method provided by the ActionChains class, move to the ‘Resources Menu’ WebElement which we located in Step(2). The build() method is used for building the chain of actions and the perform() method carries out the chained interactions.
actions.moveToElement(elem_resources_menu).build().perform();
Step (4)
Since the Blog link is inside a menu, we used the POM Builder plugin in Chrome for getting the XPath property of the WebElement.
By elem_blog_link = By.xpath(“//a[.=’Blog’]”);
An Explicit Wait for 5 seconds is triggered to tell the Selenium WebDriver to wait for the ‘presenceOfElementLocated’ condition for ‘Blog’ WebElement.
A timeout error occurs if the ‘Blog’ WebElement is not located within the specified duration of 5 seconds. On presence of the element, a click operation is performed on the Blog link so that we navigate to the LambdaTest blog page.
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.presenceOfElementLocated(elem_blog_link)).click();
Step (5)
On successful execution of Step (4), the current page should be LambdaTest Blog. The getCurrentUrl() method of Selenium WebDriver is used for getting the current page URL.
The assertTrue method is used for raising an assert with a custom message if the current URL does not match with the expected URL (i.e. LambdaTest blog). The assert is raised when the condition in assertTrue() method is False (i.e. it is not satisfied).
String expected_url = "https://www.lambdatest.com/blog/";
String current_url = driver.getCurrentUrl();
Assert.assertTrue(expected_url.equals(current_url), "URL does not match\n");
Inspect web elements to help developers and testers to debug UI flaws or make modifications in HTML or CSS files. Learn how to inspect on MacBook.
Execution
Here is the execution snapshot from the IntelliJ IDE and LambdaTest Automation Dashboard which indicates that the test was executed successfully (i.e. the Selenium WebDriver navigated to the LambdaTest Blog).
assertFalse
Like the assertTrue method, this assert in Selenium WebDriver should also be used in case you are dealing with Boolean conditions. There are two distinct ways in which you can make use of assertFalse in Selenium Java:
a. By passing the condition as a Boolean parameter that is used to assert with the assertFalse method. Assert is thrown if the given condition is met (i.e. it is True)
Syntax
Assert.assertFalse(boolean condition);
b. Here the assertFalse method takes two parameters — first parameter is the condition which leads to raising an assert if the condition is met and second parameter is the assertion error message that is displayed when the assert is thrown.
Syntax
Assert.assertFalse(boolean condition, String message);
Here are the two ways in which assertFalse method can be used in Selenium Java:
Demonstration
package com.assertvar;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.asserts.Assertion;
import org.testng.asserts.SoftAssert;
public class test_assertverify
{
WebDriver driver;
String username = "user-name";
String access_key = "access-key";
@BeforeTest
public void init() throws InterruptedException, MalformedURLException
{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("build", "[Java] Assert and Verify in Selenium WebDriver");
capabilities.setCapability("name", "[Java] Assert and Verify in Selenium WebDriver");
capabilities.setCapability("platformName", "OS X Yosemite");
capabilities.setCapability("browserName", "MicrosoftEdge");
capabilities.setCapability("browserVersion","81.0");
capabilities.setCapability("tunnel",false);
capabilities.setCapability("network",true);
capabilities.setCapability("console",true);
capabilities.setCapability("visual",true);
driver = new RemoteWebDriver(new URL("http://" + username + ":" + access_key + "@hub.lambdatest.com/wd/hub"),
capabilities);
System.out.println("Started session");
}
@Test (description = "Demonstration of assertFalse in Selenium Java", priority = 4, enabled = true)
public void Test_assert_false() throws IOException, InterruptedException
{
String test_url = "https://www.lambdatest.com";
String window_title = "Most Powerful Cross Browser Testing Tool Online | LambdaTest";
driver.get(test_url);
Boolean bTitleCheck = driver.getTitle().equalsIgnoreCase(window_title);
Assert.assertFalse(bTitleCheck);
Thread.sleep(2000);
System.out.println("AssertFalse Test Passed\n");
}
@AfterClass
public void tearDown()
{
if (driver != null)
{
driver.quit();
}
}
}
Code Walkthrough
Step (1)
We navigate to the test URL using the RemoteWebDriver instance. A boolean variable is defined which is assigned a Boolean value depending on the condition (A case insensitive comparison of Current Page Title is performed against the Expected Page Title)
String test_url = "https://www.lambdatest.com";
driver.get(test_url);
Boolean bTitleCheck = driver.getTitle().equalsIgnoreCase(window_title);
Step (2)
The assertFalse method throws an Assert if the condition variable — bTitleCheck is True.
Assert.assertFalse(bTitleCheck);
In this example, the condition turns out to be True which means that assertFalse throws an Assert without displaying any custom message.
Execution
Here is the execution snapshot which indicates that the assertFalse condition resulted in True, thereby throwing an Assert.
assertNotNull
The assertNotNull method is used for checking if a particular object is NULL or not. This method throws an assert if the object has some value (i.e. it is not NULL). In case of an assert, the current test method is aborted with an exception.
Like the assertTrue and assertFalse methods, the assertNotNull method also provides two options wherein you can have a custom message printed when the assert is thrown.
Syntax
Assert.assertNotNull(Object object);
Assert.assertNotNull(Object object, String message);
Demonstration
package com.assertvar;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.asserts.Assertion;
import org.testng.asserts.SoftAssert;
public class test_assertverify
{
WebDriver driver;
String username = "user-name";
String access_key = "access-key";
@BeforeTest
public void init() throws InterruptedException, MalformedURLException
{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("build", "[Java] Assert and Verify in Selenium WebDriver");
capabilities.setCapability("name", "[Java] Assert and Verify in Selenium WebDriver");
capabilities.setCapability("platformName", "OS X Yosemite");
capabilities.setCapability("browserName", "MicrosoftEdge");
capabilities.setCapability("browserVersion","81.0");
capabilities.setCapability("tunnel",false);
capabilities.setCapability("network",true);
capabilities.setCapability("console",true);
capabilities.setCapability("visual",true);
driver = new RemoteWebDriver(new URL("http://" + username + ":" + access_key + "@hub.lambdatest.com/wd/hub"),
capabilities);
System.out.println("Started session");
}
@Test (description = "Demonstration of assertNotNull in Selenium Java", priority = 5, enabled = true)
public void Test_assert_notnull() throws IOException, InterruptedException
{
String test_url = "https://www.lambdatest.com";
String window_title = "Most Powerful Cross Browser Testing Tool Online | LambdaTest";
driver.get(test_url);
String current_url = driver.getCurrentUrl();
Assert.assertNotNull("Null Object", current_url);
Thread.sleep(2000);
System.out.println("AssertNotNull Test Passed");
}
@AfterClass
public void tearDown()
{
if (driver != null)
{
driver.quit();
}
}
}
Code Walkthrough
Step(1)
After navigating to the test URL, we get the current page URL using the getCurrentURL method of Selenium WebDriver.
String current_url = driver.getCurrentUrl();
Step (2)
The assertNotNull method throws an assert if the current URL is NULL. A custom message is displayed when the assert is thrown.
Assert.assertNotNull(“Null Object”, current_url);
Execution
As seen in the execution snapshot, the current page URL is not NULL, hence assert is not thrown and the test passes successfully.
Soft Asserts in Selenium WebDriver using TestNG
Soft assert is a type of assert in Selenium WebDriver that does not throw an exception when the assert condition is not met. The assertAll() method has to be invoked in order to throw all the exceptions that have been caught during the execution process.
Soft Asserts can be used by including the methods provided in the org.testng.asserts.Softassert class.
To demonstrate the difference between Hard Asserts and Soft Asserts, we use a simple example where certain asserts are thrown since the conditions in those asserts are not satisfied.
package com.assertvar;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
public class test_assertverify
{
WebDriver driver;
String username = "user-name";
String access_key = "access-key";
@BeforeTest
public void init() throws InterruptedException, MalformedURLException
{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("build", "[Java] Assert and Verify in Selenium WebDriver");
capabilities.setCapability("name", "[Java] Assert and Verify in Selenium WebDriver");
capabilities.setCapability("platformName", "OS X Yosemite");
capabilities.setCapability("browserName", "MicrosoftEdge");
capabilities.setCapability("browserVersion","81.0");
capabilities.setCapability("tunnel",false);
capabilities.setCapability("network",true);
capabilities.setCapability("console",true);
capabilities.setCapability("visual",true);
driver = new RemoteWebDriver(new URL("http://" + username + ":" + access_key + "@hub.lambdatest.com/wd/hub"),
capabilities);
System.out.println("Started session");
}
@Test (description = "Demonstration of softAssert in Selenium Java", priority = 6, enabled = true)
public void Test_soft_assert() throws IOException, InterruptedException
{
String test_url = "https://www.lambdatest.com";
String test_url_title = "Most Powerful Cross Browser Testing Tool Online | LambdaTest";
String test_community_url = "https://community.lambdatest.com/";
String test_community_title = "LambdaTest Community - Connect, Ask & Learn with Tech-Savvy Folks";
driver.get(test_url);
String current_url = driver.getCurrentUrl();
/* Create an instance of Soft Assert */
SoftAssert softAssert = new SoftAssert();
/* This raises an assert but does not throw an exception since it is a Soft Assert */
softAssert.assertEquals(current_url, test_community_url);
softAssert.assertNotEquals(test_url_title, test_community_title);
softAssert.assertNull((driver.getCurrentUrl()), "Null Object Found\n");
softAssert.assertSame((driver.getCurrentUrl()), "https://www.lambdatest.com","Expected and Current URL are not same");
softAssert.assertAll();
Thread.sleep(2000);
System.out.println("SoftAssert Test Passed\n");
}
@AfterClass
public void tearDown()
{
if (driver != null)
{
driver.quit();
}
}
}
Code Walkthrough
Step (1)
As we are using Soft Asserts, the required package is imported at the start of the test implementation.
import org.testng.asserts.SoftAssert;
Step (2)
We get the current page URL using the getCurrentURL method of Selenium WebDriver.
driver.get(test_url);
String current_url = driver.getCurrentUrl();
Step (3)
Create an instance of Soft Assert. The same instance will be used with different assert methods further in the test code.
SoftAssert softAssert = new SoftAssert();
Step (4)
The assertEquals method throws an assert if the two URLs (or strings) do not match. Since the assert condition is not met, an assert is thrown but the execution continues with the next test step.
softAssert.assertEquals(current_url, test_community_url);
Step (5)
The condition in assertNotEquals method is met since the test page title and LambdaTest community page titles do not match. Hence, no assert is thrown at this step.
softAssert.assertNotEquals(test_url_title, test_community_title);
Step (6)
The assertNull method throws an assert since the current page URL is not NULL. The custom message is printed on the screen and the test execution continues with the next step.
softAssert.assertNull((driver.getCurrentUrl()), “Null Object Found\n”);
Step (7)
The assertSame method checks whether the current page URL is the same (or equal to) as the URL provided in the test condition. Since the assert condition is satisfied, assert is not thrown.
softAssert.assertSame((driver.getCurrentUrl()), “https://www.lambdatest.com","Expected and Current URL are not same”);
Step (8)
The assertAll() method is invoked to throw all the exceptions encountered during the test execution. In our case, asserts are thrown at step(4) and step(6).
softAssert.assertAll();
Thread.sleep(2000);
System.out.println("SoftAssert Test Passed\n");
Execution
As shown below, asserts thrown at steps 4 & 6 do not result in an exception and the execution continues with the remaining steps in the test scenario.
Here is the testng.xml which includes the tests that demonstrated Hard Asserts and Soft Asserts in Selenium Java:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Assert and Verify Demo">
<test verbose="2" preserve-order="true" name="Assert and Verify Demo">
<classes>
<class name="com.assertvar.test_assertverify">
</class>
</classes>
</test>
</suite>
Verify in Selenium Java
Apart from Hard Asserts and Soft Asserts, you can also use Verify in Selenium Java for verifying if a specified condition is true or false. The test execution would still continue, irrespective of the status of ‘verify’.
At a functional level, Verify in Selenium Java is similar to Soft Assert in Selenium WebDriver. Also, Verify is implemented using the SoftAssert class.
The difference between Verify and SoftAssert in TestNG framework lies in the fact that SoftAssert gives improved clarity as far as code and reporting is concerned.
Verify in Selenium Java is normally used in a try…catch block as shown in the below example:
if(isElementPresent(By.linkText("Lambdatest")))
{
System.out.println("LambdaTest link is present");
}
else
{
System.out.println("LambdaTest link is not present");
}
Difference between Assert and Verify in Selenium
Though Soft Assert and Verify have almost the same functionality, there is a significant amount of difference between Assert (particularly Hard Assert) and Verify.
Execution of the next step
For Hard Asserts, a failure in a test step results in an Exception and the test case is marked as failed. Soft Asserts do not throw an exception on the failure of the assert and execution continues with the next step (post the failure).
Akin to Soft Asserts, Verify in Selenium Java continues with the execution of the next test step, irrespective of ‘verify status’ of the previous test step.
Categories
Hard Asserts and Soft Asserts are the two major categories of Asserts.
There are no categories for Verify in Selenium Java.
Usage
Asserts (particularly Hard Asserts) are used as checkpoints for validating the logic in business-critical applications.
Verify in Selenium Java is used in scenarios where the failure of a particular condition does not result in serious repercussions on the subsequent test step. At the end of the test, the AssertAll() method has to be invoked for viewing the results.
This article explains the emulator vs simulator vs real device differences, the learning of which can help you select the right mobile testing solution for your business.
It’s a Wrap
Source- MBAforbrowngirls
Assertions (or Asserts) play an integral role when it comes to Selenium automation testing. They are used to check if the tests have passed or failed by looking into the results of the respective Assert methods.
Hard Assertis an assert in Selenium WebDriver that is used in scenarios where you want the test case execution to halt when the condition in the assert method is not met. It raises java.lang.AssertionError when the condition in Hard Assert fails.
On the other hand, Soft Asserts are used in cases where the failure of a test step does not result in the failure of the test scenario.
When do you use Hard Asserts and Soft Asserts, do let us know in the comments section.
Happy Testing ☺