我正在尝试使用 puppeteer 编写 Jest 测试
describe('Downloads', () => {
it(`should refirect to download page for ios`, async () => {
await page.setUserAgent('exotic');
let response = await page.goto(`http://localhost:8888/downloads`, {waitUntil: "networkidle0"});
let url = page.url();
expect(response.status()).toBe(303);
expect(url).toBe(`http://localhost:8888/downloads/ios`);
});
});
Run Code Online (Sandbox Code Playgroud)
但响应状态是200因为goto返回响应http://localhost:8888/downloads/ios
如何获取重定向状态码?
您可以request.redirectChain()为此使用。它返回一个包含所有请求的数组。然后您可以像这样访问第一个请求(和响应):
const response = await page.goto('...');
const chain = response.request().redirectChain();
const redirectRequest = chain[0];
const redirectResponse = await redirectRequest.response();
expect(redirectResponse.status()).toBe(303);
expect(redirectResponse.url()).toBe('...');
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2232 次 |
| 最近记录: |