prime-gaming: fix wait for sign in, exit if not, arg show otherwise headless

This commit is contained in:
Ralf Vogler 2022-03-31 19:41:42 +02:00
parent 3b3289dddf
commit f0bf3c1671

View file

@ -3,6 +3,8 @@ import path from 'path';
import { __dirname, stealth } from './util.js'; import { __dirname, stealth } from './util.js';
const debug = process.env.PWDEBUG == '1'; // runs headful and opens https://playwright.dev/docs/inspector const debug = process.env.PWDEBUG == '1'; // runs headful and opens https://playwright.dev/docs/inspector
const show = process.argv.includes('show', 2);
const headless = !debug && !show;
// const URL_LOGIN = 'https://www.amazon.de/ap/signin'; // wrong. needs some session args to be valid? // const URL_LOGIN = 'https://www.amazon.de/ap/signin'; // wrong. needs some session args to be valid?
const URL_CLAIM = 'https://gaming.amazon.com/home'; const URL_CLAIM = 'https://gaming.amazon.com/home';
@ -10,8 +12,8 @@ const TIMEOUT = 20 * 1000; // 20s, default is 30s
// https://playwright.dev/docs/auth#multi-factor-authentication // https://playwright.dev/docs/auth#multi-factor-authentication
const context = await chromium.launchPersistentContext(path.resolve(__dirname, 'userDataDir'), { const context = await chromium.launchPersistentContext(path.resolve(__dirname, 'userDataDir'), {
channel: 'chrome', // https://playwright.dev/docs/browsers#google-chrome--microsoft-edge // channel: 'chrome', // https://playwright.dev/docs/browsers#google-chrome--microsoft-edge, chrome will not work on arm64 linux, only chromium which is the default
headless: false, headless,
viewport: { width: 1280, height: 1280 }, viewport: { width: 1280, height: 1280 },
userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36', // see replace of Headless in newStealthContext above. TODO update if browser is updated! userAgent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36', // see replace of Headless in newStealthContext above. TODO update if browser is updated!
locale: "en-US", // ignore OS locale to be sure to have english text for locators locale: "en-US", // ignore OS locale to be sure to have english text for locators
@ -32,9 +34,15 @@ const clickIfExists = async selector => {
}; };
await page.goto(URL_CLAIM, {waitUntil: 'domcontentloaded'}); // default 'load' takes forever await page.goto(URL_CLAIM, {waitUntil: 'domcontentloaded'}); // default 'load' takes forever
await Promise.any(['button:has-text("Sign in")', '[data-a-target="user-dropdown-first-name-text"]'].map(s => page.waitForSelector(s)));
await clickIfExists('[aria-label="Cookies usage disclaimer banner"] button:has-text("Accept Cookies")'); // to not waste screen space in --debug await clickIfExists('[aria-label="Cookies usage disclaimer banner"] button:has-text("Accept Cookies")'); // to not waste screen space in --debug
while (await page.locator('button:has-text("Sign in")').count() > 0) { while (await page.locator('button:has-text("Sign in")').count() > 0) {
console.error("Not signed in anymore. Please login and then navigate to the 'Free Games' page."); console.error('Not signed in anymore.');
if (headless) {
console.log('Please run `node prime-gaming show` to login in the opened browser.');
await context.close(); // not needed?
process.exit(1);
}
await page.click('button:has-text("Sign in")'); await page.click('button:has-text("Sign in")');
if (!debug) context.setDefaultTimeout(0); // give user time to log in without timeout if (!debug) context.setDefaultTimeout(0); // give user time to log in without timeout
await page.waitForNavigation({url: 'https://gaming.amazon.com/home?signedIn=true'}); await page.waitForNavigation({url: 'https://gaming.amazon.com/home?signedIn=true'});