Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] How to intercept a specific response? #10045

Closed
MarcoSegundo opened this issue Nov 4, 2021 · 1 comment
Closed

[Question] How to intercept a specific response? #10045

MarcoSegundo opened this issue Nov 4, 2021 · 1 comment
Assignees

Comments

@MarcoSegundo
Copy link

Context:

  • Playwright Version: 1.16.3
  • Operating System: Windows
  • Node.js version: 16.13
  • Browser: Chromium

Code Snippet

const { test, expect, chromium } = require('@playwright/test');

test('Intercept response', async () => {
    const browser = await chromium.launch({
        headless: false
    });
    const context = await browser.newContext();
    const page = await context.newPage();
    
    await page.goto("https://demoqa.com/links");

    await page.route('**/created', async route => {
        // Fetch original response.
        const response = await page.request.fetch(route.request());
        // Add a prefix to the title.
        let status = await response.status();
        
    });

    await page.click("//a[@id='created']");

    await browser.close();
});

Describe the question
I need to intercept the response to verify the status is correct

I'm not able to intercept the response with the Fetch API (I saw in this thread that this was the solution).

Description of the error in the image:
image

@yury-s yury-s self-assigned this Nov 4, 2021
@yury-s
Copy link
Member

yury-s commented Nov 4, 2021

I'm not able to intercept the response with the Fetch API (I saw in this thread that this was the solution).

Your page closes before you get response to /created request. Modify your code this way and it will work:

// @ts-check
const { test, expect, chromium } = require('@playwright/test');

test('Intercept response', async () => {
    const browser = await chromium.launch({
        headless: false
    });
    const context = await browser.newContext();
    const page = await context.newPage();
    
    await page.goto("https://demoqa.com/links");
    
    let routeCallback;
    const routePromise = new Promise(r => routeCallback = r);

    await page.route('**/created', async route => {
        console.log('url ' + route.request().url());
        // Fetch original response.
        const response = await page.request.fetch(route.request());
        // Add a prefix to the title.
        let status = await response.status();
        console.log('status = ' + status);
        routeCallback();
    });

    await page.click("//a[@id='created']");

    // Wait before /created response is received.
    await routePromise;

    await browser.close();
});

@yury-s yury-s closed this as completed Nov 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants