Skip to content
Get Free Trial

Assertion Functions

Assertions compare runtime behaviour against expected behaviour. Assertions are used for "verification" or "checking" of functionality

Assertions are an integral part of Sahi scripts. A normal script would contain a series of actions followed by verification of functionality.

Since
Sahi Pro 3.5
Desktop Engine 7.0.0
Mobile Engine 7.5.0
SAP Engine 9.0.0

Modes BrowserWindowsJavaAndroidiOSSAP

_assert($condition[, $message])

Arguments

NameTypeDescription
$conditionbooleancondition to verify
$messagestringoptionalmessage to log if condition fails

Returns

null

_assert will do nothing if the condition passed to it is true.
_assert will log failures to the test run reports if the condition evaluates to false.

Since
Sahi Pro 3.5
Desktop Engine 7.0.0
Mobile Engine 7.5.0
SAP Engine 9.0.0

Modes BrowserWindowsJavaAndroidiOSSAP

_assertTrue($condition[, $message])

Arguments

NameTypeDescription
$conditionbooleancondition to verify
$messagestringoptionalmessage to log if condition fails

Returns

null

Same as _assert

Since
Sahi Pro 3.5
Desktop Engine 7.0.0
Mobile Engine 7.5.0
SAP Engine 9.0.0

Modes BrowserWindowsJavaAndroidiOSSAP

_assertNotTrue($condition[, $message])

Arguments

NameTypeDescription
$conditionbooleancondition which is expected to evaluate to false
$messagestringoptionalmessage to log if condition evaluates to true

Returns

null

_assertNotTrue will do nothing if the condition evaluates to false.
_assertNotTrue will log failures to the test run reports if the condition evaluates to true.

Since
Sahi Pro 3.5
Desktop Engine 7.0.0
Mobile Engine 7.5.0
SAP Engine 9.0.0

Modes BrowserWindowsJavaAndroidiOSSAP

_assertFalse($condition[, $message])

Arguments

NameTypeDescription
$conditionbooleancondition which is expected to evaluate to false
$messagestringoptionalmessage to log if condition evaluates to true

Returns

null

_assertFalse will do nothing if the condition evaluates to false.
_assertFalse will log failures to the test run reports if the condition evaluates to true.

Since
Sahi Pro 3.5
Desktop Engine 7.0.0
Mobile Engine 7.5.0
SAP Engine 9.0.0

Modes BrowserWindowsJavaAndroidiOSSAP

_assertNull($value[, $message])

Arguments

NameTypeDescription
$valueanythingAnything to be checked for null
$messagestringoptionalmessage to log if value is not null

Returns

null

_assertNull will do nothing if the value passed is null.
_assertNull will log failures to the test run reports if the value is not null.

Since
Sahi Pro 3.5
Desktop Engine 7.0.0
Mobile Engine 7.5.0
SAP Engine 9.0.0

Modes BrowserWindowsJavaAndroidiOSSAP

_assertNotNull($value[, $message])

Arguments

NameTypeDescription
$valueanythingAnything to be checked for not null
$messagestringoptionalmessage to log if value is null

Returns

null

_assertNotNull will do nothing if the value passed is not null.
_assertNotNull will log failures to the test run reports if the value is null.

Since
Sahi Pro 3.5
Desktop Engine 7.0.0
Mobile Engine 7.5.0
SAP Engine 9.0.0

Modes BrowserWindowsJavaAndroidiOSSAP

_assertExists($element[, $message])

Arguments

NameTypeDescription
$elementHTML DOM elementelement to be checked for existence
$messagestringoptionalmessage to log if element does not exist on page

Returns

null

_assertExists will do nothing if the element exists.
_assertExists will log failures to the test run reports if the element does not exist

Since
Sahi Pro 6.0.0
Desktop Engine 7.0.0
Mobile Engine 7.5.0
SAP Engine 9.0.0

Modes BrowserWindowsJavaAndroidiOSSAP

_assertVisible($element[, $message])

Arguments

NameTypeDescription
$elementHTML DOM elementelement to be checked for visibility.
$messagestringoptionalmessage to log if element is not visible.

Returns

null

_assertVisible will do nothing if the element is visible.
_assertVisible will log failures to the test run reports if the element is not visible

Since
Sahi Pro 6.0.0
Desktop Engine 7.0.0
Mobile Engine 7.5.0
SAP Engine 9.0.0

Modes BrowserWindowsJavaAndroidiOSSAP

_assertNotVisible($element[, $message])

Arguments

NameTypeDescription
$elementHTML DOM elementelement to be checked for not visible.
$messagestringoptionalmessage to log if element is visible.

Returns

null

_assertNotVisible will do nothing if the element is not visible.
_assertNotVisible will log failures to the test run reports if the element is visible

Since
Sahi Pro 3.5
Desktop Engine 7.0.0
Mobile Engine 7.5.0
SAP Engine 9.0.0

Modes BrowserWindowsJavaAndroidiOSSAP

_assertNotExists($element[, $message])

Arguments

NameTypeDescription
$elementHTML DOM elementelement to be checked for non existence
$messagestringoptionalmessage to log if element exists on page

Returns

null

_assertNotExists will do nothing if the element does not exist.
_assertNotExists will log failures to the test run reports if the element exists

Since
Sahi Pro 3.5
Desktop Engine 7.0.0
Mobile Engine 7.5.0
SAP Engine 9.0.0

Modes BrowserWindowsJavaAndroidiOSSAP

_assertEqual($expected, $actual[, $message])

Arguments

NameTypeDescription
$expectedanyExpected value. This value can be any normal javascript data type or an array Regular expressions can also be used. (eg. "/del/")
$actualanyActual value. This value can be any normal javascript data type or an array. It is mostly some attribute of a HTML DOM element.
$messagestringoptionalMessage to be written to logs if the expected and actual values are not equal

Returns

null

_assertEqual will do nothing if the expected and actual values match.
_assertEqual will log failures to the test run reports if the expected and actual values are not equal

Since
Sahi Pro 3.5
Desktop Engine 7.0.0

Modes BrowserWindowsJava

_assertEqualArrays($expected, $actual[, $message])

Arguments

NameTypeDescription
$expectedarrayExpected array.
$actualarrayActual array.
$messagestringoptionalMessage to be written to logs if the expected and actual values are not equal

Returns

null

Asserts that expected and actual arrays are equal, else logs the message.

Since
Sahi Pro 3.5
Desktop Engine 7.0.0
Mobile Engine 7.5.0
SAP Engine 9.0.0

Modes BrowserWindowsJavaAndroidiOSSAP

_assertNotEqual($expected, $actual[, $message])

Arguments

NameTypeDescription
$expectedanyExpected value. This value can be any normal javascript data type or an array Regular expressions can also be used. (eg. "/del/")
$actualanyActual value. This value can be any normal javascript data type or an array. It is mostly some attribute of a HTML DOM element.
$messagestringoptionalMessage to be written to logs if the expected and actual values are equal

Returns

null

_assertNotEqual will do nothing if the expected and actual values do not match.
_assertNotEqual will log failures to the test run reports if the expected and actual values are equal

Since
Sahi Pro 3.5
Desktop Engine 7.0.0
Mobile Engine 7.5.0
SAP Engine 9.0.0

Modes BrowserWindowsJavaAndroidiOSSAP

_assertContainsText($expected, $element[, $message])

Arguments

NameTypeDescription
$expectedstringExpected string. Regular expressions can also be used. (eg. "/del/")
$elementHTML DOM elementElement. We check if expected is a part of the text of this element.
$messagestringoptionalMessage to be written to logs if the expected string is not part of the text of given element

Returns

null

_assertContainsText will do nothing if the expected string/regex is part of the text content of given element.
_assertContainsText will log failures to the test run reports if the expected string/regex is not part of the text content of given element

Since
Sahi Pro 3.5
Desktop Engine 7.0.0
Mobile Engine 7.5.0
SAP Engine 9.0.0

Modes BrowserWindowsJavaAndroidiOSSAP

_assertNotContainsText($expected, $element[, $message])

Arguments

NameTypeDescription
$expectedstringExpected string. Regular expressions can also be used. (eg. "/del/")
$elementHTML DOM elementElement. We check if expected is not a part of the text of this element.
$messagestringoptionalMessage to be written to logs if the expected string is part of the text of given element

Returns

null

_assertNotContainsText will do nothing if the expected string/regex is not part of the text content of given element.
_assertNotContainsText will log failures to the test run reports if the expected string/regex is part of the text content of given element

Since
Sahi Pro 6.0.0
Desktop Engine 7.5.0
Mobile Engine 7.5.0
SAP Engine 9.0.0

Modes BrowserWindowsJavaAndroidiOSSAP

_assertEqualImages($expectedImagePath, $actualImagePath[, $differenceThreshold[, $displayOnSuccess[, $failureMessage]]])

Arguments

NameTypeDescription
$expectedImagePathstringExpected image path. Relative paths will be resolved relative to current executing script.
$actualImagePathstringActual image path. Relative paths will be resolved relative to current executing script.
$differenceThresholdfloatoptionalIf the difference between the two images is lesser than or equal to the differenceThreshold number, they are considered to be same.
$displayOnSuccessbooleanoptionalWhen true, the expected image, actual image and the differential image will be embedded in the playback log, if the assertion succeeds.
Default is false, which means only dissimilar images will be logged. Note that these images will always be embedded in case of assertion failure.
$failureMessagestringoptionalMessage to be written to logs if the image comparison fails

Returns

null

_assertEqualImages compares two images. If the images are similar, assertion passes. If the images are dissimilar, the images and the failure messasge will be displayed in logs as failure.

Since
Sahi Pro 6.3.0
Desktop Engine 8.0.0

Modes BrowserWindowsJava

_assertHorizontallyAligned($leftElement, $rightElement[, $position[, $gap[, $offset[, $message]]]])

Arguments

NameTypeDescription
$leftElementHTML DOM elementLeft element to be asserted.
$rightElementHTML DOM elementRight element to be asserted.
$positionstringoptionalPosition to be checked. By default position = "middle". Value of position can be either "top" or "bottom" or "both" or "middle".
$gapinteger array|integeroptionalGap between right edge of left element and left edge of right element. If gap is an integer, it will take the exact value. And if gap is an array, Eg. gap = [10, 40], implies gap can range from 10 to 40. To force sahi to check for the gap between the two elements, use the gap. If nothing provided it wouldn't consider gap.
$offsetinteger array|integeroptionalOffset above and below within which to look for elements boundary for given position. If offset is an integer, both above and below offsets are set to this offset. rightElement may be slightly above or below leftElement. Sahi normally matches only elements whose bases match. To force it to look a little above or below while looking to the right, use the offset.
$messagestringoptionalMessage to be written to logs if the assertion fails

Returns

null

Horizontally Aligned

_assertHorizontallyAligned checks if two elements are horizontally aligned. If the elements are horizontally aligned, assertion passes. If the elements are not horizontally aligned, the failure messasge will be displayed in logs as failure.

Example

_assertHorizontallyAligned(_div("item1"), _div("item2")) //by default middle position will be checked
_assertHorizontallyAligned(_div("item1"), _div("item2"), "top")
_assertHorizontallyAligned(_div("item1"), _div("item2"), "top", 20) //gap can take the values between [-20, 20]
_assertHorizontallyAligned(_div("item1"), _div("item2"), "top", [0, 20]) //gap is only positive i.e. no overlapping is allowed
_assertHorizontallyAligned(_div("item1"), _div("item2"), "top", [0, 20], 10) //gap is only positive i.e. no overlapping is allowed and offset for the top edge is 10
Since
Sahi Pro 6.3.0
Desktop Engine 8.0.0

Modes BrowserWindowsJava

_assertVerticallyAligned($topElement, $bottomElement[, $position[, $gap[, $offset[, $message]]]])

Arguments

NameTypeDescription
$topElementHTML DOM elementTop element to be asserted.
$bottomElementHTML DOM elementBottom element to be asserted.
$positionstringoptionalPosition to be checked. By default position = "center". Value of position can be either "left" or "right" or "both" or "center".
$gapinteger array|integeroptionalGap between bottom edge of top element and top edge of bottom element. If gap is an integer, it will take the exact value. And if gap is an array, Eg. gap = [0, 40], implies gap can range from 0 to 40. To force sahi to check for the gap between the two elements, use the gap. If nothing provided it wouldn't consider gap.
$offsetinteger array|integeroptionalOffset left and right within which to look for elements boundary for given position. If offset is an integer, both left and right offsets are set to this offset. topElement may be slightly left or right to the bottomElement.
$messagestringoptionalMessage to be written to logs if the assertion fails

Returns

null

Vertically Aligned

_assertVerticallyAligned checks if two elements are vertically aligned. If the elements are vertically aligned, assertion passes. If the elements are not vertically aligned, the failure messasge will be displayed in logs as failure.

Example

_assertVerticallyAligned(_div("item1"), _div("item2")) //by default center position will be checked
_assertVerticallyAligned(_div("item1"), _div("item2"), "left")
_assertVerticallyAligned(_div("item1"), _div("item2"), "left", 20) //gap can take the values between [-20, 20]
_assertVerticallyAligned(_div("item1"), _div("item2"), "left", [0, 20]) //gap is only positive i.e. no overlapping is allowed
Since
Sahi Pro 6.3.0
Desktop Engine 8.0.0

Modes BrowserWindowsJava

_assertCentered($element, $containerElement, $alignment[, $message])

Arguments

NameTypeDescription
$elementHTML DOM elementInner element to be asserted.
$containerElementHTML DOM elementContainer element to be asserted.
$alignmentstringCan be "vertical" or "horizontal".
$messagestringoptionalMessage to be written to logs if the assertion fails

Returns

null

isCentered

_assertCentered checks if inner element is vertically or horizontally centered to the container element. If the elements are vertically or horizontally aligned, assertion passes. If the elements are not vertically or horizontally aligned, the failure messasge will be displayed in logs as failure.

Example

_assertCentered(_div("item1"), _div("item2"), "vertical")
_assertCentered(_div("item1"), _div("item2"), "horizontal")
Since
Sahi Pro 6.3.0
Desktop Engine 8.0.0

Modes BrowserWindowsJava

_assertVisiblyInside($element1, $element2[, $message])

Arguments

NameTypeDescription
$element1HTML DOM elementFirst element to be asserted.
$element2HTML DOM elementSecond element to be asserted.
$messagestringoptionalMessage to be written to logs if the assertion fails

Returns

null

_assertVisiblyInside checks if element1 is visibly inside element2. If element1 is visibly inside element2, assertion passes. If element1 is not visibly inside element2, the failure messasge will be displayed in logs as failure.

Since
Sahi Pro 6.3.0
Desktop Engine 8.0.0

Modes BrowserWindowsJava

_assertVisiblyOutside($element1, $element2[, $message])

Arguments

NameTypeDescription
$element1HTML DOM elementFirst element to be asserted.
$element2HTML DOM elementSecond element to be asserted.
$messagestringoptionalMessage to be written to logs if the assertion fails

Returns

null

_assertVisiblyOutside checks if element1 is visibly outside element2 i.e one is not contained within other. If element1 is visibly outside element2, assertion passes. If element1 is not visibly outside element2, the failure messasge will be displayed in logs as failure.

Since
Sahi Pro 6.3.0
Desktop Engine 8.0.0

Modes BrowserWindowsJava

_assertVisiblyOn($element1, $element2[, $topSpace[, $rightSpace[, $bottomSpace[, $leftSpace[, $message]]]]])

Arguments

NameTypeDescription
$element1HTML DOM elementFirst element to be asserted.
$element2HTML DOM elementSecond element to be asserted. Base Element.
$topSpaceinteger array|integeroptionalSpace between top edges of the two elements. If topSpace is an integer, it will take the exact value. And if topSpace = [-20, 0], implies top edge of element2 lies above top edge of element1. To force sahi to check for the topSpace between the two elements, use the topSpace. If nothing provided it wouldn't consider topSpace.
$rightSpaceinteger array|integeroptionalSpace between right edges of the two elements. If rightSpace is an integer, it will take the exact value. And if rightSpace = [0, 20], implies right edge of element2 lies to the right side of right edge of element1. To force sahi to check for the rightSpace between the two elements, use the rightSpace. If nothing provided it wouldn't consider rightSpace.
$bottomSpaceinteger array|integeroptionalSpace between bottom edges of the two elements. If bottomSpace is an integer, it will take the exact value. And if bottomSpace = [0, 20], implies bottom edge of element2 lies below bottom edge of element1. To force sahi to check for the bottomSpace between the two elements, use the bottomSpace. If nothing provided it wouldn't consider bottomSpace.
$leftSpaceinteger array|integeroptionalSpace between left edges of the two elements. If leftSpace is an integer, it will take the exact value. And if leftSpace = [0, 20], implies top edge of element2 lies to the right side of left edge of element1. To force sahi to check for the leftSpace between the two elements, use the leftSpace. If nothing provided it wouldn't consider leftSpace.
$messagestringoptionalMessage to be written to logs if the assertion fails

Returns

null

isVisiblyOn

_assertVisiblyOn checks if element1 placed on element2. element1 may extend beyond element2 boundaries. If element1 is visibly placed on element2, assertion passes. If element1 is not visibly placed on element2, the failure messasge will be displayed in logs as failure.

Example

_assertVisiblyOn(_div("item1"), _div("item2"), [0,25]); //implies _div("item1") is placed on _div("item2") where top edge of div("item1") can be upto
// 25 units below div("item2") top edge
Since
Sahi Pro 6.3.0
Desktop Engine 8.0.0

Modes BrowserWindowsJava

_assertFitsWidth($element1, $element2[, $leftSpace[, $rightSpace[, $message]]])

Arguments

NameTypeDescription
$element1HTML DOM elementFirst element to be asserted.
$element2HTML DOM elementSecond element to be asserted. Base Element.
$leftSpaceinteger array|integeroptionalSpace between left edges of the two elements. If leftSpace is an integer, it will take the exact value. And if leftSpace = [0, 20], implies top edge of element2 lies to the right side of left edge of element1. If nothing provided leftSpace will be considered as 0.
$rightSpaceinteger array|integeroptionalSpace between right edges of the two elements. If rightSpace is an integer, it will take the exact value. And if rightSpace = [0, 20], implies right edge of element2 lies to the right side of right edge of element1. If nothing provided rightSpace will be considered as 0.
$messagestringoptionalMessage to be written to logs if the assertion fails

Returns

null

isFitsWidth

_assertFitsWidth checks if element1 fits within the width of element2. If element1 fits within element2, assertion passes. If element1 does not fits within element2, the failure messasge will be displayed in logs as failure.

Example

_assertFitsWidth(_div("item1"), _div("item2"), [0,25], [-25,0]); //implies _div("item1") is on _div("item2") where left edge of div("item1") can be
//upto 25 units right to div("item2") left edge and right edge of div("item1") can be upto 25 units left to div("item2") right edge
Since
Sahi Pro 6.3.0
Desktop Engine 8.0.0

Modes BrowserWindowsJava

_assertFitsHeight($element1, $element2[, $topSpace[, $bottomSpace[, $message]]])

Arguments

NameTypeDescription
$element1HTML DOM elementFirst element to be asserted.
$element2HTML DOM elementSecond element to be asserted. Base Element.
$topSpaceinteger array|integeroptionalSpace between top edges of the two elements. If topSpace is an integer, it will take the exact value. And if topSpace = [-20, 0], implies top edge of element2 lies above top edge of element1. If nothing provided topSpace will be considered as 0.
$bottomSpaceinteger array|integeroptionalSpace between bottom edges of the two elements. If bottomSpace is an integer, it will take the exact value. And if bottomSpace = [0, 20], implies bottom edge of element2 lies below bottom edge of element1. If nothing provided bottomSpace will be considered as 0.
$messagestringoptionalMessage to be written to logs if the assertion fails

Returns

null

isFitsHeight

_assertFitsHeight checks if element1 fits within the height of element2. If element1 fits within element2, assertion passes. If element1 does not fits within element2, the failure messasge will be displayed in logs as failure.

Example

_assertFitsHeight(_div("item1"), _div("item2"), [0,25], [-25,0]); //implies _div("item1") is on _div("item2") where top edge of div("item1") can be
//upto 25 units below div("item2") top edge and bottom edge of div("item1") can be upto 25 units above div("item2") bottom edge