Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

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");  
      }  
 }  

2 comments:

  1. You neglected to mention Assert.Fail, which is useful when you want to supplant the assertion logic and conditionally trigger the same assertion failure exception. For example, if some sporadic error _might_ occur during a particular operation, you _might_ want to generate an assertion failure (e.g. in the context of a "positive" test where an error is NOT expected) or not (e.g. in the context of a negative test where an error IS expected), so you could write something like this:

    if (failOnError)
    Assert.Fail(errorText)
    else
    System.out.println(errorText);

    ReplyDelete
    Replies
    1. Thanks a lot on the feedback and contributing to blog

      Delete