assertTrue() is one of the most popular and frequently used assertion method while creating Selenium Scripts. Being an Automation tester/Engineer, the word assertTrue(Message, Condition) comes every now and then and for that matter of fact is almost used in every script whenever we intent to “Check the presence of an element on a webpage”. Thus considering it’s importance, it has become an integral part of our testing routines/activities. In literal terms, the word can be interpreted as “to state to be true” and the same fundamental is brought into play while embedding assertions in the test scripts. Thus this post will give an essence of how can we use “assetTrue(condition)” in various context while creating test scripts. Before discussing “assertTrue(condition)” and its applicability, let’s have a look at its origination .
Assertion tool class is a part of org.testng and extends java.lang.Object class. This class presents a wide variety of static methods with a specific parameter order. To name a few, there are methods like assertTrue(condition), assertFalse(condition), assertEquals(actualValue, ExpectedValue), assertNull(object) and many more. assertTrue(boolean condition) In a very simple language it asserts that a given condition is true.
If the given condition isn’t true, an AssertionError, with the given message is thrown.
public static void assertTrue(java.lang.String message, boolean condition)
Parameters: condition – the condition to evaluate message – the assertion error message Import statements: For using assertTrue() in our test scripts, we need to import it in the test script using the following syntax:
import static org.junit.Assert.assertTrue;
Conditions and Messages: assertTrue(“Assertion Failed: Message”, boolean condition)
- String equals() Method
Description: This method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object. Syntax:
assertTrue("Verification failed: Element1 and Element2 are not same.",Element1.equals(driver.findElement(By.id(Element2 )).getText()));
- String equalsIgnoreCase() Method
Description: This method compares this String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length, and corresponding characters in the two strings are equal ignoring case. Syntax:
assertTrue("Verification Failed: Element1 and Element2 are not same.",(driver.findElement(By.xpath(Element1 )).getText().equalsIgnoreCase(Element2)));
- String substring() Method
Description: This method has two variants and returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string or up to endIndex – 1 if second argument is given. Syntax:
String name=”String Operation.AssertStatement”; assertTrue(“Verification Failed: The name is not ”String” Operation”.”,driver.findElement(By.id("name")).getAttribute("value").equals(name.substring(0, name.lastIndexOf("."))));
Output: String Operation
- String replaceAll() Method
Description: This method replaces each character of this string that matches the given regular expression with the given replacement. Syntax:
assertTrue(“Verification Failed: Message is not displayed correctly on the webpage.”,driver.findElement(By.id("message")).getText().replaceAll("[\t\n\r]", " ").equals("Testing Message is displayed correctly"));
Other than these, there are several other string operation methods available and can be effectively used in conjunction with assertTrue(). Some of the other commonly used methods are:
- isSelected() Method
Description: This method checks if the specified object is selected. The result is true if and only if the object is selected. Syntax:
assertTrue("Verification Failed: The radio button of male status is not selected for the user", driver.findElement(By.id(“gender”)).isSelected());
- getAttribute() Method
Description: This method returns the value of the attribute whose key is specified . Syntax:
assertTrue("Verification Failed: Data filled in Language field is not correct. Please check naming convention. ",driver.findElement(By.id("list_lg")).getAttribute("value").equals(language));
- Using Select
Description: Helps to select and de-select options in the dropdowns.
assertTrue("Verification Failed: Data filled in font field is not correct. Please check naming convention. ",driver.findElement(By.id("list_lg")).getAttribute("value").equals(“Georgia, Times New Roman, Times, serif”));
- isDisplayed() and isEnabled() in conjunction
assertTrue(“Verification Failed: Either element1 is not being displayed or element2 is not enabled.”,driver.findElement(By.id("element1")).isDisplayed() && driver.findElement(By.id("element42")).isEnabled());
Furthermore, there can be numerous other similar combinations.