From cdd47327b190d1423f573ec90e01aab2fe3c0c68 Mon Sep 17 00:00:00 2001 From: PAN Lei Date: Wed, 7 Sep 2022 21:24:07 +0200 Subject: [PATCH] can cancel promis --- main.js | 47 +++++++++++++++++++++++++++++------ src/workers/CommandorPage.js | 48 +++++++++++++++++++++++++----------- 2 files changed, 72 insertions(+), 23 deletions(-) diff --git a/main.js b/main.js index f4219a3..7177b8d 100644 --- a/main.js +++ b/main.js @@ -12,17 +12,48 @@ function delay(delayInms) { } const mongoManager = new MongoManager(); -mongoManager.connect() let excelUtil = new ExcelUtil(); let contactList = excelUtil.readContacts(); -(async () => { + // Connect to the device. - const [device] = await android.devices(); + +async function startBook(contactPojo, device) { console.log(`Model: ${device.model()}`); console.log(`Serial: ${device.serial()}`); - let commandor = new CommandorPage(contactList[3], device, mongoManager); -//read contacts form excel - await commandor.loadPage(); -// Take screenshot of the whole device. -})(); \ No newline at end of file + let commandor = new CommandorPage(contactPojo, device, mongoManager); + //read contacts form excel + return await commandor.loadPage(); +} + +mongoManager.connect().then(r => console.log("mongo connected")) + +async function printFiles() { + let devices = await android.devices(); + await contactList.reduce(async (promise, contactPojo) => { + // This line will wait for the last async function to finish. + // The first iteration uses an already resolved Promise + // so, it will immediately continue. + await promise; + const contents = await startBook(contactPojo, devices[0]); + console.log(contents); + }, Promise.resolve()); +} + +(async () => { + printFiles() +})() + + +// android.devices().then((device_list) => { +// contactList.forEach((contactPojo) => { +// device_list.forEach((device) => { +// (async () => { +// await startBook(contactPojo, device) +// })() +// } +// ) +// } +// ) +// }) + diff --git a/src/workers/CommandorPage.js b/src/workers/CommandorPage.js index b2b559c..73e5cf0 100644 --- a/src/workers/CommandorPage.js +++ b/src/workers/CommandorPage.js @@ -1,11 +1,11 @@ const {_android: android} = require("playwright"); + const SolveCaptcha = require("./SolveCaptcha"); const ReserveResultPojo = require("../models/ReserveResultPojo"); const PublishType = require("../models/PublishType"); -const {aws4} = require("mongodb/src/deps"); -// const RDV_URL = "http://192.168.0.44:8000/test_appointment.html" -const RDV_URL = "https://rendezvousparis.hermes.com/client/register"; +const RDV_URL = "http://192.168.0.44:8000/test_appointment.html" +// const RDV_URL = "https://rendezvousparis.hermes.com/client/register"; const COUNTRY_ID = "#phone_country" const PHONE_NUMBER = "#phone_number" @@ -17,6 +17,8 @@ const CGU_ID = "#cgu" const PROCESSING_ID = "#processing" const PASSPORT_ID = "#passport_id" +const TIME_OUT = 60 * 1000 * 10//10mins + const CONFIRMED_MESSAGE = "Your request for a Leather Goods appointment has been registered" const CONFIRMED_MESSAGE_FR = "Votre demande de rendez-vous Maroquinerie a bien été enregistrée et nous vous en remercions." const MESSAGE_URL_VALIDATION_FR = "Nous avons envoyé un lien par e-mail." @@ -49,31 +51,43 @@ class CommandorPage { this.device = device; this.mongoManager = mongoManager; this.isFillingFields = false; + this.isTerminated = false; } async loadPage() { // Connect to the device. console.log("loadPage() called"); - - // --------------------- Browser ----------------------- - // Launch Chrome browser. await this.device.shell('am force-stop com.android.chrome'); const context = await this.device.launchBrowser({command: '--incognito'}); // Use BrowserContext as usual. this.page = await context.newPage(); - this.page.on("load", (loaddePage) => { - this.onPageLoad(loaddePage) + this.page.on("load", (loadedPage) => { + this.onPageLoad(loadedPage) }) await this.page.goto(RDV_URL); - // await this.fillFields(page) - console.log(await this.page.evaluate(() => window.location.href)); //wait 10 mins - await delay(10 * 60 * 1000); - - // Close the device. - await this.device.close(); + + let cancel + const intervalTask = setInterval(() => { + if (this.isTerminated) { + console.log("request terminated, will close device") + context.close() + // this.page.close() + this.device.close() + clearInterval(intervalTask) + cancel() + return context + } + }, 10 * 1000)//interval of 10 seconds + + await new Promise(function (fulfill, reject) { + cancel = function () { + fulfill(Promise.resolve()) + } + setTimeout(fulfill, TIME_OUT, 5) + }).then(console.log) } async chooseCountry(page) { @@ -125,7 +139,7 @@ class CommandorPage { await delay(1000) await page.click(PREFER_STORE); await page.selectOption(PREFER_STORE, "faubourg"); - await page.click(PREFER_STORE); + // await page.click(PREFER_STORE); } @@ -161,6 +175,10 @@ class CommandorPage { } async resolveCaptcha(page) { + if (RDV_URL.includes("192")) { + this.isTerminated = true; + return + } this.captchaSolver = new SolveCaptcha(page); await this.captchaSolver.start((solution) => { console.log("solution is: " + solution);