This repository was archived by the owner on Sep 21, 2022. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathutil.js
More file actions
129 lines (113 loc) Β· 3.17 KB
/
Copy pathutil.js
File metadata and controls
129 lines (113 loc) Β· 3.17 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
8000
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
'use strict';
const Browser = require('lib/browser');
const Promise = require('bluebird');
const State = require('lib/state');
const Suite = require('lib/suite');
const _ = require('lodash');
function makeBrowser(capabilities, config) {
config = _.merge({}, {
id: 'id',
desiredCapabilities: capabilities,
system: {
coverage: {}
}
}, config);
return Browser.create(config);
}
function browserWithId(id) {
return Browser.create({
id,
desiredCapabilities: {
browserName: id
}
});
}
function makeSuiteStub(opts) {
opts = _.defaults(opts || {}, {
name: `some-default-name_${new Date().getTime()}`,
url: 'some-default-url',
states: []
});
const suite = Suite.create(opts.name, opts.parent);
suite.beforeActions = ['before', 'actions'];
suite.afterActions = ['after', 'actions'];
if (opts.browsers) {
suite.browsers = opts.browsers;
}
opts.states.forEach(function(state) {
suite.addState(state);
});
suite.url = opts.url;
return suite;
}
function makeStateStub(suite, opts) {
suite = suite || makeSuiteStub();
opts = opts || {};
const state = new State(suite, opts.name || 'default-state-name');
state.shouldSkip = sinon.stub();
suite.states.push(state);
return state;
}
/**
* Makes suite tree
* @param {Object} sceleton, keys must be unique for all levels
* @param {Object} rootOpts options for root suite
* @return {Object} object, where each suite/state can be accessed by its name
*
* Example:
* var tree = makeSuiteTree({
* parent: {
* child: {
* grandchild: ['state1', 'state2']
* }
* }
* }, {browsers: ['b1', 'b2']});
*
* assert.equal(tree.grandchild.parent, tree.child);
* assert.equal(tree.state1.suite, tree.grandchild);
* assert.equal(tree.state1.browsers, ['b1', 'b2']);
*/
function makeSuiteTree(sceleton, rootOpts) {
rootOpts = _.defaults(rootOpts || {}, {
browsers: ['default-browser1', 'default-browser2']
});
const tree = {};
mkTree_(sceleton, makeSuiteStub(rootOpts));
return tree;
function mkTree_(sceleton, parent) {
_.forEach(sceleton, (val, name) => {
if (_.isString(val)) {
pushToTree_(val, makeStateStub(parent, {name: val}));
} else {
const suite = makeSuiteStub({parent: parent, name: name});
parent.addChild(suite);
pushToTree_(name, suite);
mkTree_(val, suite);
}
});
}
function pushToTree_(name, val) {
if (tree[name]) {
throw new Error('Can not make tree, names should be uniq');
}
tree[name] = val;
}
}
function rejectedPromise(error) {
if (!error) {
error = new Error('Rejected');
} else if (typeof error === 'string') {
error = new Error(error);
}
const promise = Promise.reject(error);
promise.catch(() => {});
return promise;
}
module.exports = {
makeBrowser,
browserWithId,
makeStateStub,
makeSuiteStub,
makeSuiteTree,
rejectedPromise
};