Hard Assert vs Soft Assert
Asserts are used in Selenium WebDriver for verifying and validating the scenario under test. A test scenario is considered “passed” if the achieved test result matches the expected test result. In this article, we will explore the difference between hard assertions and soft assertions, as well as when and why each of these techniques should be used.
What is Hard Assert?
Hard Assert is a technique used in software testing to check whether a certain condition is true or not. In other words, it is a way of verifying that a certain piece of code is working as expected. When an assert statement is executed, it compares the actual outcome of a test with the expected outcome. If the two outcomes match, the assert statement passes and the test continues. If the actual outcome does not match the expected outcome, the assert statement fails and the test is halted. The remaining tests are skipped and the test is marked as failed.
Assertion hardAssert = new Assertion();
Some widely used assert categories are:
- Assert Equals: This includes assertEquals, and assertNotEquals.
- Assert Boolean: This includes assertTrue, and assertFalse.
- Assert None: This includes assertNull, and assertNotNull.
- Assert Identical: This includes assertSame, assertNotSame.
Below is the table with the description of the common methods available in assert:
|Types of Hard Assertions||Description|
|assertEquals()||This method takes a minimum of 2 arguments and can compare Strings, Integers, Doubles, and many more variables.|
|assertNotEquals()||This method does the opposite of the assertEquals() method.|
|assertTrue()||This method verifies the Boolean value returned by the condition. If the Boolean value is true, then the assertion passes the test case.|
|assertFalse()||This method works the opposite of assertTrue(). If the Boolean value returned by the condition is false, the assertion passes the test case.|
|assertNull()||This method verifies if the expected output is null and if not then the value returned is false.|
|assertNotNull()||This method works opposite to the assertNull() method and the assertion condition is met when the method validates the expected output to be not null.|
|assertSame()||This method is used to compare the objects and not the content. It returns true if the expected value is the same as the actual value else returns false.|
|assertNotSame()||This works opposite to assertSame().|
Example: Let’s take an example of testing a login page for a website. One of the tests you might want to perform is to check whether the login page displays an error message when an invalid username and password are entered. In this case, you would use an assert statement to verify that the error message is indeed displayed when the login page is submitted with invalid credentials. If the error message is displayed as expected, the assert statement would pass and the test would continue. If the error message is not displayed, the assert statement would fail and the test would be halted.
What is Soft Assert?
Soft assertions are the ones in which the test execution does not stop if the test does not meet the assertion condition. The remaining tests are executed. All the above methods also work with soft assertions.
Assertion softAssert = new SoftAssert();
Example: Let’s take an example of testing a shopping cart feature for an e-commerce website. One of the tests you might want to perform is to check whether the shopping cart displays the correct number of items when items are added and removed. In this case, you would use a verified statement to check that the number of items displayed in the shopping cart is correct. If the number of items displayed matches the expected outcome, the verification statement would pass and the test would continue. If the number of items displayed does not match the expected outcome, the verification statement would fail but the test would continue and the failure would be recorded in the test results.
Assert vs Verify
Below are the differences between assert and verify:
|Factor||Hard Assert||Soft Assert|
|Definition||The assertion is an expression that is used to check the expected and actual results of a test case. It is used to make sure that the actual result matches the expected result.||Verification is a process of validating or confirming that the expected behavior of the application is happening.|
|Execution of the next step||Test execution will be aborted if the assert condition is not met.||Test execution will continue till the end of the test case even if the assert condition is not met.|
|Test Results||No need to invoke any method to view test results.||Tester needs to invoke assertAll(), to view assertions at the end of the test result.|
|Default Asserts||By default, Assert in Selenium WebDriver are Hard Asserts.||These are not included by default in the TestNG framework.|
|Package||Import the org.testng.Assert package.||Import the org.testng.asserts.SoftAssert package.|
|Syntax||Assertion hardAssert = new Assertion();||Assertion softAssert = new SoftAssert();|
|Usage||These should be used in scenarios where it is required to halt the test execution if a critical test step results in a failure.||These should be used in scenarios where the failure of certain conditions does not hamper the execution of other test steps in that method.|
|Example||Opening the webpage www.geeksforgeeks.org and matching page title with the expected title and verifying if the required web elements are being displayed or not. If the page URL is incorrect then the assert should be thrown.||In eCommerce website when the order is being placed, the soft assert can be used to verify the number of cart items. If the number of cart items matches with the one user expected value then the test is passed and if not test result is noted and the test execution continues.|
Both hard and soft assertions are important for designing and running the Selenium WebDriver tests and are instrumental in verifying the application behavior at critical stages of the testing.
Please Login to comment...