check isConnected before reconnecting

This commit is contained in:
Lei PAN
2023-05-29 23:12:28 +02:00
parent 74a7f9e885
commit fe8664de77
+68 -32
View File
@@ -85,12 +85,12 @@ class CommandorPage {
async connect_to_browser(ocrResult) {
console.log("connect_to_browser() called");
console.log("browser.isConnected: " + this.browser.isConnected());
// if (!this.browser.isConnected()) {
this.browser = await puppeteer.connect({
browserWSEndpoint: "ws://127.0.0.1:" + this.port + "/devtools/browser",
headless: false, defaultViewport: null
})
// }
if (!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();
// 0 is the last page
pages.forEach((currentPage) => {
@@ -696,7 +696,9 @@ class CommandorPage {
disconnectBrowser() {
try {
logWithDevice("will disconnect browser", this.device);
this.browser.disconnect();
if (this.browser !== undefined) {
this.browser.disconnect();
}
} catch (e) {
console.log(e)
}
@@ -728,8 +730,12 @@ class CommandorPage {
checkResult = await this.ocrChecker.get_result();
}
while (checkResult === OCRResult.SLIDING_CAPTCHA) {
await this.slidingCaptcha(async () => {
checkResult = await this.ocrChecker.get_result();
await this.slidingCaptcha(async (isSuccessful) => {
if (isSuccessful) {
checkResult = await this.ocrChecker.get_result();
} else {
this.isTerminated = true
}
// await this.checkResultWithOcr()
})
await delay(10 * 1000)
@@ -768,10 +774,11 @@ class CommandorPage {
}
} else {
logWithDevice("this.browser.isConnected() is " + this.browser.isConnected(), this.device)
if (this.browser.isConnected()) {
try {
let pages = await this.browser.pages();
try {
if (this.browser.isConnected()) {
logWithDevice("get pages", this.device)
let pages = await this.browser.pages();
logWithDevice("get current page", this.device)
pages.forEach((currentPage) => {
if (currentPage.url() === RDV_URL) {
this.page = currentPage;
@@ -783,18 +790,17 @@ class CommandorPage {
// this.page.await
await this.fillFields(this.page)
await delay(2 * 1000);
} catch (e) {
console.log(e)
this.isTerminated = true
}
} catch
(e) {
console.log(e)
this.isTerminated = true
}
}
// if (!this.browser.isConnected()) {
// this.isTerminated = true;
// }
break;
case OCRResult.SUCCESS:
case
OCRResult.SUCCESS:
// reconnect to page and get url
if (!this.isTerminated) {
await this.connect_to_browser(OCRResult.SUCCESS)
@@ -805,44 +811,64 @@ class CommandorPage {
// }
}
break;
case OCRResult.RECAPTCHA_ERROR:
case
OCRResult.RECAPTCHA_ERROR
:
this.isTerminated = true;
break;
case OCRResult.TO_SKIP:
case
OCRResult.TO_SKIP
:
logWithDevice("TO_SKIP", this.device)
this.device.shell("input tap " + 488 + " " + 1848)
await delay(2000);
break;
case OCRResult.TO_REFRESH:
case
OCRResult.TO_REFRESH
:
await this.connect_to_browser(OCRResult.TO_REFRESH)
break;
case OCRResult.BLOCKED:
case
OCRResult.BLOCKED
:
await this.resetBrowser();
break;
case OCRResult.ONLINE_APPOINTMENT:
case
OCRResult.ONLINE_APPOINTMENT
:
await this.clickOnlineAppointment();
break;
case OCRResult.CONFIRM_RESEND_FORM:
case
OCRResult.CONFIRM_RESEND_FORM
:
// logWithDevice("CONFIRM_RESEND_FORM", this.device)
// await this.checkResultWithOcr();
break;
case OCRResult.PAGE_OPTIMIZATION:
case
OCRResult.PAGE_OPTIMIZATION
:
logWithDevice("PAGE_OPTIMIZATION", this.device)
await this.skipOptimizationPage();
await this.checkResultWithOcr();
break;
case OCRResult.GOOGLE_DISCONNECT:
case
OCRResult.GOOGLE_DISCONNECT
:
logWithDevice("GOOGLE_DISCONNECT", this.device)
this.device.shell("input tap " + 411 + " " + 2100)
await delay(2000);
await this.checkResultWithOcr();
break
case OCRResult.NEED_TO_CLICK_LATE_BTN:
case
OCRResult.NEED_TO_CLICK_LATE_BTN
:
await this.tapLaterBtn()
this.firstStart = true;
await this.loadPage()
break;
case OCRResult.CLOSED:
case
OCRResult.CLOSED
:
await this.closePage();
// this.isTerminated = true;
break;
@@ -850,8 +876,18 @@ class CommandorPage {
await delay(5000);
checkResult = this.ocrChecker.get_result();
}
if (checkResult === undefined) {
await delay(5000);
if (checkResult
===
undefined
) {
await
delay(
5000
)
;
checkResult = this.ocrChecker.get_result();
}
}