add more logs

This commit is contained in:
Lei PAN
2023-06-13 21:21:21 +02:00
parent 577ca37c24
commit 1e512f4909
+73 -45
View File
@@ -57,7 +57,7 @@ function log(message) {
} }
function logWithDevice(message, device) { function logWithDevice(message, device) {
appointmentLogger.log({level: "info", message: device.model() + ":" + message}) appointmentLogger.log({level: "info", message: device.model() + ":" + device.serial() + ":" + message})
} }
class CommandorPage { class CommandorPage {
@@ -134,10 +134,16 @@ class CommandorPage {
} }
async loadPage() { 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); 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) { if (this.browser) {
logWithDevice("browser.isConnected is " + this.browser.isConnected(), this.device); logWithDevice("browser.isConnected is " + this.browser.isConnected(), this.device);
} }
@@ -262,7 +268,8 @@ class CommandorPage {
} }
async chooseCountry(page) { async chooseCountry(page) {
if (this.browser.isConnected()) { logWithDevice("chooseCountry", this.device)
if (this.browser.isConnected() && !this.isTerminated) {
try { try {
if (!this.isCountryChoosen) { if (!this.isCountryChoosen) {
await page.focus(COUNTRY_ID); await page.focus(COUNTRY_ID);
@@ -281,8 +288,9 @@ class CommandorPage {
} }
async fillEmail(page) { async fillEmail(page) {
logWithDevice("fillEmail", this.device)
try { try {
if (this.browser.isConnected()) { if (this.browser.isConnected() && !this.isTerminated) {
if (!this.isEmailFilled) { if (!this.isEmailFilled) {
await page.focus(EMAIL_ID); await page.focus(EMAIL_ID);
await delay(getRandomWaitTime()) await delay(getRandomWaitTime())
@@ -304,8 +312,10 @@ class CommandorPage {
} }
async inputPhoneNumber(page) { async inputPhoneNumber(page) {
logWithDevice("inputPhoneNumber", this.device)
try { try {
if (this.browser.isConnected()) { if (this.browser.isConnected() && !this.isTerminated) {
if (!this.isPhoneInput) { if (!this.isPhoneInput) {
await page.focus(PHONE_NUMBER); await page.focus(PHONE_NUMBER);
console.log("will clear phone_number field"); console.log("will clear phone_number field");
@@ -328,10 +338,12 @@ class CommandorPage {
} }
async inputName(page) { async inputName(page) {
logWithDevice("inputName", this.device)
await delay(getRandomWaitTime());
try { try {
logWithDevice("input name called with this.browser.isConnected=" + this.browser.isConnected(), this.device) logWithDevice("input name called with this.browser.isConnected=" + this.browser.isConnected(), this.device)
logWithDevice("input name called with this.isNameInput=" + this.isNameInput, 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) { if (!this.isNameInput) {
await page.focus(LAST_NAME); await page.focus(LAST_NAME);
await delay(getRandomWaitTime()); await delay(getRandomWaitTime());
@@ -343,7 +355,13 @@ class CommandorPage {
} }
}) })
await delay(1000); 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 page.focus(FIRST_NAME);
await delay(getRandomWaitTime()); await delay(getRandomWaitTime());
console.log("will clear name field"); console.log("will clear name field");
@@ -364,8 +382,9 @@ class CommandorPage {
} }
async inputPassportId(page) { async inputPassportId(page) {
logWithDevice("inputPassportId", this.device)
try { try {
if (this.browser.isConnected()) { if (this.browser.isConnected() && !this.isTerminated) {
if (!this.isPasspordInput) { if (!this.isPasspordInput) {
await page.focus(PASSPORT_ID); await page.focus(PASSPORT_ID);
await delay(getRandomWaitTime()); await delay(getRandomWaitTime());
@@ -389,8 +408,9 @@ class CommandorPage {
} }
async checkCGU(page) { async checkCGU(page) {
logWithDevice("checkCGU", this.device)
try { try {
if (this.browser.isConnected()) { if (this.browser.isConnected() && !this.isTerminated) {
if (!this.cguChecked) { if (!this.cguChecked) {
await page.focus(CGU_ID); await page.focus(CGU_ID);
await page.evaluate(() => { await page.evaluate(() => {
@@ -417,12 +437,14 @@ class CommandorPage {
} }
async chooseStore(page) { async chooseStore(page) {
logWithDevice("chooseStore", this.device)
await delay(getRandomWaitTime())
try { try {
if (this.browser.isConnected()) { if (!this.page.isClosed() && !this.isTerminated) {
if (this.selectedStore !== "random") { if (this.selectedStore !== "random") {
page.focus(PREFER_STORE); // page.focus(PREFER_STORE);
await delay(1000) await delay(500)
page.click(PREFER_STORE); // page.click(PREFER_STORE);
let stores = this.selectedStore.split(":") let stores = this.selectedStore.split(":")
this.choosedStore = stores[Math.floor(Math.random() * stores.length)] this.choosedStore = stores[Math.floor(Math.random() * stores.length)]
await page.select(PREFER_STORE, this.choosedStore); await page.select(PREFER_STORE, this.choosedStore);
@@ -436,9 +458,9 @@ class CommandorPage {
async fillFields(page, airePlanMode) { async fillFields(page, airePlanMode) {
log("fillFields called for contact: " + this.contact.mail) logWithDevice("fillFields called for contact: " + this.contact.mail, this.device)
log("this.isFillingFields: " + this.isFillingFields); logWithDevice("this.isFillingFields: " + this.isFillingFields, this.device);
if (!this.isFillingFields) { if (!this.isFillingFields && !this.isTerminated) {
this.isFillingFields = true; this.isFillingFields = true;
if (airePlanMode) { if (airePlanMode) {
// await this.enableDisableAirPlanMode(); // await this.enableDisableAirPlanMode();
@@ -496,42 +518,48 @@ class CommandorPage {
} }
async resolveCaptcha(page) { async resolveCaptcha(page) {
logWithDevice("resolveCaptcha", this.device)
if (RDV_URL.includes("192")) { if (RDV_URL.includes("192")) {
// await this.push_message_to_queue(PublishType.SUCCESS) // await this.push_message_to_queue(PublishType.SUCCESS)
await delay(100000) await delay(100000)
return return
} }
//check whether there is captcha try {
let pageContent = await page.content() //check whether there is captcha
let hasCaptcha = pageContent.includes("g-recaptcha-response") let pageContent = await page.content()
if (hasCaptcha) { let hasCaptcha = pageContent.includes("g-recaptcha-response")
// await this.clickCheckbox() if (hasCaptcha) {
await delay(1000) // await this.clickCheckbox()
this.captchaSolver = new SolveCaptcha(page); await delay(1000)
await this.captchaSolver.start((solution) => { this.captchaSolver = new SolveCaptcha(page);
logWithDevice("solution is: " + solution, this.device); await this.captchaSolver.start((solution) => {
if (solution !== ERROR_CAPTCHA_UNSOLVABLE && solution !== TWO_CAPTCHA_CONNECTION_FAILED) { logWithDevice("solution is: " + solution, this.device);
try { if (solution !== ERROR_CAPTCHA_UNSOLVABLE && solution !== TWO_CAPTCHA_CONNECTION_FAILED) {
if (!page.isClosed()) { try {
if (this.browser.isConnected()) { if (!page.isClosed() && !this.isTerminated) {
page.evaluate((solution) => { if (this.browser.isConnected()) {
let element = document.getElementById("g-recaptcha-response"); page.evaluate((solution) => {
if (element != null) let element = document.getElementById("g-recaptcha-response");
document.getElementById("g-recaptcha-response").innerHTML = solution; if (element != null)
}, solution) document.getElementById("g-recaptcha-response").innerHTML = solution;
this.clickValid(); }, solution)
this.clickValid();
}
} }
} catch (e) {
log(e)
this.isTerminated = true;
} }
} catch (e) { } else {
log(e)
this.isTerminated = true; this.isTerminated = true;
} }
} else { })
this.isTerminated = true; } else {
} await this.clickValid();
}) }
} else { } catch (e) {
await this.clickValid(); console.log(e)
this.isTerminated = true;
} }
} }