This blog explains the usage of assert statements in Selenium.

assertTrue() in Selenium WebDriver and its implementation in WebDriver scripts.

Being a Automation tester/Engineer, the word assert 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 blog will give an essence of how can we use “assetTrue” in different context while creating test scripts.
Before discussing “assertTrue” and its applicability, let’s have a look at its origination.

Selenium Assert Statements - Class Diagram

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.

assert2

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 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.

Selenium Assert Statements - 3

Syntax:


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

Syntax:

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());

Share this:

Leave a Reply

Your email address will not be published. Required fields are marked *