Professional Documents
Culture Documents
Soft Assertion For Selenium WebDriver With TestNG
Soft Assertion For Selenium WebDriver With TestNG
Soft Assertion For Selenium WebDriver With TestNG
If you know, we have already learn about testng hard assertions (Example
assertEquals, assertNotEquals, etc..) which we can use In our webdriver test.
Hard assertion examples links are given on THIS PAGE. View all those
examples one by one to learn hard assertions. Let me tell you one thing
about hard assertions, When hard assertion will fail Inside any test method,
remaining execution of that specific test method will be aborted. Now If you
wants to continue remaining test part execution even If assertion fails and
also you wants to report assertion failure In testng result report then you can
use testng soft assertion method.
Soft Assertions In Selenium WebDriver
To use testng soft assertion, you have to use testng SoftAssert class. This
class will helps to not throw an exception on assertion failure and recording
failure. If you will use soft assertion then your test execution will remain
continue even If any assertion fails. Another most Important thing Is your
assertion failure will be reported In report so that you can view It at end of
test. You can use soft assertion when you are using multiple assertions In
same test method and you wants to execute all of them even If any one In
between fails.
Let us look at very simple example of testng hard assertion and soft
assertion to see the difference between both of them. In bellow give
example, hard_assert_text() and soft_assert_text() each have 4 assertions.
Used hard assertions In hard_assert_text() method and soft assertions In
soft_assert_text() method to describe difference between both of them.
Run bellow given example In your eclipse with testng framework.
package Testng_Pack;
import
import
import
import
import
import
import
import
import
import
org.openqa.selenium.By;
org.openqa.selenium.WebDriver;
org.openqa.selenium.firefox.FirefoxDriver;
org.openqa.selenium.support.ui.ExpectedConditions;
org.openqa.selenium.support.ui.WebDriverWait;
org.testng.Assert;
org.testng.annotations.AfterClass;
org.testng.annotations.BeforeClass;
org.testng.annotations.Test;
org.testng.asserts.SoftAssert;