Skip to content

Commit 83030f5

Browse files
committed
fix: improve traffic recording checks and add test for stopping network recording
1 parent 15a288e commit 83030f5

File tree

2 files changed

+47
-44
lines changed

2 files changed

+47
-44
lines changed

lib/helper/network/actions.js

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,68 @@
1-
const assert = require('assert');
2-
const { isInTraffic, createAdvancedTestResults, getTrafficDump } = require('./utils');
1+
const assert = require('assert')
2+
const { isInTraffic, createAdvancedTestResults, getTrafficDump } = require('./utils')
33

44
function dontSeeTraffic({ name, url }) {
55
if (!this.recordedAtLeastOnce) {
6-
throw new Error('Failure in test automation. You use "I.dontSeeTraffic", but "I.startRecordingTraffic" was never called before.');
6+
throw new Error('Failure in test automation. You use "I.dontSeeTraffic", but "I.startRecordingTraffic" was never called before.')
77
}
88

99
if (!name) {
10-
throw new Error('Missing required key "name" in object given to "I.dontSeeTraffic".');
10+
throw new Error('Missing required key "name" in object given to "I.dontSeeTraffic".')
1111
}
1212

1313
if (!url) {
14-
throw new Error('Missing required key "url" in object given to "I.dontSeeTraffic".');
14+
throw new Error('Missing required key "url" in object given to "I.dontSeeTraffic".')
1515
}
1616

1717
if (isInTraffic.call(this, url)) {
18-
assert.fail(`Traffic with name "${name}" (URL: "${url}') found, but was not expected to be found.`);
18+
assert.fail(`Traffic with name "${name}" (URL: "${url}') found, but was not expected to be found.`)
1919
}
2020
}
2121

22-
async function seeTraffic({
23-
name, url, parameters, requestPostData, timeout = 10,
24-
}) {
22+
async function seeTraffic({ name, url, parameters, requestPostData, timeout = 10 }) {
2523
if (!name) {
26-
throw new Error('Missing required key "name" in object given to "I.seeTraffic".');
24+
throw new Error('Missing required key "name" in object given to "I.seeTraffic".')
2725
}
2826

2927
if (!url) {
30-
throw new Error('Missing required key "url" in object given to "I.seeTraffic".');
28+
throw new Error('Missing required key "url" in object given to "I.seeTraffic".')
3129
}
3230

33-
if (!this.recording || !this.recordedAtLeastOnce) {
34-
throw new Error('Failure in test automation. You use "I.seeTraffic", but "I.startRecordingTraffic" was never called before.');
31+
if (!this.recordedAtLeastOnce) {
32+
throw new Error('Failure in test automation. You use "I.seeTraffic", but "I.startRecordingTraffic" was never called before.')
3533
}
3634

3735
for (let i = 0; i <= timeout * 2; i++) {
38-
const found = isInTraffic.call(this, url, parameters);
36+
const found = isInTraffic.call(this, url, parameters)
3937
if (found) {
40-
return true;
38+
return true
4139
}
42-
await new Promise((done) => {
43-
setTimeout(done, 1000);
44-
});
40+
await new Promise(done => {
41+
setTimeout(done, 1000)
42+
})
4543
}
4644

4745
// check request post data
4846
if (requestPostData && isInTraffic.call(this, url)) {
49-
const advancedTestResults = createAdvancedTestResults(url, requestPostData, this.requests);
47+
const advancedTestResults = createAdvancedTestResults(url, requestPostData, this.requests)
5048

51-
assert.equal(advancedTestResults, true, `Traffic named "${name}" found correct URL ${url}, BUT the post data did not match:\n ${advancedTestResults}`);
49+
assert.equal(advancedTestResults, true, `Traffic named "${name}" found correct URL ${url}, BUT the post data did not match:\n ${advancedTestResults}`)
5250
} else if (parameters && isInTraffic.call(this, url)) {
53-
const advancedTestResults = createAdvancedTestResults(url, parameters, this.requests);
51+
const advancedTestResults = createAdvancedTestResults(url, parameters, this.requests)
5452

55-
assert.fail(
56-
`Traffic named "${name}" found correct URL ${url}, BUT the query parameters did not match:\n`
57-
+ `${advancedTestResults}`,
58-
);
53+
assert.fail(`Traffic named "${name}" found correct URL ${url}, BUT the query parameters did not match:\n` + `${advancedTestResults}`)
5954
} else {
60-
assert.fail(
61-
`Traffic named "${name}" not found in recorded traffic within ${timeout} seconds.\n`
62-
+ `Expected url: ${url}.\n`
63-
+ `Recorded traffic:\n${getTrafficDump.call(this)}`,
64-
);
55+
assert.fail(`Traffic named "${name}" not found in recorded traffic within ${timeout} seconds.\n` + `Expected url: ${url}.\n` + `Recorded traffic:\n${getTrafficDump.call(this)}`)
6556
}
6657
}
6758

6859
async function grabRecordedNetworkTraffics() {
69-
if (!this.recording || !this.recordedAtLeastOnce) {
70-
throw new Error('Failure in test automation. You use "I.grabRecordedNetworkTraffics", but "I.startRecordingTraffic" was never called before.');
60+
if (!this.recordedAtLeastOnce) {
61+
throw new Error('Failure in test automation. You use "I.grabRecordedNetworkTraffics", but "I.startRecordingTraffic" was never called before.')
7162
}
7263

73-
const promises = this.requests.map(async (request) => {
74-
const resp = await request.response;
64+
const promises = this.requests.map(async request => {
65+
const resp = await request.response
7566

7667
if (!resp) {
7768
return {
@@ -81,13 +72,13 @@ async function grabRecordedNetworkTraffics() {
8172
statusText: '',
8273
body: '',
8374
},
84-
};
75+
}
8576
}
8677

87-
let body;
78+
let body
8879
try {
8980
// There's no 'body' for some requests (redirect etc...)
90-
body = JSON.parse((await resp.body()).toString());
81+
body = JSON.parse((await resp.body()).toString())
9182
} catch (e) {
9283
// only interested in JSON, not HTML responses.
9384
}
@@ -99,19 +90,21 @@ async function grabRecordedNetworkTraffics() {
9990
statusText: resp.statusText(),
10091
body,
10192
},
102-
};
103-
});
104-
return Promise.all(promises);
93+
}
94+
})
95+
return Promise.all(promises)
10596
}
10697

10798
function stopRecordingTraffic() {
10899
// @ts-ignore
109-
this.page.removeAllListeners('request');
110-
this.recording = false;
100+
this.page.removeAllListeners('request')
101+
// @ts-ignore
102+
this.page.removeAllListeners('requestfinished')
103+
this.recording = false
111104
}
112105

113106
function flushNetworkTraffics() {
114-
this.requests = [];
107+
this.requests = []
115108
}
116109

117110
module.exports = {
@@ -120,4 +113,4 @@ module.exports = {
120113
grabRecordedNetworkTraffics,
121114
stopRecordingTraffic,
122115
flushNetworkTraffics,
123-
};
116+
}

test/helper/webapi.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,16 @@ module.exports.tests = function () {
17031703
expect(traffics.length).to.equal(0)
17041704
})
17051705

1706+
it('should stop the network recording', async () => {
1707+
await I.startRecordingTraffic()
1708+
await I.amOnPage('https://codecept.io/')
1709+
await I.stopRecordingTraffic()
1710+
const traffics1 = await I.grabRecordedNetworkTraffics()
1711+
await I.amOnPage('https://codecept.io/')
1712+
const traffics2 = await I.grabRecordedNetworkTraffics()
1713+
expect(traffics2.length).to.equal(traffics1.length)
1714+
})
1715+
17061716
it('should see recording traffics', async () => {
17071717
I.startRecordingTraffic()
17081718
I.amOnPage('https://codecept.io/')

0 commit comments

Comments
 (0)