Skip to content

Commit 3291a3f

Browse files
authored
feat: include the recorded request in grabRecordedNetworkTraffics (#5801)
* feat: include request headers in grabRecordedNetworkTraffics Every helper that records traffic (Playwright, Puppeteer, CDPBrowser) already stores requestHeaders on each recorded request, but grabRecordedNetworkTraffics dropped them, so tests had to reach into helper internals to assert on a request header. * feat: return the recorded request in grabRecordedNetworkTraffics Expose url, method, headers and postData as a request object alongside response, instead of only the headers. * refactor: name the request object req to match resp; use a real header in the docs example * fix: parse JSON post data in grabRecordedNetworkTraffics so every helper returns the same type
1 parent 36be057 commit 3291a3f

3 files changed

Lines changed: 43 additions & 0 deletions

File tree

‎docs/webapi/grabRecordedNetworkTraffics.mustache‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ Grab the recording network traffics
33
```js
44
const traffics = await I.grabRecordedNetworkTraffics();
55
expect(traffics[0].url).to.equal('https://reqres.in/api/comments/1');
6+
expect(traffics[0].request.method).to.equal('GET');
7+
expect(traffics[0].request.headers['user-agent']).to.contain('Mozilla');
68
expect(traffics[0].response.status).to.equal(200);
79
expect(traffics[0].response.body).to.contain({ name: 'this was mocked' });
810
```

‎lib/helper/network/actions.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,33 @@ async function seeTraffic({ name, url, parameters, requestPostData, timeout = 10
5656
}
5757
}
5858

59+
function parsePostData(postData) {
60+
if (typeof postData !== 'string') return postData
61+
try {
62+
return JSON.parse(postData)
63+
} catch (e) {
64+
return postData
65+
}
66+
}
67+
5968
async function grabRecordedNetworkTraffics() {
6069
if (!this.recordedAtLeastOnce) {
6170
throw new Error('Failure in test automation. You use "I.grabRecordedNetworkTraffics", but "I.startRecordingTraffic" was never called before.');
6271
}
6372

6473
const promises = this.requests.map(async request => {
6574
const resp = await request.response
75+
const req = {
76+
url: request.url,
77+
method: request.method,
78+
headers: request.requestHeaders,
79+
postData: parsePostData(request.requestPostData),
80+
}
6681

6782
if (!resp) {
6883
return {
6984
url: '',
85+
request: req,
7086
response: {
7187
status: '',
7288
statusText: '',
@@ -85,6 +101,7 @@ async function grabRecordedNetworkTraffics() {
85101

86102
return {
87103
url: resp.url(),
104+
request: req,
88105
response: {
89106
status: resp.status(),
90107
statusText: resp.statusText(),

‎test/helper/webapi.js‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,6 +2523,30 @@ export function tests() {
25232523
expect(traffics.length).to.equal(0)
25242524
})
25252525

2526+
it('should grab the request of recorded traffics', async () => {
2527+
await I.startRecordingTraffic()
2528+
await I.amOnPage('https://codecept.io/')
2529+
const traffics = await I.grabRecordedNetworkTraffics()
2530+
expect(traffics.length).to.be.above(0)
2531+
expect(traffics[0].request.method).to.equal('GET')
2532+
const headerNames = Object.keys(traffics[0].request.headers).map(name => name.toLowerCase())
2533+
expect(headerNames).to.include('user-agent')
2534+
})
2535+
2536+
it('should grab JSON post data of recorded traffics as an object', async () => {
2537+
await I.amOnPage('https://codecept.io/')
2538+
await I.startRecordingTraffic()
2539+
await I.executeScript(() =>
2540+
fetch('https://codecept.io/traffic-post-data', { method: 'POST', body: JSON.stringify({ name: 'codecept' }) })
2541+
.then(res => res.status)
2542+
.catch(() => 0),
2543+
)
2544+
await I.seeTraffic({ name: 'post', url: 'https://codecept.io/traffic-post-data' })
2545+
const traffics = await I.grabRecordedNetworkTraffics()
2546+
const post = traffics.find(t => t.request.method === 'POST')
2547+
expect(post.request.postData).to.deep.equal({ name: 'codecept' })
2548+
})
2549+
25262550
it('should stop the network recording', async () => {
25272551
await I.startRecordingTraffic()
25282552
await I.amOnPage('https://codecept.io/')

0 commit comments

Comments
 (0)