pull out newStealthContext

This commit is contained in:
Ralf Vogler 2021-12-28 17:54:18 +01:00
parent 1cec85b9e9
commit 481253a12f

View file

@ -5,12 +5,8 @@ if (!existsSync('auth.json')) {
const { chromium } = require('playwright'); // stealth plugin needs no outdated playwright-extra const { chromium } = require('playwright'); // stealth plugin needs no outdated playwright-extra
(async () => { // stealth with playwright: https://github.com/berstend/puppeteer-extra/issues/454#issuecomment-917437212
const browser = await chromium.launch({ const newStealthContext = async (browser) => {
channel: 'chrome',
headless: true,
});
// stealth with playwright: https://github.com/berstend/puppeteer-extra/issues/454#issuecomment-917437212
const originalUserAgent = await (await (await browser.newContext()).newPage()).evaluate(() => navigator.userAgent); const originalUserAgent = await (await (await browser.newContext()).newPage()).evaluate(() => navigator.userAgent);
console.log('userAgent:', originalUserAgent); // Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/96.0.4664.110 Safari/537.36 console.log('userAgent:', originalUserAgent); // Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/96.0.4664.110 Safari/537.36
const context = await browser.newContext({ const context = await browser.newContext({
@ -46,7 +42,16 @@ const { chromium } = require('playwright'); // stealth plugin needs no outdated
for (let evasion of stealth.callbacks) { for (let evasion of stealth.callbacks) {
await context.addInitScript(evasion.cb, evasion.a); await context.addInitScript(evasion.cb, evasion.a);
} }
return context;
};
// could change to .mjs to get top-level-await, but would then also need to change require to import and dynamic import for stealth below would just add more async/await
(async () => {
const browser = await chromium.launch({
channel: 'chrome',
headless: true,
});
const context = await newStealthContext(browser);
const page = await context.newPage(); const page = await context.newPage();
await page.goto('https://www.epicgames.com/store/en-US/free-games'); await page.goto('https://www.epicgames.com/store/en-US/free-games');
// await expect(page.locator('a[role="button"]:has-text("Sign In")')).toHaveCount(0); // await expect(page.locator('a[role="button"]:has-text("Sign In")')).toHaveCount(0);
@ -63,7 +68,7 @@ const { chromium } = require('playwright'); // stealth plugin needs no outdated
await page.click('button:has-text("Continue")'); await page.click('button:has-text("Continue")');
// it then creates an iframe for the rest // it then creates an iframe for the rest
// await page.frame({ url: /.*store\/purchase.*/ }).click('button:has-text("Place Order")'); // not found because it does not wait for iframe // await page.frame({ url: /.*store\/purchase.*/ }).click('button:has-text("Place Order")'); // not found because it does not wait for iframe
const iframe = page.frameLocator('.webPurchaseContainer iframe') const iframe = page.frameLocator('#webPurchaseContainer iframe')
await iframe.locator('button:has-text("Place Order")').click(); await iframe.locator('button:has-text("Place Order")').click();
await iframe.locator('button:has-text("I Agree")').click(); await iframe.locator('button:has-text("I Agree")').click();
console.log(await page.locator('[data-testid="purchase-cta-button"]').innerText()); console.log(await page.locator('[data-testid="purchase-cta-button"]').innerText());