Home » Software Testing » Automation Testing » Selenium WebDriver Tutorial » assertTrue(message,condition) in Selenium WebDriver and it’s implementation

assertTrue(message,condition) in Selenium WebDriver and it’s implementation

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 .

[the_ad_placement id=”incontent”]

assert1

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.

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:

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:

  • 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:

  • 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:

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:

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:
[the_ad_placement id=”incontent”]

  • 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:

  • getAttribute() Method

Description:  This method returns the value of the attribute whose key is specified .  Syntax:

  • Using Select

Description:  Helps to select and de-select options in the dropdowns.

assert3

Syntax:

  • isDisplayed() and isEnabled() in conjunction 

Syntax:

Furthermore, there can be numerous other similar combinations.

[the_ad_placement id=”incontent”]

1 thought on “assertTrue(message,condition) in Selenium WebDriver and it’s implementation”

  1. the above images are not getting loaded
    ie., assert1
    aseert2
    not able to understand anything because of this problem..

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top
Scroll to Top
%d bloggers like this: