Skip to main content

Assert Functions

Assert functions check a condition and record the result in the playback log. When the condition is false the step is reported as a failure, and what happens next depends on the assert type in effect: by default the script stops at that step (see Verify Functions for setAssertType and the Fail / FailAtEnd / Warning behaviours).

API summary

APIParametersFails when
assertExists(accessor)accessorthe element is not on the page
assertNotExists(accessor)accessorthe element is on the page
assertTrue(value)valuethe value evaluates to false
assertFalse(value)valuethe value evaluates to true
assertNull(value)valuethe value is not null
assertNotNull(value)valuethe value is null
assertEqual / assertEquals(A, B)A, Bthe two values are not equal
assertNotEqual / assertNotEquals(A, B)A, Bthe two values are equal
assertGreaterThan(first, second)first, secondfirst is not greater than second
assertLessThan(first, second)first, secondfirst is not lesser than second
assertGreaterOrEqual(first, second)first, secondfirst is not greater than or equal to second
assertLessOrEqual(first, second)first, secondfirst is not lesser than or equal to second
assertRange(number, min, max)number, min, maxthe number is outside the range
assertContains(fullText, snippet)fullText, snippetfullText does not contain snippet
assertDoesNotContain(fullText, snippet)fullText, snippetfullText contains snippet
assertContainsText(accessor, text)accessor, expected text or regular expressionthe element text does not contain the expected value
assertDoesNotContainText(accessor, text)accessor, expected text or regular expressionthe element text contains the expected value
assertTextPresent(accessor, text)accessor, expected textthe expected text is not present
assertTextNotPresent(accessor, text)accessor, expected textthe expected text is present
assertSelectHasOption(accessor, option)accessor, optionthe select has no such option
assertSelectMissingOption(accessor, option)accessor, optionthe select has that option

Never use an assert inside an if condition. Assert APIs return no value and throw an exception when they fail, so the branch cannot be evaluated. To decide a branch, use a value-returning API such as getText, getValue, getAttribute, getBrowserURL or isVisible, or switch to the verify equivalent.

Detailed reference and examples

The sections below list each assert API with its aliases, its description and a working example. The underscored form and the plain form are interchangeable — both are accepted in a script.

_assertExists = assertExists

Description:
Asset Exists will log the step in the playback logs when the element exists, it will log as a failure if the element does not exist.
Note: Do not use this in an if condition as it does not return any value, and throws an exception if the assertion fails.

Example:

navigateTo("http://demosite.appvance.com/");
wait(1000);
focus(span("Products[1]"));
assertExists(span("Products[1]"));

_assertNotExists

Parameters: value
Description:
Assert Not Exists will log failures in the logs if the element does exist on the page. This assertion is to check if certain elements that we are looking for does not exist on the page.
Note: Do not use this in an if condition as it does not return any value, and throws an exception if the assertion fails.

Example:

navigateTo("http://demosite.appvance.com/");
wait(1000);
assertNotExists(span("Products[1]"));
click(link("Clothing"));

_assertTrue = assertTrue

Parameters: value
Description: Assert True will log failures in the logs if the condition evaluates to false.
Example:

navigateTo("http://sahitest.com/demo/training/login.htm");
var val2=_isVisible(_bold("Sahi Training Site"));
assertTrue(val2);

_assertFalse = assertFalse

Parameters: value
Description:
Assert False will log failures in the logs if the condition evaluates to true.
Note: Do not use this in an if condition as it does not return any value, and throws an exception if the assertion fails.

Example:

navigateTo("http://sahitest.com/demo/training/login.htm");
var val1=_getText(_bold("Sahi Training Site"));
assertDoesNotContainText(log(val1);
assertEqual(val1,"Sahi Training Site");
assertNotEqual(val1,"abc");
assertFalse("abc");
var val2=_isVisible(_bold("Sahi Training Site"));
assertTrue(val2);

_assertNull = assertNull

Parameters: value
Description:
Assert Null will log failures to the logs if the value is not null and will continue to playback if the value is null.
Note: Do not use this in an if condition as it does not return any value, and throws an exception if the assertion fails.

Example:

navigateTo("http://sahitest.com/demo/training/login.htm");
assertExists(bold("Sahi Training Site"));
assertNull(null);

_assertNotNull = assertNotNull

Parameters: value
Description:
Assert Not Null will log failure in the logs if the value is null.
Note: Do not use this in an if condition as it does not return any value, and throws an exception if the assertion fails.

Example:

navigateTo("http://sahitest.com/demo/training/login.htm");
assertExists(bold("Sahi Training Site"));
assertNotNull("Sahi Training Site");

_assertEqual = assertEqual = _assertEquals = assertEquals

Parameters: value1, value2
Description:
Assert Equal checks if the expected value is equal to the Actual value.
AssertEqual will log failures to the playback logs if the expected and actual values are not equal. It logs the step when the assertion passes in the logs section.
Note: Do not use this in an if condition as it does not return any value, and throws an exception if the assertion fails.

Example:

navigateTo("http://demosite.appvance.com/");
click(span("Products[1]"));
assertEqual(span("Home[1]"),"Home");
click(link("Clothing"));

_assertNotEqual = assertNotEqual

Parameters: value1, value2
Description:
As the name suggests, Assert not equal expects the string to be not equal and logs failures in the playback logs when the Expected string matches with the actual string.
Note: Do not use this in an if condition as it does not return any value, and throws an exception if the assertion fails.

Example:

navigateTo("http://demosite.appvance.com/");
click(span("Products[1]"));
assertNotEqual(span("Home[1]"),"Home");
click(link("Clothing"));

_assertContainsText = assertContainsText

Parameters: fullText, element
Description:
As the name suggests, assert contains text checks for a string in the target element and logs success or failure.
It will log the step details if the assertion passes else it logs as a failure if the expected string or reg-ex is not part of the text content of a given element with a proper explanation and stops the script playback.
Note: Do not use this in an if condition as it does not return any value, and throws an exception if the assertion fails.

Example:

navigateTo("http://demosite.appvance.com/");
click(span("Products[1]"));
assertContainsText("Home",span("Home[1]"));
click(link("Clothing"));

_assertDoesNotContainText = assertDoesNotContainText

Parameters: fullText, element
Description: assertContainsText will do nothing if the expected string/regex is not part of the text content of given element _assertContainsText will log failures to the playback logs if the expected string/regex is part of the text content of given element
Example:

navigateTo("http://demosite.appvance.com/");
click(span("Products[1]"));
assertDoesNotContainText(click(link("Clothing"));