forked from DevExpress/testcafe
/
test.js
179 lines (149 loc) · 6.55 KB
/
test.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const path = require('path');
const { expect } = require('chai');
const isCI = require('is-ci');
const config = require('../../config');
const { createReporter } = require('../../utils/reporter');
const testInfo = require('./test-info');
if (config.useLocalBrowsers) {
describe('Concurrency', function () {
let data = '';
function resolvePath (file) {
return path.join(__dirname, file);
}
function run (browsers, concurrency, files, reporter) {
let src = null;
reporter = reporter || 'json';
if (typeof files === 'string')
src = resolvePath(files);
else {
src = files.map(function (file) {
return resolvePath(file);
});
}
return testCafe
.createRunner()
.src(src)
.reporter(reporter, {
write: function (newData) {
data += newData;
},
end: function (newData) {
data += newData;
},
})
.browsers(browsers)
.concurrency(concurrency)
.run();
}
function createConnections (count) {
const connections = [];
function createConnection () {
return testCafe.createBrowserConnection();
}
function addConnection (connection) {
connections.push(connection);
return connections;
}
let promise = Promise.resolve();
for (let i = 0; i < count; i++) {
promise = promise
.then(createConnection)
.then(addConnection);
}
return promise;
}
const customReporter = createReporter({
reportTestDone: function (name) {
this.write('Test ' + name + ' done').newline();
},
reportFixtureStart: function (name) {
this.write('Fixture ' + name + ' started').newline();
},
});
const slowReporter = createReporter({
reportTaskStart: async function () {
await new Promise(resolve => setTimeout(() => resolve(), 100));
this.write('Task start').newline();
},
reportTestStart: async function () {
this.write('Test start').newline();
},
});
beforeEach(function () {
data = '';
});
afterEach(() => {
testInfo.delete();
});
it('Should run tests sequentially if concurrency = 1', function () {
return run('chrome:headless --no-sandbox', 1, './testcafe-fixtures/sequential-test.js')
.then(() => {
expect(testInfo.getData()).eql(['long started', 'long finished', 'short started', 'short finished']);
});
});
it('Should run tests concurrently if concurrency > 1', function () {
return run('chrome:headless --no-sandbox', 2, './testcafe-fixtures/concurrent-test.js')
.then(() => {
expect(testInfo.getData()).eql(['test started', 'test started', 'short finished', 'long finished']);
});
});
// TODO: this test doesn't work on CI due to big resource demands
if (!isCI) {
it('Should run tests concurrently in different browser kinds', function () {
return run(['chrome:headless --no-sandbox', 'chrome:headless --no-sandbox --user-agent="TestAgent"'], 2, './testcafe-fixtures/multibrowser-concurrent-test.js')
.then(() => {
const timelineData = testInfo.getData();
expect(Object.keys(timelineData).length).gt(1);
for (const browserTimeline of Object.values(timelineData))
expect(browserTimeline).eql(['test started', 'test started', 'short finished', 'long finished']);
});
});
}
if (!config.proxyless) {
// TODO: stabilize test on Firefox
(config.hasBrowser('firefox') ? it.skip : it)('Should run tests concurrently with Role', function () {
return run('chrome:headless --no-sandbox', 2, './testcafe-fixtures/role-test.js')
.then(() => {
expect(testInfo.getData()).eql(['/fixtures/concurrency/pages/first-page.html', '/fixtures/concurrency/pages/second-page.html']);
});
});
}
it('Should report fixture start correctly if second fixture finishes before first', function () {
return run('chrome:headless --no-sandbox', 2, ['./testcafe-fixtures/multifixture-test-a.js', './testcafe-fixtures/multifixture-test-b.js'], customReporter)
.then(failedCount => {
expect(failedCount).eql(0);
expect(data.split('\n')).eql([
'Fixture Multifixture A started',
'Test Long test done',
'Fixture Multifixture B started',
'Test Short test done',
'',
]);
});
});
it('Should not start any test before report task start finishes', function () {
return run('chrome:headless --no-sandbox', 2, './testcafe-fixtures/concurrent-test.js', slowReporter)
.then(failedCount => {
expect(failedCount).eql(0);
expect(data.split('\n')).eql([
'Task start',
'Test start',
'Test start',
'',
]);
});
});
it('Should fail if number of remotes is not divisible by concurrency', function () {
return createConnections(3)
.then(function (connections) {
return run(connections, 2, './testcafe-fixtures/concurrent-test.js');
})
.then(function () {
throw new Error('Promise rejection expected');
})
.catch(function (error) {
expect(error.message).eql('The number of remote browsers should be divisible by the concurrency factor.');
});
});
});
}