Sunday, February 14, 2016

Selenium : Simple Error Reporting in TestNG without Halt (Soft assertion)

 Simple Error Reporting in TestNG without Halt (Soft assertion)

 Following Class in TestNG offers reporting :
1. Soft Assertion : Execution will not skip the rest of the steps in  the method.
 SoftAssert soft_assert=new SoftAssert();  
 soft_assert.assertEquals("abc", "ABC");
 soft_assert.assertAll(); //should be added in all @Test Methods
2. Hard Assertion :Execution will skip rest of the steps in the method and move to the next method
Assert.assertEquals("abc", "ABC");

3. To log the information from the script to the HTML report we must use the class org.testng.Reporter
Now to print the data to the report we must use :
Reporter.log("PASS/FAIL");
-------------------------------------------------------------------------------------

 Project Tree



// Assert1.java

import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

public class Assert1 {
    SoftAssert soft_assert=new SoftAssert();
       
    @Test
    public void test1(){               
        Assert.assertEquals("abc", "ABC");
        System.out.println("hi");       
    }
   
    @Test
    public void test2(){               
        soft_assert.assertEquals("abc", "ABC");
        System.out.println("bye");
        soft_assert.assertAll();       
    }
}

//testng.xml

<suite name="TestNg_test">       
       <test name="assert1">>        
              <classes>
                     <class name="Assert1" />                     
              </classes>
       </test>       
</suite>

1 comment:

  1. How can i report in HTML report which asserts passed and which failed(not only tests)?

    ReplyDelete