forked from nightwatchjs/nightwatch-plugin-browserstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelper.js
More file actions
421 lines (349 loc) · 14.5 KB
/
Copy pathhelper.js
File metadata and controls
421 lines (349 loc) · 14.5 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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
const {expect} = require('chai');
const mockery = require('mockery');
describe('generateLocalIdentifier', () => {
let generateLocalIdentifier;
before(() => {
mockery.enable();
var os = {
hostname: function() {
return 'HOSTNAME';
}
};
mockery.registerMock('os', os);
delete require.cache[require.resolve('../../../src/utils/helper')];
generateLocalIdentifier = require('../../../src/utils/helper').generateLocalIdentifier;
});
it('returns identifier with correct format', async () => {
const identifierComponents = generateLocalIdentifier().split('_');
expect(identifierComponents.length).to.eq(5);
expect(identifierComponents[0]).to.eq(new Date().getDate().toString());
expect(identifierComponents[1]).to.eq(new Date().toLocaleString('en-GB', {month: 'short'}));
expect(identifierComponents[3]).to.eq('HOSTNAME');
expect(identifierComponents[4].length).to.eq(4);
});
after(() => {
mockery.disable();
});
});
describe('isUndefined', () => {
let isUndefined;
before(() => {
isUndefined = require('../../../src/utils/helper').isUndefined;
});
it('returns true for undefined', async () => {
expect(isUndefined(undefined)).to.be.true;
});
it('returns true for null', async () => {
expect(isUndefined(null)).to.be.true;
});
it('returns false for other values', async () => {
expect(isUndefined('null')).to.be.false;
expect(isUndefined('')).to.be.false;
expect(isUndefined(0)).to.be.false;
});
});
describe('isObject', () => {
let isObject;
before(() => {
isObject = require('../../../src/utils/helper').isObject;
});
it('returns false for undefined and null', async () => {
expect(isObject(undefined)).to.be.false;
expect(isObject(null)).to.be.false;
});
it('returns true for objects', async () => {
expect(isObject({})).to.be.true;
expect(isObject({'h': 1})).to.be.true;
expect(isObject(new Object())).to.be.true;
});
});
describe('getAccessKey', () => {
let getAccessKey;
const settings = {};
before(() => {
getAccessKey = require('../../../src/utils/helper').getAccessKey;
});
it('returns null for empty settings', async () => {
expect(getAccessKey(settings)).to.be.oneOf([null, undefined]);
});
it('returns null for empty desireCapabilities', async () => {
settings.desiredCapabilities = {};
expect(getAccessKey(settings)).to.be.oneOf([null, undefined]);
});
it('returns key for non w3c', async () => {
settings.desiredCapabilities = {
'browserstack.key': 'ACCESS_KEY'
};
expect(getAccessKey(settings)).to.eq('ACCESS_KEY');
});
it('returns key for w3c', async () => {
settings.desiredCapabilities = {
'bstack:options': {
'accessKey': 'ACCESS_KEY'
}
};
expect(getAccessKey(settings)).to.eq('ACCESS_KEY');
});
it('returns undefined for no key w3c', async () => {
settings.desiredCapabilities = {
'bstack:options': {}
};
expect(getAccessKey(settings)).to.be.oneOf([null, undefined]);
});
it('returs key from env for no key in settings', async () => {
process.env.BROWSERSTACK_ACCESS_KEY = 'ACCESS_KEY';
settings.desiredCapabilities = {};
expect(getAccessKey(settings)).to.eq('ACCESS_KEY');
});
});
describe('getUserName', () => {
let getUserName;
const settings = {};
before(() => {
getUserName = require('../../../src/utils/helper').getUserName;
});
it('returns null for empty settings', async () => {
expect(getUserName(settings)).to.be.oneOf([null, undefined]);
});
it('returns null for empty desireCapabilities', async () => {
settings.desiredCapabilities = {};
expect(getUserName(settings)).to.be.oneOf([null, undefined]);
});
it('returns user for non w3c', async () => {
settings.desiredCapabilities = {
'browserstack.user': 'USERNAME'
};
expect(getUserName(settings)).to.eq('USERNAME');
});
it('returns user for w3c', async () => {
settings.desiredCapabilities = {
'bstack:options': {
'userName': 'USERNAME'
}
};
expect(getUserName(settings)).to.eq('USERNAME');
});
it('returns undefined for no user w3c', async () => {
settings.desiredCapabilities = {
'bstack:options': {}
};
expect(getUserName(settings)).to.be.oneOf([null, undefined]);
});
it('returs user from env for no key in settings', async () => {
process.env.BROWSERSTACK_USERNAME = 'BROWSERSTACK_USERNAME';
settings.desiredCapabilities = {};
expect(getUserName(settings)).to.eq('BROWSERSTACK_USERNAME');
});
});
describe('isAccessibilitySession', () => {
let isAccessibilitySession;
before(() => {
isAccessibilitySession = require('../../../src/utils/helper').isAccessibilitySession;
});
it('returns false for undefined', async () => {
delete process.env.BROWSERSTACK_ACCESSIBILITY;
expect(isAccessibilitySession()).to.be.false;
});
it('returns true if env variable is set to true', async () => {
process.env.BROWSERSTACK_ACCESSIBILITY = true;
expect(isAccessibilitySession()).to.be.true;
delete process.env.BROWSERSTACK_ACCESSIBILITY;
});
it('returns false if env variable is set to false', async () => {
process.env.BROWSERSTACK_ACCESSIBILITY = false;
expect(isAccessibilitySession()).to.be.false;
delete process.env.BROWSERSTACK_ACCESSIBILITY;
});
});
describe('getProjectName', () => {
let getProjectName;
let options = {};
let bstackOptions = {};
let fromProduct = {};
before(() => {
getProjectName = require('../../../src/utils/helper').getProjectName;
});
beforeEach(() => {
options = {};
bstackOptions = {};
fromProduct = {};
});
it('returns empty for empty options', async () => {
expect(getProjectName(options, bstackOptions, fromProduct)).to.eq('');
});
it('returns projectName from bstackOptions', async () => {
bstackOptions.projectName = 'bstackOptionsProjectName';
expect(getProjectName(options, bstackOptions, fromProduct)).to.eq('bstackOptionsProjectName');
});
it('returns projectName from bstackOptions if fromProduct is not set', async () => {
bstackOptions.projectName = 'bstackOptionsProjectName';
options.test_observability = {projectName: 'obsProjectName'};
options.accessibility = {projectName: 'accessibilityProjectName'};
expect(getProjectName(options, bstackOptions, fromProduct)).to.eq('bstackOptionsProjectName');
});
it('returns projectName from test_observability if fromProduct is set to test_observability', async () => {
bstackOptions.projectName = 'bstackOptionsProjectName';
options.test_observability = {projectName: 'obsProjectName'};
options.accessibility = {projectName: 'accessibilityProjectName'};
fromProduct.test_observability = true;
expect(getProjectName(options, bstackOptions, fromProduct)).to.eq('obsProjectName');
});
it('returns projectName from accessibility if fromProduct is set to accessibility', async () => {
bstackOptions.projectName = 'bstackOptionsProjectName';
options.test_observability = {projectName: 'obsProjectName'};
options.accessibility = {projectName: 'accessibilityProjectName'};
fromProduct.accessibility = true;
expect(getProjectName(options, bstackOptions, fromProduct)).to.eq('accessibilityProjectName');
});
it('returns projectName from bstackOptions if fromProduct is set to test_observability and projectName not overriden in test_observability', async () => {
bstackOptions.projectName = 'bstackOptionsProjectName';
options.test_observability = {};
options.accessibility = {projectName: 'accessibilityProjectName'};
fromProduct.test_observability = true;
expect(getProjectName(options, bstackOptions, fromProduct)).to.eq('bstackOptionsProjectName');
});
it('returns projectName from bstackOptions if fromProduct is set to accessibility and projectName not overriden in accessibility', async () => {
bstackOptions.projectName = 'bstackOptionsProjectName';
options.test_observability = {};
options.accessibility = {};
fromProduct.test_observability = true;
expect(getProjectName(options, bstackOptions, fromProduct)).to.eq('bstackOptionsProjectName');
});
});
describe('getBuildName', () => {
let getBuildName;
let options = {};
let bstackOptions = {};
let fromProduct = {};
before(() => {
getBuildName = require('../../../src/utils/helper').getBuildName;
});
beforeEach(() => {
options = {};
bstackOptions = {};
fromProduct = {};
});
it('returns empty for empty options', async () => {
expect(getBuildName(options, bstackOptions, fromProduct)).to.eq('nightwatch-plugin-browserstack');
});
it('returns buildName from bstackOptions', async () => {
bstackOptions.buildName = 'bstackOptionsBuildName';
expect(getBuildName(options, bstackOptions, fromProduct)).to.eq('bstackOptionsBuildName');
});
it('returns buildName from bstackOptions if fromProduct is not set', async () => {
bstackOptions.buildName = 'bstackOptionsBuildName';
options.test_observability = {buildName: 'obsBuildName'};
options.accessibility = {buildName: 'accessibilityBuildName'};
expect(getBuildName(options, bstackOptions, fromProduct)).to.eq('bstackOptionsBuildName');
});
it('returns buildName from test_observability if fromProduct is set to test_observability', async () => {
bstackOptions.buildName = 'bstackOptionsBuildName';
options.test_observability = {buildName: 'obsBuildName'};
options.accessibility = {buildName: 'accessibilityBuildName'};
fromProduct.test_observability = true;
expect(getBuildName(options, bstackOptions, fromProduct)).to.eq('obsBuildName');
});
it('returns buildName from accessibility if fromProduct is set to accessibility', async () => {
bstackOptions.buildName = 'bstackOptionsBuildName';
options.test_observability = {buildName: 'obsBuildName'};
options.accessibility = {buildName: 'accessibilityBuildName'};
fromProduct.accessibility = true;
expect(getBuildName(options, bstackOptions, fromProduct)).to.eq('accessibilityBuildName');
});
it('returns buildName from bstackOptions if fromProduct is set to test_observability and projectName not overriden in test_observability', async () => {
bstackOptions.buildName = 'bstackOptionsBuildName';
options.test_observability = {};
options.accessibility = {};
fromProduct.test_observability = true;
expect(getBuildName(options, bstackOptions, fromProduct)).to.eq('bstackOptionsBuildName');
});
it('returns buildName from bstackOptions if fromProduct is set to accessibility and projectName not overriden in accessibility', async () => {
bstackOptions.buildName = 'bstackOptionsBuildName';
options.test_observability = {};
options.accessibility = {};
fromProduct.test_observability = true;
expect(getBuildName(options, bstackOptions, fromProduct)).to.eq('bstackOptionsBuildName');
});
});
describe('isBrowserstackInfra', () => {
let isBrowserstackInfra;
before(() => {
isBrowserstackInfra = require('../../../src/utils/helper').isBrowserstackInfra;
});
it('returns true for undefined settings', async () => {
expect(isBrowserstackInfra()).to.be.true;
});
it('returns true for empty settings', async () => {
expect(isBrowserstackInfra({})).to.be.true;
});
it('returns true if webdriver.host contains browserstack', async () => {
const settings = {
webdriver: {
host: 'hub-cloud.browserstack.com'
}
};
expect(isBrowserstackInfra(settings)).to.be.true;
});
it('returns false if webdriver.host does not contain browserstack', async () => {
const settings = {
webdriver: {
host: 'localhost'
}
};
expect(isBrowserstackInfra(settings)).to.be.false;
});
});
describe('isSuppressedFailure', () => {
// Shared primitive used by both src/testObservability.js (sendTestRunEvent)
// and nightwatch/globals.js (setSessionStatus). Covers both call sites by
// reference — any regression here breaks both surfaces equivalently.
let isSuppressedFailure;
before(() => {
isSuppressedFailure = require('../../../src/utils/helper').isSuppressedFailure;
});
it('returns false for null / undefined commands', () => {
expect(isSuppressedFailure(null)).to.be.false;
expect(isSuppressedFailure(undefined)).to.be.false;
});
it('returns false for commands that did not fail', () => {
expect(isSuppressedFailure({status: 'pass', args: [{suppressNotFoundErrors: true}]})).to.be.false;
});
it('returns false when args is missing or empty', () => {
expect(isSuppressedFailure({status: 'fail'})).to.be.false;
expect(isSuppressedFailure({status: 'fail', args: []})).to.be.false;
});
it('returns false when args is not an array', () => {
expect(isSuppressedFailure({status: 'fail', args: {suppressNotFoundErrors: true}})).to.be.false;
expect(isSuppressedFailure({status: 'fail', args: 'not-an-array'})).to.be.false;
});
it('returns false when args[0] is a primitive', () => {
expect(isSuppressedFailure({status: 'fail', args: [null]})).to.be.false;
expect(isSuppressedFailure({status: 'fail', args: [42]})).to.be.false;
expect(isSuppressedFailure({status: 'fail', args: [true]})).to.be.false;
});
it('returns true when args[0] is an object with suppressNotFoundErrors:true', () => {
expect(isSuppressedFailure({
status: 'fail',
args: [{selector: '#x', suppressNotFoundErrors: true, timeout: 2000}, null]
})).to.be.true;
});
it('returns true when args[0] is a JSON string carrying suppressNotFoundErrors:true', () => {
expect(isSuppressedFailure({
status: 'fail',
args: ['{"selector":"#x","suppressNotFoundErrors":true,"timeout":2000}', null]
})).to.be.true;
});
it('returns false when args[0] is a malformed JSON string (safe default)', () => {
// Unparseable strings default to "not suppressed" — i.e., the test stays
// failed. That's the safer direction, matches the rest of the guard's
// bias to under-suppress rather than over-suppress.
expect(isSuppressedFailure({status: 'fail', args: ['{this is not json']})).to.be.false;
});
it('returns false when args[0] is an object without suppressNotFoundErrors', () => {
expect(isSuppressedFailure({status: 'fail', args: [{selector: '#x', timeout: 2000}]})).to.be.false;
});
it('returns false when suppressNotFoundErrors is falsy', () => {
expect(isSuppressedFailure({status: 'fail', args: [{suppressNotFoundErrors: false}]})).to.be.false;
expect(isSuppressedFailure({status: 'fail', args: [{suppressNotFoundErrors: 'true'}]})).to.be.false;
});
});