How to use SoftAssert class of org.testng.asserts package

Best Testng code snippet using org.testng.asserts.SoftAssert

Run Testng automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1package Assertion;
2
3import org.testng.Reporter;
4import org.testng.annotations.Test;
5
6public class SoftAssert {
7  @Test
8  public void b() 
9  {
10	    Reporter.log("Launch the browser",true);
11		Reporter.log("launch the web application",true);
12		Reporter.log("Verify the login page title",true);
13		org.testng.asserts.SoftAssert softassert = new org.testng.asserts.SoftAssert();
14		softassert.assertEquals(false, true);
15		Reporter.log("login to the application",true);
16		
17		Reporter.log("verify the home page title",true);
18		
19		softassert.assertAll();
20  }
21}
22
Full Screen
copy
1package test.softassertions;
2
3import org.testng.annotations.Test;
4import org.testng.asserts.Assertion;
5import org.testng.asserts.SoftAssert;
6
7public class testing_softassertion {
8
9	private Assertion hardAssert = new Assertion();
10	private SoftAssert softAssert = new SoftAssert();
11	
12
13@Test
14public void testHardAndSoftAssert() {
15	softAssert.assertEquals(1, 1, "integer incorrect asserssion");
16	softAssert. assertTrue(Boolean.TRUE, "boolean1 incorrect asserssion");
17	softAssert.assertTrue(Boolean.FALSE, "boolean2 incorrect asserssion");
18	softAssert.assertTrue(Boolean.TRUE,  "boolean3 incorrect asserssion");
19	softAssert.assertTrue(Boolean.FALSE, "boolean4 incorrect asserssion");
20	softAssert.assertAll();
21}
22
23}
Full Screen
copy
1package test.assertion;
2
3import org.testng.Assert;
4import org.testng.annotations.Test;
5import org.testng.asserts.IAssert;
6import org.testng.asserts.SoftAssert;
7
8import java.util.ArrayList;
9import java.util.Collection;
10
11public class SoftAssertTest {
12
13  @Test
14  public void testOnSucceedAndFailureCalled() throws Exception {
15    final Collection<IAssert> succeed = new ArrayList<>();
16    final Collection<IAssert> failures = new ArrayList<>();
17    final SoftAssert sa = new SoftAssert() {
18      @Override
19      public void onAssertSuccess(IAssert assertCommand) {
20        succeed.add(assertCommand);
21      }
22
23      @Override
24      public void onAssertFailure(IAssert assertCommand, AssertionError ex) {
25        failures.add(assertCommand);
26      }
27    };
28    sa.assertTrue(true);
29    sa.assertTrue(false);
30    Assert.assertEquals(succeed.size(), 1, succeed.toString());
31    Assert.assertEquals(failures.size(), 1, failures.toString());
32  }
33
34  @Test
35  public void testAssertAllCount() throws Exception {
36    String message = "My message";
37    SoftAssert sa = new SoftAssert();
38    sa.assertTrue(true);
39    sa.assertTrue(false, message);
40    try {
41      sa.assertAll();
42      Assert.fail("Exception expected");
43    } catch (AssertionError e) {
44      String[] lines = e.getMessage().split("\r?\n");
45      Assert.assertEquals(lines.length, 2);
46      lines[1] = lines[1].replaceFirst(message, "");
47      Assert.assertFalse(lines[1].contains(message));
48    }
49  }
50}
51
Full Screen
copy
1verifyTrue(selenium.isTextPresent("Please enter Email ID"));
2verifyTrue(selenium.isTextPresent("Please enter Password"));
3
Full Screen
copy
1try{
2        Assert.assertEquals(true, false);
3        }catch(AssertionError e)
4        {
5            System.out.println("Assertion error. ");
6        }
7
8        System.out.println("Test Completed.");
9
Full Screen
copy
1package automation.tests;
2
3import org.testng.asserts.Assertion;
4import org.testng.asserts.SoftAssert;
5
6public class MyTest {
7  private Assertion hardAssert = new Assertion();
8  private SoftAssert softAssert = new SoftAssert();
9}
10
11@Test
12public void testForSoftAssertionFailure() {
13  softAssert.assertTrue(false);
14  softAssert.assertEquals(1, 2);
15  softAssert.assertAll();
16}
17
Full Screen
copy
1import org.testng.annotations.Test;
2import org.testng.asserts.SoftAssert;
3
4public class SoftAsert
5{
6    @Test
7    public void test()
8    {
9        SoftAssert asert=new SoftAssert();
10        asert.assertEquals(false, true,"failed");
11        asert.assertEquals(0, 1,"brokedown");
12        asert.assertAll();
13    }
14}
15
Full Screen
copy
1FAILED: test
2java.lang.AssertionError: The following asserts failed:
3failed, brokedown
4
Full Screen
copy
1SoftAssert softAssert = new SoftAssert();
2String ActualErrorMEssage = firstNameerrorXpath.getText;
3String ActualErrorMEssage2 = secondNameNameerrorXpath.getText;
4softAssert.assertEquals(ActualErrorMEssage,ExpectedErrorMEssage);
5softAssert.assertEquals(ActualErrorMEssage2,ExpectedErrorMEssage);
6softAssert.assertAll();
7
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used methods in SoftAssert

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)