[selenium-users] Types of Assertions in TestNG

40 views
Skip to first unread message

phaneendra

unread,
Jun 24, 2016, 9:32:07 AM6/24/16
to seleniu...@googlegroups.com
Hello Team,


May I know, what are the different types of Assertions in TestNG and how to use them?


Thanks in Advance...



--
Cheers,
Phaneendra


Shawn McCarthy

unread,
Jun 24, 2016, 9:53:03 AM6/24/16
to Selenium Users
This question is probably better for the testng-users group, and not Selenium Users.

However, there is assertTrue, assertFalse, assertEquals, assertNull, assertNotNull, and some more I believe.

Vijay Sharma

unread,
Jun 27, 2016, 1:22:59 AM6/27/16
to Selenium Users


Hi from my kowledge uptill now i have found out the below mentioned  information on assertion

Assertions are like Accessors, but they verify that the state of the application is as expected. All Selenium Assertions can be used in 3 modes: "assert", "verify", and "waitFor".



Let’s look at the most frequently used “assert” command.

assertTitle(pattern) 

Check the title of the page (by comparing the expected).

Argument: expected page title.

Returns: TRUE if page title matches to the expected, FALSE otherwise.

If title does not matche to the expected the test will fail. 

assertElementPresent(locator)

Verify that the specified element is somewhere on the page.

Arguments: locator - an element locator

.

Returns: TRUE if the element is present, FALSE otherwise.

It verifies that the specified element is somewhere on the page, if element wasn’t found, test will fail.

assertChecked(locator)

Check that the specified element is checked or not.

Arguments: locator - an element locator pointing to a checkbox or radio button.

Returns: TRUE if the checkbox is checked, FALSE otherwise.

If the specified element doesn't exist or isn't a toggle-button, test will fail. 

assertAlert(pattern)

Check the message of a JavaScript alert generated during the previous action and click OK.

Arguments: expected message text.

Returns: TRUE if the alert message matches to the pattern, FALSE otherwise. 

Retrieve the message of a JavaScript alert generated during the previous action and check that the alert message matches to the pattern and click OK on alert, test will fail if there were no alerts or message does not match the pattern.

If an alert is generated but you do not consume it with getAlert, the next Selenium action will fail. Under Selenium, JavaScript alerts will NOT pop up a visible alert dialog. 

Selenium does NOT support JavaScript alerts that are generated in a page's onload() event handler. In this case a visible dialog WILL be generated and Selenium will hang until someone clicks OK manually. 

assertCookieByName(name, pattern)

It verifies that the specified cookie has specified value.

Arguments: name - the name of the cookie, pattern - expected value of the cookie.

Returns: TRUE if the value of specified cookie matches to the pattern, FALSE otherwise. 

It receives the value of specified cookie and compares it with the pattern, if the value does not match or cookie is not present, test will fail. Many of “assert” commands have a pair like “assertnot”. Difference between this commands is only that: if “assertnot” command returns true, test will failed. Here is only a small part of selenium “assert” commands. If you want to learn more you can read Selenium Command Reference (http://release.seleniumhq.org/selenium-core/1.0.1/reference.html). Also you can type first letters of any command in Selenium IDE command input to see possible variants, select one and see description on reference tab.

Reply all
Reply to author
Forward
0 new messages