-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
92 lines (66 loc) · 2.13 KB
/
index.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
const createTestCafe = require('testcafe');
class User {
constructor (name, fileName, browser) {
this.name = name;
this.fileName = fileName;
this.browser = browser;
this.promiseOfInit = new Promise(resolve => {
this.confirmCurrentStep = resolve;
});
this.expectedStageName = void 0;
this.runExpectedStage = null;
this._runTest();
}
async _runTest () {
const testcafe = await createTestCafe('localhost', 0, 0);
const runner = testcafe.createRunner();
await runner
.src(this.fileName)
.browsers(this.browser)
.run();
testcafe.close();
}
async runStage (stageName) {
if (stageName !== this.expectedStageName) {
throw new Error(`Another stage was expected:
expected: ${this.expectedStageName}
trying run: ${stageName}`);
}
this.runExpectedStage();
return new Promise(resolve => {
this.confirmCurrentStep = resolve;
});
}
}
const scenarios = global.scenarios || new Map();
global.scenarios = scenarios;
class Scenario {
constructor (description) {
scenarios.set(description, this);
this.users = new Map();
}
createUser (name, fileName, browser) {
const user = new User(name, fileName, browser);
this.users.set(name, user);
return user.promiseOfInit;
}
initUser (name) {
const user = this.users.get(name);
if (!user)
throw new Error(`The user named '${name}' does not exist`);
return function stage (stageName) {
user.confirmCurrentStep(user);
user.expectedStageName = stageName;
return new Promise(resolve => {
user.runExpectedStage = resolve;
});
};
}
}
function getScenario (description) {
const scenario = scenarios.get(description);
if (scenario)
return scenario;
throw new Error(`The scenario '${description}' does not exist`);
}
module.exports = { Scenario, getScenario };