-
-
Notifications
You must be signed in to change notification settings - Fork 737
/
Copy pathtryTo.js
104 lines (100 loc) · 2.4 KB
/
tryTo.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
const recorder = require('../recorder');
const store = require('../store');
const { debug } = require('../output');
const defaultConfig = {
registerGlobal: true,
};
/**
*
*
* Adds global `tryTo` function inside of which all failed steps won't fail a test but will return true/false.
*
* Enable this plugin in `codecept.conf.js` (enabled by default for new setups):
*
* ```js
* plugins: {
* tryTo: {
* enabled: true
* }
* }
* ```
* Use it in your tests:
*
* ```js
* const result = await tryTo(() => I.see('Welcome'));
*
* // if text "Welcome" is on page, result => true
* // if text "Welcome" is not on page, result => false
* ```
*
* Disables retryFailedStep plugin for steps inside a block;
*
* Use this plugin if:
*
* * you need to perform multiple assertions inside a test
* * there is A/B testing on a website you test
* * there is "Accept Cookie" banner which may surprisingly appear on a page.
*
* #### Usage
*
* #### Multiple Conditional Assertions
*
* ```js
*
* Add assert requires first:
* ```js
* const assert = require('assert');
* ```
* Then use the assert:
* const result1 = await tryTo(() => I.see('Hello, user'));
* const result2 = await tryTo(() => I.seeElement('.welcome'));
* assert.ok(result1 && result2, 'Assertions were not succesful');
* ```
*
* ##### Optional click
*
* ```js
* I.amOnPage('/');
* tryTo(() => I.click('Agree', '.cookies'));
* ```
*
* #### Configuration
*
* * `registerGlobal` - to register `tryTo` function globally, true by default
*
* If `registerGlobal` is false you can use tryTo from the plugin:
*
* ```js
* const tryTo = codeceptjs.container.plugins('tryTo');
* ```
*
*/
module.exports = function (config) {
config = Object.assign(defaultConfig, config);
if (config.registerGlobal) {
global.tryTo = tryTo;
}
return tryTo;
};
function tryTo(callback) {
let result = false;
return recorder.add('tryTo', () => {
recorder.session.start('tryTo');
callback();
recorder.add(() => {
result = true;
recorder.session.restore('tryTo');
return result;
});
recorder.session.catch((err) => {
result = false;
const msg = err.inspect ? err.inspect() : err.toString();
debug(`Unsuccessful try > ${msg}`);
recorder.session.restore('tryTo');
return result;
});
return recorder.add('result', () => {
return result;
}, true, false);
}, false, false);
}