Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts
MindMap for Automation testing
Continuing TestNG test execution post assertion failure
Assertions are used to validate if a condition holds true. In previous article, we discuss on how to create assertion in TestNG.
See: How to work with Assertions in TestNG tests.
Assertion added in previous article were hard assertion, i.e : current test execution stops once an assertion fails and the next test in the test suite is executed while using hard assertions. When we use import org.testng.Assert library, the test execution stops once an error is encountered. This is useful to stop an test execution in case of critical defect in the test, and move to next test in the suite.
Example of creating multiple test in explained in the below TestNG.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite1">
<test name="Test1">
<classes>
<class name="testNG.assertionVal"/>
</classes>
</test>
<test name="Test2">
<classes>
<class name="testNG.NewTest1"/>
</classes>
</test>
</suite>
There can be scenario where we do not want to stop test execution due to a minor validation defect. In that scenario we can use the concept of soft assertion, i.e the test case execution will continue although the test will be marked as failed in the final results but will be executed until the test flow is completed or an hard assertion failure condition occurs.
Below points are important to note from current article:
- There are two types of assertion Hard assertion and soft assertion.
- Hard assertion use import org.testng.Assert library
- Soft assertion use import org.testng.asserts.SoftAssert library
- Step softas.assertAll() needs to be added to display the validation failure from soft assertion failure
Below code explains how to use soft assertion in the test.
package testNG;
import org.testng.Assert;
import org.testng.annotations.Test;
//soft assertion will use the below softAssert class
import org.testng.asserts.SoftAssert;
public class assertionVal {
// Create an soft assertion object softas
SoftAssert softas = new SoftAssert();
@Test
public void assertVal()
{
//Create an assertion similar to hard assertion as shown below
softas.assertEquals("aaaa", "bb");
softas.assertEquals("aa","aaccc","asserts exists");
// this is an example of hard assertion, test will stop execution
//at this step in case error is encountered.
Assert.assertEquals("aa", "aa");
System.out.println("nitin");
//This step is very important as without this step,
//no soft assertion failure will be reported in test result.
softas.assertAll();
}
}
How to work with Assertions in TestNG tests
Assertions are added in TestNG to validate whether a condition is true or false and report in test results the execution state of condition. For example in selenium test for login, we provide valid username and password and login, we expect the title to be displayed correctly in the page. Assertions are used in code, where we need to validate the state of an object properties and fail the test in case expected conditions are not met.
Different type of assertions:
- Assert.AssertEquals(Expected condition, Actual
condition) – This compares the expected condition with actual condition and
fails the test in case the assertion fails.Different object types can be
compared using Assert.AssertEquals
- Assert.AssetEquals(Expected condition, Actual
condition, Message)- This compares the expected condition with actual condition
and fails the test in case the assertion fails displaying the message as
defined while calling the assertion.
- Assert.assertFalse(Boolean condition) - Asserts
that a condition is false
- Assert.assertFalse(Boolean condition, String
Message )- Asserts
that a condition is false. If it isn't, an Assertion Error, with the given
message, is thrown.
- Assert.assertTrue(Boolean condition, String
Message )- Asserts
that a condition is true. Assertion fails if condition is not true and string
message is displayed.
- Assert.AssertNull(object) – Validates if a
assertion is null.
There are various other assertion which we can add in TestNG test. The issue with assertion is case execution stops in case an assertion fails. So if we do not want to stop a test on failure and continue with test execution, using assert statement will not work, instead we can wrap the assert function to verify the test condition and continue with test execution. We will discuss in future article for how to use soft assertion in the test scripts.
Below code explains how to use different assert in TestNG tests.
package testNG;
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class assertionVal {
WebDriver driver;
@BeforeTest
public void setup()
{
File file = new File("D:\\selenium\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
driver = new InternetExplorerDriver();
}
@Test
public void assertVal()
{
driver.navigate().to("http://qaautomationqtp.blogspot.com");
Assert.assertEquals("Learning Automation Concepts in QTP", driver.getTitle());
//Assert to compare two values and reporting a message in case validation fails
Assert.assertEquals("aaa", "aaa", "this is the first test and value does not match");
//Assert to validate a boolean condition as false
Assert.assertFalse(driver.getTitle().contentEquals("Learning Automation Concepts in QTP"));
//Assert to validate a boolean condition as true
Assert.assertFalse(driver.getTitle().contentEquals("Learning Automation Concepts in QTP"));
//Assertion to validate an object is not null.
Assert.assertNotNull("driver");
}
}
Subscribe to:
Posts (Atom)