-
Notifications
You must be signed in to change notification settings - Fork 659
/
test-controller-events-test.js
202 lines (166 loc) · 6.69 KB
/
test-controller-events-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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const expect = require('chai').expect;
const AsyncEventEmitter = require('../../lib/utils/async-event-emitter');
const TestRun = require('../../lib/test-run');
const TestController = require('../../lib/api/test-controller');
const Task = require('../../lib/runner/task');
const BrowserJob = require('../../lib/runner/browser-job');
const Reporter = require('../../lib/reporter');
const { Role } = require('../../lib/api/exportable-lib');
const TestRunErrorFormattableAdapter = require('../../lib/errors/test-run/formattable-adapter');
class TestRunMock extends TestRun {
constructor () {
super({ id: 'test-id', name: 'test-name', fixture: { path: 'dummy', id: 'fixture-id', name: 'fixture-name' } }, {}, {}, {}, {});
this.browserConnection = {
browserInfo: {
alias: 'test-browser'
},
isHeadlessBrowser: () => false
};
}
_addInjectables () {
}
_initRequestHooks () {
}
get id () {
return 'test-run-id';
}
executeCommand () {
return Promise.resolve();
}
}
class TestControllerMock extends TestController {
constructor (testRun) {
super(testRun);
testRun.controller = this;
}
}
class TaskMock extends AsyncEventEmitter {
constructor () {
super();
this.tests = [];
this.browserConnectionGroups = [];
this.opts = {};
}
_assignBrowserJobEventHandlers (job) {
Task.prototype._assignBrowserJobEventHandlers.call(this, job);
}
}
const options = {
caretPos: 1,
modifiers: {
alt: true,
ctrl: true,
meta: true,
shift: true
},
offsetX: 1,
offsetY: 2,
destinationOffsetX: 3,
destinationOffsetY: 4,
speed: 1,
replace: true,
paste: true,
};
const actions = {
click: ['#target', options],
rightClick: ['#target', options],
doubleClick: ['#target', options],
hover: ['#target', options],
drag: ['#target', 100, 200, options],
dragToElement: ['#target', '#target', options],
typeText: ['#input', 'test', options],
selectText: ['#input', 1, 3, options],
selectTextAreaContent: ['#textarea', 1, 2, 3, 4, options],
selectEditableContent: ['#contenteditable', '#contenteditable', options],
pressKey: ['enter', options],
wait: [1],
navigateTo: ['./index.html'],
setFilesToUpload: ['#file', '../test.js'],
clearUpload: ['#file'],
takeScreenshot: [{ path: 'screenshotPath', fullPage: true }],
takeElementScreenshot: ['#target', 'screenshotPath'],
resizeWindow: [200, 200],
resizeWindowToFitDevice: ['Sony Xperia Z', { portraitOrientation: true }],
maximizeWindow: [],
switchToIframe: ['#iframe'],
switchToMainWindow: [],
setNativeDialogHandler: [() => true],
getNativeDialogHistory: [],
getBrowserConsoleMessages: [],
debug: [],
setTestSpeed: [1],
setPageLoadTimeout: [1],
useRole: [new Role('http://example.com', async () => {}, { preserveUrl: true })],
};
let testController = null;
let task = null;
const initializeReporter = (reporter) => {
return new Reporter(reporter, task);
};
describe('TestController action events', () => {
beforeEach(() => {
const job = new BrowserJob([], [], void 0, void 0, void 0, void 0, { TestRunCtor: TestRunMock });
const testRunController = job._createTestRunController();
const testRun = new TestRunMock();
testRunController.testRun = testRun;
testController = new TestControllerMock(testRun);
task = new TaskMock();
task._assignBrowserJobEventHandlers(job);
testRunController._assignTestRunEvents(testRun);
});
it('Actions list', async () => {
const startLog = [];
const doneLog = [];
initializeReporter({
async reportTestActionStart (name) {
startLog.push(name);
},
async reportTestActionDone (name, { testRunId, command, test, fixture, browser }) {
const item = { testRunId, name, command, test, fixture, browser };
doneLog.push(item);
}
}, task);
// eval and expect has their functional tests
// addRequestHooks/removeRequestHooks are not logged
const exceptions = ['eval', 'expect', 'addRequestHooks', 'removeRequestHooks'];
const props = TestController.API_LIST
.filter(prop => !prop.accessor)
.map(prop => prop.apiProp)
.filter(prop => exceptions.indexOf(prop) === -1)
.filter(prop => typeof testController[prop] === 'function');
props.forEach(prop => {
if (!actions[prop])
throw new Error(`Describe the '${prop}' command`);
});
const actionsKeys = Object.keys(actions);
for (let i = 0; i < actionsKeys.length; i++)
await testController[actionsKeys[i]].apply(testController, actions[actionsKeys[i]]);
expect(actionsKeys.length).eql(startLog.length);
expect(actionsKeys.length).eql(doneLog.length);
expect(startLog).eql(actionsKeys);
const expected = require('./data/test-controller-reporter-expected');
expect(doneLog).eql(expected);
});
it('Error action', () => {
let actionResult = null;
let errorAdapter = null;
initializeReporter({
async reportTestActionDone (name, { command, errors }) {
errorAdapter = errors[0];
actionResult = { name, command: command.type, err: errors[0].errMsg };
}
});
testController.testRun.executeCommand = () => {
throw new Error('test error');
};
return testController.click('#target')
.then(() => {
throw new Error();
})
.catch(err => {
expect(errorAdapter).instanceOf(TestRunErrorFormattableAdapter);
expect(err.message).eql('test error');
expect(actionResult).eql({ name: 'click', command: 'click', err: 'Error: test error' });
});
});
});