I need to intercept network request and save it's response body to variable, so I can perform assertions with values, that are displayed on UI, but when I try to access variable which is supposed to contain saved response body I receive [object Object] instead of a valid body.
JSON.stringify also doesn't fix the problem, as my variable becomes {"_type":"Page","_guid":"page@"}
instead of an actual response.
Here's the code:
const resp = await page.on('response', async response => {
if (response.url().includes('/some_url/') && response.status() === 200) {
console.log('BODY() ' + (await response.body())); //logs valid JSON response body
return await response.body();
}
})
console.log('RESPONSE' + resp); //logs RESPONSE[object Object]
It's been nearly a year since I asked this question. There's also another way to save responses using async predicate functions (especially useful with graphql, where there's only one endpoint)
const [response] = await Promise.all([
page.waitForResponse(async response => {
const text = await response.text();
return text.includes(`some response text, that we need to intercept`);
})
]);
return response.json();