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 thoughts on “Soft Assertion For Selenium WebDriver With TestNG”
great info man
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!!
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.
where ever I am using Soft Assert, testng report never shows fail. can you tell me how you resolved this issue?
Are you using s_assert.assertAll(); at the end of your @test method?
add soft assert in test methods instead of class
SoftAssert s_assert = new SoftAssert();
I am getting below error after trying your code.
[TestNG] Running:
C:Userscb08778AppDataLocalTemptestng-eclipse-2143853512testng-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
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
Could you tell me the purpose of assertAll()? please!
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();
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();
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();