Assertion in TestNG


ASSERTIONS:

Generally Assertions in Testng is used to validation purpose.We are having two types of assertions Hard Assertion and SoftAssertions.

Hard Assertions:

 Hard Assertion in Testng are used for validation.when we apply hardassertion in a testcase if our testcase fails then it stops the execution. Means Execution of Testcase Aborted.

In the below code we are asserting the text.So we have asserted two times. 1st time assertion fails because Expected Text is given wrong intentionally.So  assertion fails test execution will not continue  because of HardAssertion . 2nd assertion's Expected Text is given correct. But it will Never Execute.

Below is the Sample Code For HardAssertion.



package assertions;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;

public class HardAssertions {

String Actualtext;
WebDriver driver = new FirefoxDriver();
@Test
// In this method, If any assertion fails then execution will be aborted.
public void hard_assert_text() 
{
Actualtext = driver.findElement(By.xpath("http://executeprogramming.blogspot.in/")).getText();
//Below Assertion the expected text is written wrong.
Assert.assertEquals(Actualtext, "ExecuteProgra","1st assert Failed.");
System.out.println("Hard Assertion : 1st Assertion executed.");

//This below code will never execute in hard assertion.
//above assert will fail.testcase aborted.
Assert.assertEquals(Actualtext, "ExecuteProgramming","2nd assert Pass.");
System.out.println("Hard Assertion 2nd Assertion executed.");

}
@AfterClass
public void Closebrowser() {
driver.quit();
}
}

Output:

FAILED: hard_assert_text



Soft Assertions:

            Soft Assertion in Testng are also  for validation.But difference in softassertion over hardassertion is when we apply softassertion in a testcase if our testcase fails then should not  abort the execution. Means Execution of Testcase should continue till end.

In the below code we are asserting the text.So we have asserted two times. 1st time assertion fails because Expected Text is given wrong intentionally.Even though assertion fails test execution continues because of SoftAssertion Class. 2nd assertion will pass as Expected Text is given correct. so Assertion will pass.

Below is the Sample Code For SoftAssertion.

package assertions;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;



public class Softassertions {

// Creating Softassert class
SoftAssert s_assert = new SoftAssert();

String Actualtext;
WebDriver driver = new FirefoxDriver();

@BeforeClass
public void load_url() {
driver.manage().window().maximize();
driver.get("http://executeprogramming.blogspot.in/");
}

@Test
public void soft_assert_text()
                 {

Actualtext = driver.findElement(By.xpath("//h1[@class='title']")).getText();


//Below Assertion the expected text is written wrong.
s_assert.assertEquals(Actualtext, "Execute","1st Assertion will Fail.");
System.out.println("Soft Assertion : 1st  assertion is executed.");

         

//Below Assertion the expected text is written correct.
s_assert.assertEquals(Actualtext, "ExecuteProgramming ","2nd Assertion will Pass.");
System.out.println("Soft Assertion : 2nd  assertion is executed.");


}

@AfterClass
public void Closebrowser()
               {

                     driver.quit();

}

}

Output:

Soft Assertion : 1st  assertion is executed.
Soft Assertion : 2nd  assertion is executed.

FAILED: soft_assert_text








No comments:

Post a Comment