3.2 AssertionEngine
3.2.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
You can do this:
*** Test Cases ***
Example 2
Get Text [name="name"] == Prefilled Name
- Allows using different operators, like
equals,containsandmatches
Example 3
Get Text [name="name"] contains Name
- Performs an automatic retry if the received value does not match what was expected. If the element is found but it does not contain the expected value, the keyword will perform automatic retry and will search the element again.
Example this will fail:
Example 4
Get Text [name="name"] contains Not Here
By opening the log.html file, one can see from the logging that the keyword performed multiple retries.
Start the test application in a separate shell with command:
node test_app/server/server.js
Run the example with command:
robot --outputdir output --loglevel debug examples/3/3.2/example.robot
3.2.2 AssertionEngine formatters
Assertion formatters allow performing simple operations, such as normalize spaces, strip, case insensitive, and apply to expected,
on the returned value. If apply to expected is defined, then the formatter is also applied
to the expected value. Multiple formatters can be applied at the same time. The keyword also returns the old scope.
Formatters also support scopes. If scope is not defined,
the default scope is Global, which means the formatter is valid forever. Other scopes are Suite and Test.
Assertion Formatters
TRY
Get Text [name="name"] contains ${SPACE*4}Prefilled Name${SPACE*3}
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
3.2.3 Assertion with Python plugins
Assertion operators can also be used with Python plugins, but assertions cannot be used with JavaScript plugins, because assertions exist only on the Python side. Using assertions is shown in the 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