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

waitForResponse() #5781

Closed
64chevy opened this issue Mar 10, 2021 · 2 comments
Closed

waitForResponse() #5781

64chevy opened this issue Mar 10, 2021 · 2 comments

Comments

@64chevy
Copy link

64chevy commented Mar 10, 2021

I just to know that Playwright does not work on CentOS so I moved to Puppeteer few days ago and been stuck on this thing ever since. Basically what I am trying to do is load up a page, do .click() and the the button then sends an xHr request 2 times (one with OPTIONS method & one with POST) and gives the response in JSON. Everything worked fine in playwright, the requests were sent successfully and response was good but in Puppeteer, the request is fine but the response is different. Here are both of the codes:

Playwright:

const {webkit, devices} = require("playwright-webkit");
const iPhone11 = devices["iPhone 11 Pro"];
const fs = require('fs');

var userAgent =
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36"

var options = {
    args: [],
    ignoreDefaultArgs: ["--mute-audio", "--hide-scrollbars"],
    ignoreHTTPSErrors: true,
};

(async () => {
    const browser = await webkit.launch(options);

    let emulateTemplate = {
        ...iPhone11
    };
    emulateTemplate.viewport.width = getRandomInt(320, 1920);
    emulateTemplate.viewport.height = getRandomInt(320, 1920);

    const context = await browser.newContext({
        ...emulateTemplate,
        deviceScaleFactor: getRandomInt(1, 3),
        isMobile: Math.random() > 0.5,
        hasTouch: Math.random() > 0.5,
        userAgent: userAgent,
    });

	const cookies = fs.readFileSync('cookies.json', 'utf8');
	const deserializedCookies = JSON.parse(cookies);
		
	await context.addCookies(deserializedCookies)
	
    const page = await context.newPage();
    url = "https://example.com";

    console.log("~browser started");
  
    await page.goto(url, {
        waitUntil: "load",
        timeout: 0
    });
	
    console.log("~u loaded");
		
    const [response] = await Promise.all([
		page.waitForResponse(/[\\/]run/),
        page.click('.go-button'),
    ]);

	console.log(await response.json())

    await browser.close();

})();

function getRandomInt(a, b) {
    const min = Math.min(a, b);
    const max = Math.max(a, b);
    const diff = max - min + 1;
    return min + Math.floor(Math.random() * Math.floor(diff));
}

Puppeteer:

const webkit = require('puppeteer');
const iPhone = webkit.devices['iPhone 11 Pro'];
const fs = require('fs');

var url = "https://www.example.com";

var userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.192 Safari/537.36"

var options = {
    ignoreDefaultArgs: ["--mute-audio", "--hide-scrollbars"],
    ignoreHTTPSErrors: true,
    defaultViewport: {
        width: getRandomInt(320, 1920),
        height: getRandomInt(320, 1920),
        isMobile: Math.random() > 0.5,
        hasTouch: Math.random() > 0.5,
    }
};

(async () => {
    const browser = await webkit.launch(options);
	const page = await browser.newPage();

    // Enable request interception
    //await page.setRequestInterception(true);

    //set iPhone as emulator
    await page.emulate(iPhone);

	// set user agent (override the default headless User Agent)
	await page.setUserAgent(userAgent);

	const cookies = fs.readFileSync('cookies.json', 'utf8');
	const deserializedCookies = JSON.parse(cookies);
	
	//set cookie
	await page.setCookie(...deserializedCookies);
	
    console.log("~browser started");

    await page.goto(url, {
        waitUntil: "load",
        timeout: 0
    });
    
    console.log("~u loaded");
    
    const [response] = await Promise.all([
        page.waitForResponse(response => response.url().includes('/run/') && (response.request().method() === 'POST')),
        page.click('.go-button')
    ]);
    
	console.log(response.url(), await response.json())

	await browser.close();
})();

function getRandomInt(a, b) {
    const min = Math.min(a, b);
    const max = Math.max(a, b);
    const diff = max - min + 1;
    return min + Math.floor(Math.random() * Math.floor(diff));
}

Would be grateful if someone can help me on this. I literally can't figure out whats wrong.

@yury-s
Copy link
Member

yury-s commented Mar 10, 2021

Since your question is about Puppeteer you should ask it in their bug tracker. We'll respond about CentOS in the other bug that you filed about it.

@yury-s yury-s closed this as completed Mar 10, 2021
@64chevy
Copy link
Author

64chevy commented Mar 11, 2021

Their bug report is dead. If you or someone else knows the difference between theirs and Playwright waitForResponse & click(), it would be helpful. I am stuck on this thing.

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