Using soft asserts

Unlike hard assertions, soft assertions allow us to continue execution on failure without aborting. For this, a class called SoftAssert is used. An object of this class has to be created in order to use it:

SoftAssert assertNew = new SoftAssert();

Now use this object to invoke the various methods. Let's have a look at this with a simple example:

public class SoftAssertExample {
@Test
public void checkListeners() {
SoftAssert assertNew = new SoftAssert();
System.setProperty("webdriver.chrome.driver",
System.getProperty("user.dir")
+ "\src\main\resources\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.freecrm.com");
System.out.println("Title is : " + driver.getTitle());
assertNew.assertEquals(driver.getTitle(),
"# Free CRM software in the cloud for sales and
service");
System.out.println("Assert has been executed");
}
}
The output from the preceding code is given here:


If you check the output from this code, notice that even when the assertion fails, control passes to the statement following the assert, which in the preceding case is:

System.out.println("Assert has been executed");

Title is : #1 Free CRM software in the cloud for sales and service
Assert has been executed

checkListeners was a success
[Utils] Attempting to create C:UsersBhagyashreeworkspaceSeleniumFramework est-outputDefault suiteDefault test.xml
[Utils] Directory C:UsersBhagyashreeworkspaceSeleniumFramework est-outputDefault suite exists: true
PASSED: checkListeners
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================

Notice the println statement after the assert has been printed. Try the same code using a hard assert. You will notice that the println statement does not get printed and, moreover, the test fails.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.17.91