Text capture
I capture <textRef> as <name>
Captures the referenced text and binds it to the given name in the global scope.
I capture <textRef> as <name>
Where
<textRef>is the name of the binding containing the source text- Can be the name of any binding that contains or resolves to a text value, including a web element in which case the text in the element will be dynamically retrieved and used.
<name>is the name to bind the captured value to
Example
Given the username is "Gwen"
When I capture the username as my username
Then my username should be the username
I capture <element>
Captures the text value of an element and binds it to the element name in the global scode.
I capture <element>
I capture <element> as <name>
Captures the text value of an element and binds it to the given name in the global scope.
I capture <element> as <name>
I capture <name> <of|on|in> <element> by <javascript|js> "<script>"
Executes a JavaScript expression or function on an element and binds the result to the given name.
I capture <name> <of|on|in> <element> by <javascript|js> "<script>"
Where
<name>is the name to bind the javascript return value to<of|on|in>isof,onorin<element>is the name of the element to execute the javascript on<javascript|js>isjavascript(orjs, since v2.46.0)<script>is the javascript expression or function that will return the value (can be in DocString position)- The reference to the element will be provided to the javascript through an implicit object named
element. - For a one liner expression, no function, arrow declaration or
returnprefix is required For a function, use either:
- Arrow syntax (preferred):
() => { body }(Since v3.56.0) - Or anonymous function syntax (legacy):
(function(){ body })()
- Arrow syntax (preferred):
- The reference to the element will be provided to the javascript through an implicit object named
Example
Given the red checkbox can be located by id "red"
When I navigate to "https://automationintesting.com/selenium/testpage"
And I click the red checkbox
And I capture my favorite color in the red checkbox by js
"""
() => {
var color = element.value
if (!element.checked) color = "Blue"
return color;
}
"""
Then my favorite color should be "Red"