forked from vitalets/autotester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collect_network_requests_test.js
202 lines (179 loc) · 6.91 KB
/
collect_network_requests_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
'use strict';
var webdriver = require('..');
var assert = require('../testing/assert');
var test = require('../lib/test');
test.suite(function(env) {
var driver;
test.before(function () {
driver = env.builder().build();
});
test.beforeEach(function () {
driver.get(test.Pages.echoPage);
});
test.after(function () {
driver.quit();
});
describe('network requests', function () {
test.it('should collect normal navigation and apply filter', function () {
driver.requests().collect();
driver.get(test.Pages.echoPage);
driver.requests().stop();
assert(driver.requests().getCount({type: 'document', url: test.Pages.echoPage})).equalTo(1);
assert(driver.requests().getCount({url: test.Pages.echoPage})).equalTo(1);
assert(driver.requests().getCount({type: 'document'})).equalTo(1);
assert(driver.requests().getCount({url: 'abc'})).equalTo(0);
});
test.it('should collect js navigation', function () {
driver.requests().collect();
driver.executeScript(function(newUrl) {
location.href = newUrl;
}, test.Pages.simpleTestPage);
driver.wait(webdriver.until.urlIs(test.Pages.simpleTestPage));
driver.requests().stop();
assert(driver.requests().getCount({url: test.Pages.simpleTestPage})).equalTo(1);
});
test.it('should collect XMLHttpREquest', function () {
driver.requests().collect();
driver.executeScript(function() {
const xhr = new XMLHttpRequest();
xhr.open('GET', location.href);
xhr.send();
});
driver.requests().stop();
assert(driver.requests().getCount({type: 'xhr', url: test.Pages.echoPage})).equalTo(1);
});
test.it('should collect fetch', function () {
driver.requests().collect();
driver.executeScript(function() {
fetch(location.href);
});
driver.requests().stop();
assert(driver.requests().getCount({url: test.Pages.echoPage})).equalTo(1);
});
test.it('should collect new Image() load', function () {
driver.requests().collect();
driver.executeScript(function() {
const img = new Image();
img.src = 'image.png';
});
driver.requests().stop();
assert(driver.requests().getCount({url: test.whereIs('image.png')})).equalTo(1);
});
test.it('should collect <img> load', function () {
driver.requests().collect();
driver.executeScript(function() {
const img = document.createElement('img');
img.src = 'image.png';
document.body.appendChild(img);
});
driver.requests().stop();
assert(driver.requests().getCount({url: test.whereIs('image.png')})).equalTo(1);
});
test.it('should collect <script> load', function () {
driver.requests().collect();
driver.executeScript(function() {
const script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'script.js';
document.body.appendChild(script);
});
driver.requests().stop();
assert(driver.requests().getCount({url: test.whereIs('script.js')})).equalTo(1);
});
test.it('should collect <link> stylesheet load', function () {
driver.requests().collect();
driver.executeScript(function() {
const css = document.createElement('link');
css.rel = 'stylesheet';
css.href = 'style.css';
document.body.appendChild(css);
});
driver.requests().stop();
assert(driver.requests().getCount({url: test.whereIs('style.css')})).equalTo(1);
});
test.it('should collect new tabs', function () {
const initialHandle = driver.getWindowHandle();
driver.executeScript(function(href) {
const a = document.createElement('a');
a.href = href;
a.target = '_blank';
a.textContent = 'newtablink';
a.id = 'newtablink';
document.body.appendChild(a);
}, test.Pages.simpleTestPage);
driver.requests().collect();
const oldHandles = driver.getAllWindowHandles();
driver.wait(until.elementLocated({id: 'newtablink'}), 1000).click();
// delay needed for newtab to open
driver.sleep(1000);
driver.requests().stop();
// close opened tab
// todo: find more convenient way, maybe switchTo by url ?
const newHandles = driver.getAllWindowHandles();
webdriver.promise.all([oldHandles, newHandles, initialHandle]).then(([oldHandles, newHandles, initialHandle]) => {
const newHandle = newHandles.filter(h => oldHandles.indexOf(h) === -1)[0];
driver.switchTo().window(newHandle);
driver.close();
driver.switchTo().window(initialHandle);
});
assert(driver.requests().getCount({type: 'newtab', url: test.Pages.simpleTestPage})).equalTo(1);
});
// we can not test it as error should be uncaught to stop request catcher
test.it.skip('should call .stop() in case of error / idle', function () {
driver.requests().collect();
driver.call(() => {
throw new Error('some error');
});
});
test.it('should dump to string', function () {
driver.requests().collect();
driver.executeScript(function() {
fetch(location.href);
});
driver.requests().stop();
assert(driver.requests().dump()).equalTo([
'Collected 1 request(s):',
`Other: GET ${runContext.baseUrl}echo`,
].join('\n'));
});
test.it('should dump to console', function () {
driver.requests().collect();
driver.executeScript(function() {
fetch(location.href);
});
driver.requests().stop();
const consoleMock = {log: s => consoleMock.s = s};
driver.requests().dump(consoleMock).then(() => {
assert(consoleMock.s).equalTo([
'Collected 1 request(s):',
`Other: GET ${runContext.baseUrl}echo`,
].join('\n'));
});
});
test.it('should collect requests from another extension bg', function () {
driver.switchTo().window(runContext.selftest.handle);
driver.requests().collect();
driver.switchTo().newTab(runContext.selftest.ui);
driver.wait(until.titleContains('ready'));
const code = `fetch('${test.Pages.simpleTestPage}')`;
driver.executeScript(tests => {
window.runTests(tests);
}, [{path: 'test.js', code}]);
driver.wait(until.titleContains('done'));
driver.requests().stop();
assert(driver.requests().getCount({url: test.Pages.simpleTestPage})).equalTo(1);
});
test.it('should throw error on several .collect() calls', function () {
driver.requests().collect();
driver.requests().collect().catch(e => {
assert(e.message).equalTo('Requests already in collecting state');
});
driver.requests().stop();
});
test.it('should not throw error on several .stop() calls', function () {
driver.requests().collect();
driver.requests().stop();
driver.requests().stop();
});
});
});