-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (84 loc) · 3.46 KB
/
index.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
const Helper = codeceptjs.helper;
const {
assertAccumulatedLength,
assertResourcesExist,
assertResourcesNumber,
assertIsUniqueResource,
} = require('./asserts');
class MyHelper extends Helper {
_after() {
delete this.resources;
}
async _startSpyingThePage(page) {
this.resources = [];
await page.setCacheEnabled(false);
const devToolsResponses = new Map();
const devTools = await page.target().createCDPSession();
await devTools.send('Network.enable');
devTools.on('Network.responseReceived', (event) => {
devToolsResponses.set(event.requestId, event.response);
});
devTools.on('Network.loadingFinished', (event) => {
const response = devToolsResponses.get(event.requestId);
const resource = {
url: response.url,
contentLength: event.encodedDataLength || 0,
contentType: response.headers['content-type'],
};
this.debug(`${resource.contentType}\t${resource.contentLength}\t${resource.url}`);
this.resources.push(resource);
});
}
async startSpyTheResources() {
const { page } = this.helpers.Puppeteer;
this._startSpyingThePage(page);
this.debug('Start capturing resources');
}
async spyTheResourcesLoadedIn(url) {
const { browser } = await this.helpers.Puppeteer;
const page = await browser.newPage();
this._startSpyingThePage(page);
this.debug('Resources Captured');
await page.goto(url, { waitUntil: ['networkidle0'] });
this.debug('End Resources Captured');
await page.close();
}
async seeResourcesBeenLoaded() {
assertResourcesExist(this.resources);
}
async checkTheResourceSize(pattern, expectedSize) {
assertResourcesExist(this.resources);
const resourcesMatch = this.resources.filter(resource => pattern.test(resource.url));
assertIsUniqueResource(resourcesMatch, { Pattern: pattern });
assertAccumulatedLength(resourcesMatch, expectedSize, resourcesMatch[0], this.config.threshold);
}
async checkTheResourcesSize(pattern, expectedSize) {
assertResourcesExist(this.resources);
const resourcesMatch = this.resources.filter(resource => pattern.test(resource.url));
assertAccumulatedLength(resourcesMatch, expectedSize, { pattern }, this.config.threshold);
}
async checkTheResourceTypeSize(contentType, expectedSize) {
assertResourcesExist(this.resources);
const resourcesMatch = this.resources.filter(resource => resource.contentType === contentType);
assertAccumulatedLength(resourcesMatch, expectedSize, { 'content type': contentType }, this.config.threshold);
}
async checkAllResourcesSize(expectedSize) {
assertResourcesExist(this.resources);
assertAccumulatedLength(this.resources, expectedSize, {}, this.config.threshold);
}
async checkTheNumberOfResource(pattern, expectedNumber) {
assertResourcesExist(this.resources);
const resourcesMatch = this.resources.filter(resource => pattern.test(resource.url));
assertResourcesNumber(resourcesMatch, expectedNumber, { pattern });
}
async checkTheNumberOfResourceType(contentType, expectedNumber) {
assertResourcesExist(this.resources);
const resourcesMatch = this.resources.filter(resource => resource.contentType === contentType);
assertResourcesNumber(resourcesMatch, expectedNumber, { contentType });
}
async checkTheNumberOfResources(expectedNumber) {
assertResourcesExist(this.resources);
assertResourcesNumber(this.resources, expectedNumber);
}
}
module.exports = MyHelper;