Soft Assertion For Selenium WebDriver With TestNG

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

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;

public class Soft_Assert {

//Created object of testng SoftAssert class to use It's Properties.


SoftAssert s_assert = new SoftAssert();
String Actualtext;
WebDriver driver = new FirefoxDriver();
@BeforeClass
public void load_url(){
driver.manage().window().maximize();
driver.get("http://only-testing-blog.blogspot.in/2014/01/textbox.html");
}
@Test
//In this method, If any assertion fails then execution will be aborted.
public void hard_assert_text() {
Actualtext = driver.findElement(By.xpath("//h2/span")).getText();
//Text on expected side Is written Incorrect intentionally to get fail this
assertion.
Assert.assertEquals(Actualtext, "Tuesday, 01 January 2014", "1st assert
failed.");
System.out.println("Hard Assertion -> 1st pagetext assertion executed.");
Assert.assertEquals(Actualtext, "Tuesday, 28 January 2014", "2nd assert
failed.");
System.out.println("Hard Assertion -> 2nd pagetext assertion executed.");
driver.findElement(By.xpath("//input[@value='Show Me Alert']")).click();
String Alert_text = driver.switchTo().alert().getText();
driver.switchTo().alert().accept();
Assert.assertEquals(Alert_text, "Hi.. is alert message!", "Alert Is InCorrect");
System.out.println("Hard Assertion -> 1st alert assertion executed.");
Assert.assertEquals(Alert_text, "Hi.. This is alert message!", "Alert Is
Correct");
System.out.println("Hard Assertion -> 2nd alert assertion executed.");
}
@Test
//In this method, Test execution will not abort even If any assertion fail. Full
Test will be executed.
public void soft_assert_text() {
Actualtext = driver.findElement(By.xpath("//h2/span")).getText();
//Text on expected side Is written Incorrect intentionally to get fail this
assertion.
s_assert.assertEquals(Actualtext, "Tuesday, 01 January 2014", "1st assert
failed.");

System.out.println("Soft Assertion -> 1st pagetext assertion executed.");


s_assert.assertEquals(Actualtext, "Tuesday, 28 January 2014", "2nd assert
failed.");
System.out.println("Soft Assertion -> 2nd pagetext assertion executed.");
driver.findElement(By.xpath("//input[@value='Show Me Alert']")).click();
String Alert_text = driver.switchTo().alert().getText();
driver.switchTo().alert().accept();
//Alert expected text Is written Incorrect intentionally to get fail this
assertion.
s_assert.assertEquals(Alert_text, "Hi.. is alert message!", "Alert Is
InCorrect");
System.out.println("Soft Assertion -> 1st alert assertion executed.");
s_assert.assertEquals(Alert_text, "Hi.. This is alert message!", "Alert Is
Correct");
System.out.println("Soft Assertion -> 2nd alert assertion executed.");
s_assert.assertAll();
}
@Test
public void wait_and_click(){
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='
submitButton']")));
driver.findElement(By.xpath("//input[@id='submitButton']")).click();
}
@AfterClass
public void Closebrowser(){
driver.quit();
}
}
On execution completion, you will get bellow given result In console.
Soft Assertion -> 1st pagetext assertion executed.
Soft Assertion -> 2nd pagetext assertion executed.
Soft Assertion -> 1st alert assertion executed.
Soft Assertion -> 2nd alert assertion executed.
PASSED: wait_and_click
FAILED: hard_assert_text

As per console result, soft_assert_text() method Is executed full and printed


all statements even failed 2 assertions. On other side, hard_assert_text()
method has not printed any statement In console because execution aborted
on first assertion failure.
Now If you look at testng test result report, both failures of soft assertion has
been reported In testng report (You can view this post to see HOW TO VIEW
TESTNG TEST RESULT REPORT). That means your assertion failure has been
reported without test abortion. Look at bellow give our testng test result
report.

You might also like