This repository was archived by the owner on Sep 21, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathrunner.test.js
More file actions
357 lines (285 loc) Β· 11.9 KB
/
Copy pathrunner.test.js
File metadata and controls
357 lines (285 loc) Β· 11.9 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
'use strict';
var q = require('q'),
sinon = require('sinon'),
createSuite = require('../lib/suite').create,
State = require('../lib/state'),
Config = require('../lib/config'),
Runner = require('../lib/runner'),
CONFIG_TEXT = [
'rootUrl: http://example.com',
'gridUrl: http://grid.example.com',
'browsers: ',
' - browser',
].join('\n');
function addState(suite, name) {
var state = new State(suite, name, function() {});
suite.addState(state);
}
describe('runner', function() {
beforeEach(function() {
this.sinon = sinon.sandbox.create();
var browser = {
fullName: 'browser',
createActionSequence: this.sinon.stub().returns({
perform: this.sinon.stub().returns(q.resolve())
}),
open: this.sinon.stub().returns(q.resolve()),
buildElementsMap: this.sinon.stub().returns(q.resolve()),
captureState: this.sinon.stub().returns(q.resolve()),
quit: this.sinon.stub().returns(q.resolve())
};
this.browser = browser;
this.launcher = {
launch: function() {
return browser;
},
stop: function() {}
};
this.suite = new createSuite('suite');
this.suite.url = '/path';
this.runner = new Runner(new Config('/root', CONFIG_TEXT), this.launcher);
});
afterEach(function() {
this.sinon.restore();
});
describe('run', function() {
it('should emit `begin` event when tests start', function() {
var spy = this.sinon.spy();
this.runner.on('begin', spy);
return this.runner.run([]).then(function() {
sinon.assert.calledOnce(spy);
});
});
it('should emit `beginSuite` event for each suite', function() {
var spy = this.sinon.spy();
this.runner.on('beginSuite', spy);
return this.runner.run([this.suite]).then(function() {
sinon.assert.calledWith(spy, 'suite');
});
});
it('should call `before` hook with action sequence and find function', function() {
var stub = this.sinon.stub(this.suite, 'beforeHook'),
sequence = {
stub: true,
perform: this.sinon.stub().returns(q.resolve())
};
this.browser.createActionSequence.returns(sequence);
addState(this.suite, 'state');
return this.runner.run([this.suite]).then(function() {
sinon.assert.calledWith(stub, sequence, require('../lib/find-func').find);
});
});
it('should peroform before sequence ', function() {
var sequence = { perform: this.sinon.stub().returns(q())};
this.browser.createActionSequence.returns(sequence);
addState(this.suite, 'state');
return this.runner.run([this.suite]).then(function() {
sinon.assert.called(sequence.perform);
});
});
it('should emit `beginState` for each suite state', function() {
var spy = this.sinon.spy();
addState(this.suite, 'state');
this.runner.on('beginState', spy);
return this.runner.run([this.suite]).then(function() {
sinon.assert.calledWith(spy, 'suite', 'state', 'browser');
});
});
it('should not emit `beginState` if state is skipped', function() {
var spy = this.sinon.spy();
this.suite.addState({
name: 'state',
suite: this.suite,
shouldSkip: this.sinon.stub().returns(true)
});
this.runner.on('beginState', spy);
return this.runner.run([this.suite]).then(function() {
sinon.assert.notCalled(spy);
});
});
it('should emit `skipState` if state is skipped', function() {
var spy = this.sinon.spy();
this.suite.addState({
name: 'state',
suite: this.suite,
shouldSkip: this.sinon.stub().returns(true)
});
this.runner.on('skipState', spy);
return this.runner.run([this.suite]).then(function() {
sinon.assert.calledWith(spy, 'suite', 'state', 'browser');
});
});
it('should not emit `skipState` if state is not skipped', function() {
var spy = this.sinon.spy();
this.suite.addState({
name: 'state',
suite: this.suite,
shouldSkip: this.sinon.stub().returns(false)
});
this.runner.on('skipState', spy);
return this.runner.run([this.suite]).then(function() {
sinon.assert.notCalled(spy);
});
});
it('should launch each browser in config', function() {
this.runner.config.browsers = [
{name: 'browser1', version: '1'},
{name: 'browser2'}
];
addState(this.suite, 'state');
this.sinon.spy(this.launcher, 'launch');
return this.runner.run([this.suite]).then(function() {
sinon.assert.calledWith(this.launcher.launch, this.runner.config.browsers[0]);
sinon.assert.calledWith(this.launcher.launch, this.runner.config.browsers[1]);
}.bind(this));
});
it('should launch browser only once for suite', function() {
addState(this.suite, 'state1');
addState(this.suite, 'state2');
this.sinon.spy(this.launcher, 'launch');
return this.runner.run([this.suite]).then(function() {
sinon.assert.calledOnce(this.launcher.launch);
}.bind(this));
});
it('should launch browser second time if there is a second suite', function() {
var secondSuite = createSuite('second');
secondSuite.url = '/hello';
addState(this.suite, 'state');
addState(secondSuite, 'state');
this.sinon.spy(this.launcher, 'launch');
return this.runner.run([this.suite, secondSuite]).then(function() {
sinon.assert.calledTwice(this.launcher.launch);
}.bind(this));
});
it('should open suite url in browser', function() {
addState(this.suite, 'state');
return this.runner.run([this.suite]).then(function() {
sinon.assert.calledWith(this.browser.open, 'http://example.com/path');
}.bind(this));
});
it('should capture state in browser', function() {
addState(this.suite, 'state');
return this.runner.run([this.suite]).then(function() {
sinon.assert.calledWith(this.browser.captureState,
sinon.match.instanceOf(State).and(sinon.match.has('name', 'state')));
}.bind(this));
});
it('should emit `endState` for each suite state', function() {
var spy = this.sinon.spy();
addState(this.suite, 'state');
this.runner.on('endState', spy);
return this.runner.run([this.suite]).then(function() {
sinon.assert.calledWith(spy, 'suite', 'state', 'browser');
});
});
it('should not emit `endState` if state is skipped', function() {
var spy = this.sinon.spy();
this.suite.addState({
name: 'state',
suite: this.suite,
shouldSkip: this.sinon.stub().returns(true)
});
this.runner.on('endState', spy);
return this.runner.run([this.suite]).then(function() {
sinon.assert.notCalled(spy);
});
});
it('should execute next state only after previous has been finished', function() {
addState(this.suite, 'state1');
addState(this.suite, 'state2');
var endState = this.sinon.spy(),
beginState = this.sinon.spy();
this.runner.on('endState', endState);
this.runner.on('beginState', beginState);
return this.runner.run([this.suite]).then(function() {
sinon.assert.callOrder(
endState.withArgs('suite', 'state1'),
beginState.withArgs('suite', 'state2')
);
});
});
it('should emit `endSuite` for each suite', function() {
var spy = this.sinon.spy();
this.runner.on('endSuite', spy);
return this.runner.run([this.suite]).then(function() {
sinon.assert.calledWith(spy, 'suite');
});
});
it('should also run child suites automatically', function() {
var spy = this.sinon.spy();
createSuite('child', this.suite);
this.runner.on('beginSuite', spy);
return this.runner.run([this.suite]).then(function() {
spy.secondCall.args.must.eql(['child']);
});
});
it('should finish parent suite only after all children', function() {
var spy = this.sinon.spy();
createSuite('child', this.suite);
this.runner.on('endSuite', spy);
return this.runner.run([this.suite]).then(function() {
sinon.assert.callOrder(
spy.withArgs('child'),
spy.withArgs('suite')
);
});
});
it('should execute next suite only after previous has been finished', function() {
var nextSuite = createSuite('next'),
endSuite = sinon.spy(),
beginSuite = sinon.spy();
nextSuite.url = '/path2';
this.runner.on('endSuite', endSuite);
this.runner.on('beginSuite', beginSuite);
return this.runner.run([this.suite, nextSuite]).then(function() {
sinon.assert.callOrder(
endSuite.withArgs('suite'),
beginSuite.withArgs('next')
);
});
});
it('should allow to run a suite without url and states', function() {
var beginSuite = sinon.spy(),
endSuite = sinon.spy(),
suite = createSuite('suite');
this.runner.on('beginSuite', beginSuite);
this.runner.on('endSuite', endSuite);
return this.runner.run([suite]).then(function() {
sinon.assert.calledWith(beginSuite, 'suite');
sinon.assert.calledWith(endSuite, 'suite');
});
});
it('should emit `end` after all suites', function() {
var spy = this.sinon.spy();
this.runner.on('end', spy);
return this.runner.run([this.suite]).then(function() {
sinon.assert.calledWith(spy);
});
});
it('should emit events in correct order', function() {
var begin = this.sinon.spy(),
beginSuite= this.sinon.spy(),
beginState = this.sinon.spy(),
endState = this.sinon.spy(),
endSuite= this.sinon.spy(),
end = this.sinon.spy();
addState(this.suite, 'state');
this.runner.on('begin', begin);
this.runner.on('beginSuite', beginSuite);
this.runner.on('beginState', beginState);
this.runner.on('endState', endState);
this.runner.on('endSuite', endSuite);
this.runner.on('end', end);
return this.runner.run([this.suite]).then(function() {
sinon.assert.callOrder(
begin,
beginSuite,
beginState,
endState,
endSuite,
end
);
});
});
});
});