Selenium Assertions

Sergei Terekhov

Written by Sergei Terekhov | Monday, 20 May 2013

command tells Selenium what to do. There are three 'types' of Selenium commands: Actions, Accessors and Assertions. Each command must contain command name, target and value: 

Accessors examine the state of the application and store the results in variables, e.g. "storeText". They are also used to generate Assertions automatically. 

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

"WaitFor" commands waits until some condition become true. When the condition becomes already true, test will succeed. If the condition has not become true within a specified time-out, test will be considered as failed. 

If "verify" command is failed the test script continues to run with the entry in the error log. 

If"assert" command is failed the test script is aborted. 

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.

comments powered by Disqus