2.3 AssertionEngine
2.3.1 AssertionEngine Introduction
AssertionEngine provides three different features.
- Allows inline assertions with
Get...keywords. Instead of doing this:
*** Test Cases ***
Example 1
${text} Get Text [name="name"]
Should Be Equal ${text} Prefilled Name
One can do this:
*** Test Cases ***
Example 2
Get Text [name="name"] == Prefilled Name
- Allows usage different operators, like
equals,containsandmatches
Example 3
Get Text [name="name"] contains Name
- Performs automatic retry if received value does not match what was expected If element is found, but it did not contain the expected value keyword will perform automatic retry and will search the element again.
Example this will fail:
Example 4
Get Text [name="name"] contains Not Here
But by opening the log.html file, on can see from logging that keyword did perform multiple retries.
Start test application in separate shell with command:
node test_app/server/server.js
Run example with commands:
robot --outputdir output --loglevel debug examples/3/3.2/example.robot
2.3.2 AssertionEngine formatters
Assertion formatters allows to perform simple operations, normalize spaces, strip, case insensitive and apply to expected
operations to the returned value. If apply to expected is defined, then formatter is also applied
to the expected value. Multiple formatters can be applied in same time. Keyword also returns the old scope.
Formatters also support scopes. If scope is not defined,
default scope if Global, which means formatter is valid forever. Other scopes are Suite and Test.
Assertion Formatters
TRY
Get Text [name="name"] contains ${SPACE*4}Prefilled Name${SPACE*3}
<ExamplesExplorer path="3/3.2" title="Chapter 3.2 Examples" />
EXCEPT Text 'Prefilled Name' (str) should contain* type=GLOB
No Operation
END
${old_scope} = Set Assertion Formatters {"Get Text": ["strip", "apply to expected"]}
Get Text [name="name"] contains ${SPACE*4}Prefilled Name${SPACE*3}
Log ${old_scope}
Run example with command:
robot --outputdir output --loglevel debug examples/3/3.2/assertion_formatters.robot
2.3.3 Assertion with Python plugins
Assertion operators can be also used with Python plugins, but assertions can not be used with JavaScript plugins, because assertion only exist in the Python side. Using assertion is show in following example:
@keyword
def get_protocol(
self,
assertion_operator: Optional[AssertionOperator] = None,
assertion_expected: Optional[str] = None,
message: Optional[str] = None,
) -> str:
protocol = self.library.evaluate_javascript(None, f"window.location.protocol")
logger.info(f"Protocol: {protocol}")
return verify_assertion(
protocol, assertion_operator, assertion_expected, message
)
And then used like this:
Get Protocol == https:
Run the full example with command:
robot --outputdir output --loglevel debug examples/3/3.2/PythonPluginExample.robot