Search code examples
javascriptwebautomationplaywright

How to capture requests and responses in Playwright after hitting a button?


I have the following Playwright code :

await page.goto(API_URL + "/login/");
await page.type('input[name="userName"]', username);
await page.type('input[name="password"]', password);
await page.click('button#submit');

what happens, it goes to about three redirects. Last redirect is to a pseudoaddress (basically a string) that contains credentials data. I need to extract that. How can I achieve that?


Solution

  • One needs to add:

     const response = await page.waitForRequest(url => url.url().includes('templateFrom3rdRedirect'));
    

    where 'templateFrom3rdRedirect' is the part of URL unique to the necessary "hop" of the last redirect, that captures call to pseudo-url (that is later found at response.url()).