Commit f3dc5d2
authored
## Purpose
In some cases, you need to create custom actions that contain multiple
default actions or other custom actions.
## Approach
Provide the ability to describe and register custom actions in a JS
configuration file. Create a **TestController.customActions** property
containing these actions. If the user function returns a value, the
result of calling the user action will be that value. If the function
does not return a value, the result will be a TestController object,
which you can chain further.
## API
### Define Custom Actions
```js
// JS Configuration file
module.exports = {
customActions: {
async makeSomething (selector) {
await this.click(selector);
},
async getSelectorValue (selector) {
return await Selector(selector).innerText;
},
}
}
```
### Use Custom Actions
```js
test('Check span value', async t => {
const spanValue = await t.customActions.getSelectorValue('#result');
await t.expect(spanValue).eql('OK');
});
test('Click the button and check span value', async t => {
const spanValue = await t.customActions.makeSomething()
.customActions.getSelectorValue('#result');
await t.expect(spanValue).eql('OK');
});
```
### Reporter Changes (For Dashboard team):
- runCustomAction is a command that fires **reportTestActionStart**
**before** any inner action starts and fires **reportTestActionDone**
**after** the latest inner action fineshed.
- you can identify each custom action run using **command.actionId**
property.
- in the case of concurrency mode you can also use testRunId.
- you can also access the result of the runCustomAction command(the
value returned by the custom action) in the **reportTestActionDone**
function using **command.actionResult** property.
An example of reporter:
```js
const customActionsStack = {}
const shouldReportInnerActions = false;
function isCustomAction (name /*or type */) {
return name ==='runCustomAction';
// or return type === 'run-custom-action';
}
{
reportTestActionStart: (actionName, { command, testRunId }) => {
if(isCustomAction(actionName /* or command.type */ ) ) {
const { actionId, name } = command;
customActionsStack[testRunId].push({ actionId, name }) ;
}
},
reportTestActionDone: (name, { command, testRunId }) => {
if( isCustomAction(actionName /* or command.type */ ) ) {
customActionsStack[testRunId].pop();
const { actionResult, actionId, name } = command;
// Do something with actionResult
return;
}
if (!shouldReportInnerActions && customActionsStack[testRunId].length) {
// Do not report action
} else {
// Report action
}
}
}
```
## References
Closes #1535
## Pre-Merge TODO
- [x] Write tests for your proposed changes
- [x] Make sure that existing tests do not fail
- [x] Make sure that the documentation link is correct in the Error
template
1 parent 5534e34 commit f3dc5d2
File tree
27 files changed
+521
-87
lines changed- src
- api
- test-controller
- configuration
- errors
- runtime
- reporter/command
- runner
- test-run
- commands
- test
- functional
- fixtures/custom-actions
- pages
- testcafe-fixtures
- server
- data/custom-actions
27 files changed
+521
-87
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
63 | | - | |
| 63 | + | |
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
| |||
0 commit comments