How to Use Soft Assert in Selenium Webdriver

How to Use Soft Assert in Selenium Webdriver

During the process of Selenium Automation Testing, 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'.

Be assertive

Source

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.

TABLE OF CONTENT

  • 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
      • Program Execution
      • Assertion Error
      • Default Asserts
  • Asserts in TestNG for Selenium Testing
    • Hard Asserts in Selenium WebDriver using TestNG
      • assertEquals
      • assertNotEquals
      • assertTrue
      • assertFalse
      • assertNotNull
    • Soft Asserts in Selenium WebDriver using TestNG
  • Verify in Selenium Java
    • Difference between Assert and Verify in Selenium

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.

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.

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.

Asserts in Selenium WebDriver

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:

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.

assertEquals

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.

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:

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.

image4

assertNotEquals

The assertNotEquals method also compares the actual object (or result) with the expected object (or result). assertNotEquals is the opposite of assertEquals assertion.

assertNotEquals

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:

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

findElement method in Selenium WebDriver

Step (2)

In the located Element, enter a random email address using the sendKeys method of Selenium WebDriver.

Step (3)

Click on the 'Start Free Testing' button located using the findElement method in Selenium. The XPath of the element 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.

Signup to browser automation tool

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

*/

Assert . assertNotEquals ( driver . getCurrentUrl ( ) , "https://www.lambdatest.com/" ) ;

Execution

As seen in the execution snapshot, Assert is not thrown and the test executes successfully.

test execution snapshot

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:

assertTrue method

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.

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

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.

findElement method in Selenium WebDriver

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']" ) ;

getting the XPath property of the WebElement

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

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

IntelliJ IDE and LambdaTest
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:

assertFalse

Demonstration

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.

assertFalse test execution

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.

assertNotNull

Syntax

Assert . assertNotNull ( Object object ) ;

Assert . assertNotNull ( Object object , String message ) ;

Demonstration

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.

assertNotNull execution

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.

Read – JUnit Asserts For Selenium Automation Testing

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.

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.

assertAll() method

Here is the testng.xml which includes the tests that demonstrated Hard Asserts and Soft Asserts in Selenium Java:

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.

It's a Wrap

We did it

Source

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 ☺

Himanshu Sheth

Himanshu Sheth is a seasoned technologist and blogger with more than 15+ years of diverse working experience. He currently works as the 'Lead Developer Evangelist' and 'Senior Manager [Technical Content Marketing]' at LambdaTest. He is very active with the startup community in Bengaluru (and down South) and loves interacting with passionate founders on his personal blog (which he has been maintaining since last 15+ years).

Written by

Source: https://www.lambdatest.com/blog/assert-and-verify-in-selenium-webdriver/

Posted by: bardonwearprapart.blogspot.com

0 Response to "How to Use Soft Assert in Selenium Webdriver"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel