TestNG Asserts

 TestNG Asserts:


Assertion will help us to check whether test case is passed or failed by comparing actual and expected result.

Here if testng assert is failed it won't execute next step.

Syntax for TestNG Assertions:

Assert.Method( actual, expected)


The parameter as you see contains three values:

  • Actual: The actual value that the tester gets like if the tester’s assertion is on the title of the page then what was the actual title of the page goes here.
  • Expected: The value that you expect like if the tester’s assertion is on the title of the page then what value of title do you expect goes here.
@Test (priority = 1)
public void OpenBrowser() {
        String expectedTitle = "Helping Testers";
        String originalTitle = "HelpingTesters";
        Assert.assertEquals(originalTitle, expectedTitle);
  }



Use Message as a Parameter in TestNG Asserts?

In the above example, you may notice that an AssertionError is thrown by Java which is quite complete in its own form. But, in TestNG asserts, we can mention a message as well in the parameter that will be displayed when the assertion fails along with the assertion error. So, as a slight modification to the syntax, this will work as a third parameter making syntax as follows:

Assert.Method( actual, expected, message)

The parameter as you see contains three values:

  • Actual: The actual value that the tester gets like if the tester’s assertion is on the title of the page then what was the actual title of the page goes here.
  • Expected: The value that you expect like if the tester’s assertion is on the title of the page then what value of title do you expect goes here.
  • Message: A string message to display only in case of an error when the assert fails. 
@Test (priority = 1)
public void OpenBrowser() {
        String expectedTitle = "Helping Testers";
        String originalTitle = "HelpingTesters";
        Assert.assertEquals(originalTitle, expectedTitle);
          Assert.assertEquals(originalTitle, expectedTitle, "Provided text do not match");
}

Different types of Asserts in TestNG

There are two types of TestNg Assert:

  • Hard Assert
  • Soft Assert

 

Hard Assert in TestNG

Hard Asserts are those asserts that stop the test execution when an assert statement fails, and the subsequent assert statements are therefore not validated. It plays a vital role in projects where we have an element without whose validation, asserting other elements is useless. One good example in such cases is the login functionality. If I want to see my past orders, for example, then what is the point of checking this test case when the login validation already failed? Hard asserts are the default type of asserts in TestNG, and what we used in the previous section was Hard Assert.

 

Soft Assert in TestNG

Soft asserts are just the opposite of hard asserts. In soft asserts, the subsequent assertions keep on running even though one assert validation fails, i.e., the test execution does not stop. Soft assert does not include by default in TestNG. For this, you need to include the package org.testng.asserts.SoftassertSo, when should we use soft asserts in TestNG? We use soft asserts when we do not care about the failure of specific validations and want the test execution to proceed and also want to see the exception errors.

A good example is multiple validations on an input form. Also, to note that on many platforms, you will see “verify” while learning about asserts. Soft asserts are also known as “Verify” and hence do not get confused about the same.

 

Usage of Soft Assert in TestNG:

The following code will demonstrate the use of soft asserts in TestNG. In this code, we are validating the title of the web page, bypassing two different expected titles.


@Test (priority = 1)
public void OpenBrowser() {
        
        SoftAssert softassert = new SoftAssert();
        String expectedTitle = "Helping Testers";
        String originalTitle = "HelpingTesters";
        softassert.assertEquals(originalTitle, expectedTitle);
        softassert.assertEquals("P", "P"); softassert.assertEquals("q", "P"); softassert.assertAll();
}



 Important points to remember concerning soft assert that we can notice in the above code are:

  • Soft assert requires the external import of the package import org.testng.asserts.SoftAssert;.
  • An object of the SoftAssert runs the assert statements.
  • The object should have a life within the same test method in which we declared it.
  • object.assertAll() statement is required to see the exceptions; otherwise, the tester won’t know what passed and what failed.

 

It shows that our assertions executed and which ones failed. But the test execution did not stop.

 

 

Commonly used TestNG Assert Methods

All the TestNG Assert statements work in the same vein when we talk about the basic structure of its execution. But, like any other method, it can take different parameters and perform various types of validations in the test case methods. Since asserts are so crucial in TestNG and used so commonly, we will discuss some of the most common assertion methods.

  • Assert.assertEqual(String actual, String expected): Pass the actual string value and the expected string value as parameters. Validates if the actual and expected values are the same or not.
  • Assert.assertEqual(String actual, String expected, String message): Similar to the previous method just that when the assertion fails, the message displays along with the exception thrown.
  • Assert.assertEquals(boolean actual, boolean expected): Takes two boolean values as input and validates if they are equal or not.
  • Assert.assertTrue(condition): This method asserts if the condition is true or not. If not, then the exception error is thrown.
  • Assert.assertTrue(condition, message):  Similar to the previous method with an addition of message, which is shown on the console when the assertion fails along with the exception.
  • Assert.assertFalse(condition): This method asserts if the condition is false or not. If not, then it throws an exception error.
  • Assert.assertFalse(condition, message): Similar to the previous method but with an addition of a message string which is shown on the console when the assertion fails, i.e., the condition is true.
  • public static void assertEquals ( Object actual, Object expected, String message): Asserts whether the two objects passed are equal or not. If not, the message and the exception error appears. The message parameter is optional.
  • public static void assertEquals (String actual, String expected, String message): Asserts whether two strings are equal or not. If not, the message along with the exception error displays. The message parameter is optional.



No comments:

Post a Comment

Scroll iOS application using Appium

When creating test scripts for iOS native app we need to perform scroll to perform any other action on particular element  Here we will get ...