From 1e512f490970fa4ec4e2398da1bf8e5348d0f95b Mon Sep 17 00:00:00 2001 From: Lei PAN Date: Tue, 13 Jun 2023 21:21:21 +0200 Subject: [PATCH] add more logs --- src/workers/CommandorPage.js | 118 ++++++++++++++++++++++------------- 1 file changed, 73 insertions(+), 45 deletions(-) diff --git a/src/workers/CommandorPage.js b/src/workers/CommandorPage.js index d9fd681..33c1405 100644 --- a/src/workers/CommandorPage.js +++ b/src/workers/CommandorPage.js @@ -57,7 +57,7 @@ function log(message) { } function logWithDevice(message, device) { - appointmentLogger.log({level: "info", message: device.model() + ":" + message}) + appointmentLogger.log({level: "info", message: device.model() + ":" + device.serial() + ":" + message}) } class CommandorPage { @@ -134,10 +134,16 @@ class CommandorPage { } async loadPage() { - this.device.shell("am start -n com.android.chrome/com.google.android.apps.chrome.Main") - await delay(5 * 1000); - // Connect to the device. logWithDevice(this.device.serial() + ":loadPage() called, with port:" + this.port, this.device); + try { + this.device.shell("am start -n com.android.chrome/com.google.android.apps.chrome.Main") + await delay(5 * 1000); + } catch (e) { + console.log(e) + this.isTerminated = true; + } + + // Connect to the device. if (this.browser) { logWithDevice("browser.isConnected is " + this.browser.isConnected(), this.device); } @@ -262,7 +268,8 @@ class CommandorPage { } async chooseCountry(page) { - if (this.browser.isConnected()) { + logWithDevice("chooseCountry", this.device) + if (this.browser.isConnected() && !this.isTerminated) { try { if (!this.isCountryChoosen) { await page.focus(COUNTRY_ID); @@ -281,8 +288,9 @@ class CommandorPage { } async fillEmail(page) { + logWithDevice("fillEmail", this.device) try { - if (this.browser.isConnected()) { + if (this.browser.isConnected() && !this.isTerminated) { if (!this.isEmailFilled) { await page.focus(EMAIL_ID); await delay(getRandomWaitTime()) @@ -304,8 +312,10 @@ class CommandorPage { } async inputPhoneNumber(page) { + logWithDevice("inputPhoneNumber", this.device) + try { - if (this.browser.isConnected()) { + if (this.browser.isConnected() && !this.isTerminated) { if (!this.isPhoneInput) { await page.focus(PHONE_NUMBER); console.log("will clear phone_number field"); @@ -328,10 +338,12 @@ class CommandorPage { } async inputName(page) { + logWithDevice("inputName", this.device) + await delay(getRandomWaitTime()); try { logWithDevice("input name called with this.browser.isConnected=" + this.browser.isConnected(), this.device) logWithDevice("input name called with this.isNameInput=" + this.isNameInput, this.device) - if (this.browser.isConnected()) { + if (this.browser.isConnected() && !this.isTerminated) { if (!this.isNameInput) { await page.focus(LAST_NAME); await delay(getRandomWaitTime()); @@ -343,7 +355,13 @@ class CommandorPage { } }) await delay(1000); - await page.keyboard.type(this.contact.lastName); + // await page.keyboard.type(this.contact.lastName); + await page.evaluate((lastName) => { + let field = document.getElementById("surname"); + if (field) { + field.value = lastName + } + }, this.contact.lastName) await page.focus(FIRST_NAME); await delay(getRandomWaitTime()); console.log("will clear name field"); @@ -364,8 +382,9 @@ class CommandorPage { } async inputPassportId(page) { + logWithDevice("inputPassportId", this.device) try { - if (this.browser.isConnected()) { + if (this.browser.isConnected() && !this.isTerminated) { if (!this.isPasspordInput) { await page.focus(PASSPORT_ID); await delay(getRandomWaitTime()); @@ -389,8 +408,9 @@ class CommandorPage { } async checkCGU(page) { + logWithDevice("checkCGU", this.device) try { - if (this.browser.isConnected()) { + if (this.browser.isConnected() && !this.isTerminated) { if (!this.cguChecked) { await page.focus(CGU_ID); await page.evaluate(() => { @@ -417,12 +437,14 @@ class CommandorPage { } async chooseStore(page) { + logWithDevice("chooseStore", this.device) + await delay(getRandomWaitTime()) try { - if (this.browser.isConnected()) { + if (!this.page.isClosed() && !this.isTerminated) { if (this.selectedStore !== "random") { - page.focus(PREFER_STORE); - await delay(1000) - page.click(PREFER_STORE); + // page.focus(PREFER_STORE); + await delay(500) + // page.click(PREFER_STORE); let stores = this.selectedStore.split(":") this.choosedStore = stores[Math.floor(Math.random() * stores.length)] await page.select(PREFER_STORE, this.choosedStore); @@ -436,9 +458,9 @@ class CommandorPage { async fillFields(page, airePlanMode) { - log("fillFields called for contact: " + this.contact.mail) - log("this.isFillingFields: " + this.isFillingFields); - if (!this.isFillingFields) { + logWithDevice("fillFields called for contact: " + this.contact.mail, this.device) + logWithDevice("this.isFillingFields: " + this.isFillingFields, this.device); + if (!this.isFillingFields && !this.isTerminated) { this.isFillingFields = true; if (airePlanMode) { // await this.enableDisableAirPlanMode(); @@ -496,42 +518,48 @@ class CommandorPage { } async resolveCaptcha(page) { + logWithDevice("resolveCaptcha", this.device) if (RDV_URL.includes("192")) { // await this.push_message_to_queue(PublishType.SUCCESS) await delay(100000) return } - //check whether there is captcha - let pageContent = await page.content() - let hasCaptcha = pageContent.includes("g-recaptcha-response") - if (hasCaptcha) { - // await this.clickCheckbox() - await delay(1000) - this.captchaSolver = new SolveCaptcha(page); - await this.captchaSolver.start((solution) => { - logWithDevice("solution is: " + solution, this.device); - if (solution !== ERROR_CAPTCHA_UNSOLVABLE && solution !== TWO_CAPTCHA_CONNECTION_FAILED) { - try { - if (!page.isClosed()) { - if (this.browser.isConnected()) { - page.evaluate((solution) => { - let element = document.getElementById("g-recaptcha-response"); - if (element != null) - document.getElementById("g-recaptcha-response").innerHTML = solution; - }, solution) - this.clickValid(); + try { + //check whether there is captcha + let pageContent = await page.content() + let hasCaptcha = pageContent.includes("g-recaptcha-response") + if (hasCaptcha) { + // await this.clickCheckbox() + await delay(1000) + this.captchaSolver = new SolveCaptcha(page); + await this.captchaSolver.start((solution) => { + logWithDevice("solution is: " + solution, this.device); + if (solution !== ERROR_CAPTCHA_UNSOLVABLE && solution !== TWO_CAPTCHA_CONNECTION_FAILED) { + try { + if (!page.isClosed() && !this.isTerminated) { + if (this.browser.isConnected()) { + page.evaluate((solution) => { + let element = document.getElementById("g-recaptcha-response"); + if (element != null) + document.getElementById("g-recaptcha-response").innerHTML = solution; + }, solution) + this.clickValid(); + } } + } catch (e) { + log(e) + this.isTerminated = true; } - } catch (e) { - log(e) + } else { this.isTerminated = true; } - } else { - this.isTerminated = true; - } - }) - } else { - await this.clickValid(); + }) + } else { + await this.clickValid(); + } + } catch (e) { + console.log(e) + this.isTerminated = true; } }