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 for software web application. 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 Software Testing Tool
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 software web application's 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 for software web application to describe difference between both of them.

Run bellow given example In your eclipse with testng framework.

package Testng_Pack;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import 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 = null;
 
  @BeforeClass
  public void load_url(){
   System.setProperty("webdriver.gecko.driver", "D:\\Selenium Files\\geckodriver.exe");
   driver = new FirefoxDriver();
   driver.manage().window().maximize();
   driver.get("http://only-testing-blog.blogspot.com/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 software test 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.

12 comments:

  1. Hi This seems a very useful info and I tried using it in my keyword driven framework which uses both webderiver_TestBG. But where ever I am using Soft Assert, testng report never shows fail. Can u plz guide!!

    ReplyDelete
  2. HI, plz Ignore my previous comment it has been resolved. I was doing a thing wrong. But now I am facing other issue. If my Method1 contains some text in assertall. And after it my Method2 also fails then it shows messages from both Method1 and method2 in TestNG report. How can I make sure that msg from method2 only should show up.

    ReplyDelete
    Replies
    1. add soft assert in test methods instead of class
      SoftAssert s_assert = new SoftAssert();

      Delete
  3. where ever I am using Soft Assert, testng report never shows fail. can you tell me how you resolved this issue?

    ReplyDelete
    Replies
    1. Are you using s_assert.assertAll(); at the end of your @test method?

      Delete
    2. Could you tell me the purpose of assertAll()? please!

      Delete
  4. I am getting below error after trying your code.
    [TestNG] Running:
    C:\Users\cb08778\AppData\Local\Temp\testng-eclipse-2143853512\testng-customsuite.xml

    Aug 24, 2016 1:35:43 PM org.openqa.selenium.os.UnixProcess$SeleniumWatchDog destroyHarder
    INFO: Command failed to close cleanly. Destroying forcefully (v2). org.openqa.selenium.os.UnixProcess$SeleniumWatchDog@1eaee49
    FAILED: soft_assert_text
    java.lang.NoSuchMethodError: org.testng.collections.Maps.newLinkedHashMap()Ljava/util/Map;
    at org.testng.asserts.SoftAssert.(SoftAssert.java:14)
    at TestNG.Test1.soft_assert_text(Test1.java:50)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:702)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:894)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1219)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
    at org.testng.TestRunner.privateRun(TestRunner.java:768)
    at org.testng.TestRunner.run(TestRunner.java:617)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
    at org.testng.SuiteRunner.run(SuiteRunner.java:240)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1185)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1110)
    at org.testng.TestNG.run(TestNG.java:1022)
    at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:109)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:202)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:173)


    ===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0
    ===============================================


    ===============================================
    Default suite
    Total tests run: 1, Failures: 1, Skips: 0
    ===============================================

    [TestNG] Time taken by org.testng.reporters.EmailableReporter@1186cf9: 6 ms
    [TestNG] Time taken by org.testng.reporters.JUnitReportReporter@191c263: 3 ms
    [TestNG] Time taken by org.testng.reporters.jq.Main@34e2cc: 18 ms
    [TestNG] Time taken by org.testng.reporters.XMLReporter@1c74f8d: 8 ms
    [TestNG] Time taken by [TestListenerAdapter] Passed:0 Failed:0 Skipped:0]: 5 ms
    [TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@6df9bc: 16 ms

    I did initialization of SoftAssert sa= new SoftAssert(); in Test level and Class level. But both are getting failed

    ReplyDelete
  5. wow.. thank you so much for information and knowledge...
    great thing i learned today along with soft asert, ie use of wait.util.
    thankyou sir

    ReplyDelete
  6. Hi
    in my scenario Method 1 has two assertions and if first assertion fails then its not coming into second assertion, NOTE: i used two different ref varbles for soft assertion and two times i had written like softAssert1.assertAll(); ,softAssert2.assertAll();

    ReplyDelete
  7. Hi
    in my scenario Method 1 has two assertions and if first assertion fails then its not coming into second assertion, NOTE: i used two different ref varbles for soft assertion and two times i had written like softAssert1.assertAll(); ,softAssert2.assertAll();

    ReplyDelete
  8. Hi
    in my scenario Method 1 has two assertions and if first assertion fails then its not coming into second assertion, NOTE: i used two different ref varbles for soft assertion and two times i had written like softAssert1.assertAll(); ,softAssert2.assertAll();

    ReplyDelete