Let us know so we can improve!
Arguments
Use arguments to pass data to methods.
Argument Types
This table shows the syntax for declaring a non-literal argument with a name and type. Instead of using a combination of name and type, it’s also possible to use a hardcoded (literal) value for an argument of these types.
| Name/type (non literal) | Value/type (literal) | |
|---|---|---|
| string primitive | { "name": "myArg", "type": "string" } | { "value": "myString" } |
| integer primitive | { "name": "myArg", "type": "number" } | { "value": 1024 } |
| boolean primitive | { "name": "myArg", "type": "boolean" } | { "value": false } |
| locator | { "name": "myArg", "type": "locator" } | { "value": { "css": ".myClass" }, "type": "locator" } |
| element reference | not supported | { "type": "elementReference", "value": "myElement" } |
| page object type | { "name": "myArg", "type": "pageObject" } | { "value": "my/page/object", "type": "pageObject" } |
| root page object type | { "name": "myArg", "type": "rootPageObject" } | { "value": "my/page/object", "type": "rootPageObject" } |
| frame element | { "name": "myArg", "type": "frame" } | same as element reference |
| function | not supported | { "type": "function", "predicate": [...] } |
Argument Reference
To access an argument passed to a compose method in multiple statements, declare a reusable argument at the method level. The args property must be a sibling of the name property.
For example, we declare a reusable passwordStr argument at the method level:
1{
2 "name": "passwordStr",
3 "type": "string"
4}Then in each compose statement where you want to access the reusable argument, set the argument’s type to argumentReference, the name of an argument with "type": "argumentReference" must match the name of one of the reusable arguments declared at the method level:
1{
2 "name": "passwordStr",
3 "type": "argumentReference"
4}Here’s the full method declaration. The statements that apply actions to the password and passwordConfirmation elements both declare a reference to the reusable passwordStr argument. When test code invokes this method, the passwordStr argument is passed to both statements:
1{
2 "name": "confirmPassword",
3 "args": [
4 {
5 "name": "passwordStr",
6 "type": "string"
7 }
8 ],
9 "compose": [
10 {
11 "element": "password",
12 "apply": "clearAndType",
13 "args": [
14 {
15 "name": "passwordStr",
16 "type": "argumentReference"
17 }
18 ]
19 },
20 {
21 "element": "passwordConfirmation",
22 "apply": "clearAndType",
23 "args": [
24 {
25 "name": "passwordStr",
26 "type": "argumentReference"
27 }
28 ]
29 }
30 ]
31}A reusable argument can’t be an argument with a hardcoded value, also known as a literal type.
Note
For a working example of argument references, see this tutorial.
Element Reference Argument Type
The elementReference type in an argument allows you to reference an existing element as a parameter value. This example uses an elementReference to a targetElement element.
1{
2 "elements": [
3 {
4 "name": "sourceElement",
5 "selector": {
6 "css": ".source"
7 },
8 "type": ["draggable"]
9 },
10 {
11 "name": "targetElement",
12 "selector": {
13 "css": ".source"
14 }
15 }
16 ],
17 "methods": [
18 {
19 "name": "composeDragAndDrop",
20 "compose": [
21 {
22 "element": "sourceElement",
23 "apply": "dragAndDrop",
24 "args": [
25 {
26 "type": "elementReference",
27 "value": "targetElement"
28 }
29 ]
30 }
31 ]
32 }
33 ]
34}Generated JavaScript code:
1// declaration
2composeDragAndDrop(): Promise<void>;
3
4// implementation
5async composeDragAndDrop() {
6 const _statement0 = await this.__getSourceElement();
7 await _statement0.dragAndDrop(await this.__getTargetElement());
8}If a referenced element has a selector or filter arguments, those values are automatically inferred and added to the method declaration.
1{
2 "name": "target",
3 "selector": {
4 "css": "foo:nth-child(%d)",
5 "args": [
6 {
7 "name": "index",
8 "type": "number"
9 }
10 ]
11 }
12}When the element is referenced in a compose statement, this page object hard codes the value of 1 for an index.
1{
2 "element": "source",
3 "apply": "dragAndDrop",
4 "args": [
5 {
6 "value": "target",
7 "type": "elementReference",
8 "args": [
9 {
10 "value": 1
11 }
12 ]
13 }
14 ]
15}Let us know so we can improve!