can close the last page

This commit is contained in:
Lei PAN
2023-05-25 23:52:43 +02:00
parent 9455aa3d4f
commit b42d257ab9
5 changed files with 72 additions and 22 deletions
-2
View File
@@ -13,14 +13,12 @@
"@nodegui/nodegui": "^0.57.1",
"alert": "^5.1.1",
"axios": "^0.27.2",
"electron-squirrel-startup": "^1.0.0",
"mongodb": "^4.9.1",
"mongoose": "^6.5.4",
"node-schedule": "^2.1.0",
"node-tesseract-ocr": "^2.2.1",
"node-wget": "^0.4.3",
"node-xlsx": "^0.21.0",
"opencv4nodejs": "wandenberg/opencv4nodejs",
"playwright": "^1.32.1",
"puppeteer": "^15.5.0",
"read-ini-file": "^3.0.1",
-1
View File
@@ -198,7 +198,6 @@ async function startBookWithNumbers(startNumber, endNumber, selectedStore, pathT
console.log('Output was:\n', output);
}
// start chrome
device.shell("am start -n com.android.chrome/com.google.android.apps.chrome.Main")
// device.shell("am start -n com.brave.browser/com.google.android.apps.chrome.Main")
return attributedPort
}
+1
View File
@@ -9,6 +9,7 @@ const OCRResult = {
PAGE_OPTIMIZATION: Symbol("PAGE_OPTIMIZATION"),
CONFIRM_RESEND_FORM: Symbol("CONFIRM_RESEND_FORM"),
RECHECK: Symbol("RECHECK"),
CLOSED: Symbol("CLOSED"),
RECAPTCHA_ERROR: Symbol("RECAPTCHA_ERROR"),
}
module.exports = OCRResult
+68 -19
View File
@@ -14,6 +14,7 @@ const SlidingCaptchaSolver = require("./SlidingCaptchaSolver");
const OCRChecker = require("./OCRChecker");
const {browser} = require("yarn/lib/cli");
const {disconnect} = require("mongoose");
const {execSync} = require("child_process");
// const RDV_URL = "http://192.168.0.44:8000/test_appointment.html"
const RDV_URL = "https://rendezvousparis.hermes.com/client/register";
const BLANK_URL = "about:blank"
@@ -81,7 +82,7 @@ class CommandorPage {
this.isEmailFilled = false;
this.isCountryChoosen = false;
this.isPhoneInput = false;
this.firstStart = false;
this.firstStart = true;
}
@@ -131,29 +132,39 @@ 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("loadPage() called, with port:" + this.port, this.device);
if (this.browser) {
logWithDevice("browser.isConnected is " + this.browser.isConnected(), this.device);
}
try {
this.browser = await puppeteer.connect({
browserWSEndpoint: "ws://127.0.0.1:" + this.port + "/devtools/browser",
headless: false, defaultViewport: null
})
this.page = await this.browser.newPage();
logWithDevice("connecting to browser:" + this.port, this.device);
while (this.browser === undefined || !this.browser.isConnected()) {
this.browser = await puppeteer.connect({
browserWSEndpoint: "ws://127.0.0.1:" + this.port + "/devtools/browser",
headless: false, defaultViewport: null
})
// let pages = await this.browser.pages();
// this.page = pages[0];
this.page = await this.browser.newPage();
this.page.on("load", (loadedPage) => {
this.onPageLoad(loadedPage)
})
this.page.on("load", (loadedPage) => {
this.onPageLoad(loadedPage);
})
this.page.on("response", (response) => {
this.onResponse(response)
})
this.page.on("response", (response) => {
this.onResponse(response)
})
await delay(3 * 1000);
}
} catch (e) {
await this.checkResultWithOcr()
console.log(e)
// this.isTerminated = true
}
if (this.page !== undefined) {
try {
logWithDevice("will open google", this.device)
if (!this.isTerminated) {
@@ -170,7 +181,8 @@ class CommandorPage {
await delay(10 * 1000);
await this.clickOnlineAppointment();
await delay(2000);
await this.clickOnlineAppointment();
if (!this.isTerminated)
await this.clickOnlineAppointment();
} else {
this.isTerminated = true;
}
@@ -191,7 +203,6 @@ class CommandorPage {
// }
// this.page.close()
// this.device.close()
clearInterval(intervalTask)
cancel()
// return this.browser
@@ -618,7 +629,7 @@ class CommandorPage {
let reserve = ReserveResultPojo.create_from_contact(this.contact, id, url, this.choosedStore, publishType);
reserve.source_from = this.device.model();
await this.mongoManager.saveReserveToDb(reserve.to_mongo_dict())
await this.deleteFromBlackList()
// await this.deleteFromBlackList()
// await this.resetBrowser()
this.isTerminated = true
}
@@ -719,19 +730,34 @@ class CommandorPage {
switch (checkResult) {
case OCRResult.FILL_FIELD:
console.log("browser.isConnected: " + this.browser.isConnected());
if (!this.browser.isConnected()) {
while (!this.browser.isConnected()) {
logWithDevice("trying to connect to browser", this.device)
// let cmd = 'adb -s ' + this.device.serial() + " forward tcp:" + this.port + " localabstract:chrome_devtools_remote";
// console.log("cmd is " + cmd);
// const output = execSync(cmd, {encoding: 'utf-8'}); // the default is 'buffer'
// console.log('Output was:\n', output);
this.browser = await puppeteer.connect({
browserWSEndpoint: "ws://127.0.0.1:" + this.port + "/devtools/browser",
headless: false, defaultViewport: null
})
let pages = await this.browser.pages();
this.page = pages[0];
pages.forEach((currentPage) => {
if (currentPage.url() === RDV_URL) {
this.page = currentPage;
}
})
await this.page.bringToFront();
// this.page = pages;
// this.page.await
await delay(5 * 1000);
}
await this.fillFields(this.page)
break;
case OCRResult.SUCCESS:
// reconnect to page and get url
await this.connect_to_browser(OCRResult.SUCCESS);
if (!this.isTerminated) {
await this.connect_to_browser(OCRResult.SUCCESS);
}
break;
case OCRResult.RECAPTCHA_ERROR:
this.isTerminated = true;
@@ -763,6 +789,10 @@ class CommandorPage {
this.firstStart = true;
await this.loadPage()
break;
case OCRResult.CLOSED:
await this.closePage();
// this.isTerminated = true;
break;
default:
await delay(5000);
checkResult = ocrChecker.get_result();
@@ -773,6 +803,25 @@ class CommandorPage {
}
}
async closePage() {
while (!this.browser.isConnected()) {
logWithDevice("trying to connect to browser", this.device)
this.browser = await puppeteer.connect({
browserWSEndpoint: "ws://127.0.0.1:" + this.port + "/devtools/browser",
headless: false, defaultViewport: null
})
await delay(2 * 1000);
}
console.log("will close all pages");
let pages = await this.browser.pages();
await pages[0].close()
console.log("page closed");
// pages.forEach((currentPage) => {
// currentPage.close();
// })
// await this.browser.close();
}
async tapLaterBtn() {
logWithDevice("tapLaterBtn", this.device)
let model = this.device.model();
+3
View File
@@ -32,6 +32,7 @@ const PAGE_OPTIMIZATION_CHROME_FR_2 = "Vous pouvez modifier vos options a tout m
const PAGE_OPTIMIZATION_CHROME_FR_3 = "Vous pouvez effectuer des modifications"
const ONLINE_APPOINTMENT = "Online Appointment"
const CONFIRM_RESEND_FORM_FR = "Confirmer le nouvel envoi"
const CLOSED_MESSAGE_FR = "Depuis plus de 130 ans"
class OCRChecker {
@@ -72,6 +73,8 @@ class OCRChecker {
return OCRResult.PAGE_OPTIMIZATION
} else if (result.includes(CONFIRM_RESEND_FORM_FR)) {
return OCRResult.CONFIRM_RESEND_FORM
} else if (result.includes(CLOSED_MESSAGE_FR)) {
return OCRResult.CLOSED
}
}