Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get redirect status in puppeteer?

I'm trying write Jest test using puppeteer

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`);
    });
});

But response status is 200 because goto return response for http://localhost:8888/downloads/ios

How to get redirect status code?

like image 837
matchish Avatar asked Oct 15 '25 04:10

matchish


1 Answers

You can use the request.redirectChain() for this. It returns an array with all requests. You can then access the first request (and response) like this:

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('...');
like image 71
Thomas Dondorf Avatar answered Oct 18 '25 08:10

Thomas Dondorf