add timeout for pages()

This commit is contained in:
2023-11-30 16:19:48 +01:00
parent 939b98e3a3
commit 8ec0443a01
3 changed files with 58 additions and 50 deletions
+52 -48
View File
@@ -32,6 +32,8 @@ const MESSAGE_URL_VALIDATION_FR = "Nous avons envoyé un lien par e-mail."
const MESSAGE_URL_VALIDATION_EN = "Please click on the link we sent by email"
const REGEX_RDV_URL = "https:\/\/rendezvousparis\.hermes\.com\/client\/register\/[A-Z0-9]+"
const DEFAULT_STORE = 'faubourg';
const timeout = (prom, time) =>
Promise.race([prom, new Promise((_r, rej) => setTimeout(rej, time))]);
function delay(delayInMs) {
return new Promise(resolve => {
@@ -178,8 +180,9 @@ class CommandorPage {
if (this.isTerminated) {
log(this.device.model() + ":request terminated, send cancel()");
try {
if (this.page !== undefined)
if (this.page !== undefined) {
await this.page.close()
}
} catch (e) {
console.log(e)
}
@@ -786,7 +789,6 @@ class CommandorPage {
}
}
async checkResultWithOcr() {
logWithDevice("checkResultWithOcr() called.", this.device)
if (this.device.model() === "M2006C3LG")
@@ -806,7 +808,9 @@ class CommandorPage {
await this.slidingCaptcha(async (isSuccessful) => {
logWithDevice("SLIDING_CAPTCHA result is " + isSuccessful, this.device)
if (isSuccessful) {
await delay(5 * 1000)
checkResult = await this.ocrChecker.get_result();
console.log(checkResult);
} else {
checkResult = OCRResult.TERMINAED
this.isTerminated = true
@@ -836,41 +840,61 @@ class CommandorPage {
browserWSEndpoint: "ws://127.0.0.1:" + this.port + "/devtools/browser",
headless: false, defaultViewport: null
})
logWithDevice("get pages", this.device)
let pages = await this.browser.pages();
try {
for (const currentPage of pages) {
logWithDevice("get pages", this.device)
// let pages = await this.browser.pages();
let pages = await timeout(this.browser.pages(), 5 * 1000);
pages.forEach((currentPage) => {
if (currentPage.url() === RDV_URL) {
logWithDevice("get content", this.device)
let pageContent = await currentPage.content()
logWithDevice("content:" + pageContent, this.device)
if (!pageContent.includes("geo.captcha-delivery.com")) {
this.page = currentPage;
break
} else {
try {
await currentPage.close()
} catch (e) {
console.log(e)
}
}
this.page = currentPage;
} else {
try {
await currentPage.close()
currentPage.close()
} catch (e) {
console.log(e)
}
}
}
} catch (e) {
this.isTerminated = true;
console.log(e)
}
logWithDevice("this.page.bringToFront();", this.device)
})
logWithDevice("this.page.bringToFront();", this.device)
await this.page.bringToFront();
await this.fillFields(this.page)
await delay(2 * 1000);
} catch (e) {
console.log(e)
await this.resetBrowser()
// this.isTerminated = true;
}
// try {
// for (const currentPage of pages) {
// if (currentPage.url() === RDV_URL) {
// logWithDevice("get content", this.device)
// let pageContent = await currentPage.content()
// logWithDevice("content:" + pageContent, this.device)
// if (!pageContent.includes("geo.captcha-delivery.com")) {
// this.page = currentPage;
// break
// } else {
// try {
// await currentPage.close()
// } catch (e) {
// console.log(e)
// }
// }
// } else {
// try {
// await currentPage.close()
// } catch (e) {
// console.log(e)
// }
// }
// }
// } catch (e) {
// this.isTerminated = true;
// console.log(e)
// }
await this.page.bringToFront();
await this.fillFields(this.page)
await delay(2 * 1000);
} catch (e) {
console.log(e)
this.isTerminated = true
@@ -879,27 +903,7 @@ class CommandorPage {
logWithDevice("this.browser.isConnected() is " + this.browser.isConnected(), this.device)
try {
if (this.browser.isConnected()) {
// logWithDevice("get pages", this.device)
// let pages = await this.browser.pages();
logWithDevice("will use old page", this.device)
// try {
// pages.forEach((currentPage) => {
// if (currentPage.url() === RDV_URL) {
// this.page = currentPage;
// } else {
// // try {
// // if (!currentPage.isClosed()) {
// // currentPage.close()
// // }
// // } catch (e) {
// // console.log(e)
// // }
// }
// })
// } catch (e) {
// this.isTerminated = true
// console.log(e)
// }
logWithDevice("this.page.bringToFront();", this.device)
await this.page.bringToFront();
// this.page = pages;
+1
View File
@@ -194,6 +194,7 @@ class OCRChecker {
async take_screen_shot() {
let name = this.get_file_name()
console.log("will take screenshot for " + this.device.model() + ":" + this.device.serial())
console.log("name is " + name)
let stdout1 = await exec("adb -s " + this.device.serial() + " exec-out screencap -p > " + name)
await delay(5000);
console.log(`stdout: ${stdout1}`);
+5 -2
View File
@@ -34,17 +34,18 @@ class SlidingCaptchaSolver {
let stdout1 = await exec("adb -s " + device.serial() + " exec-out screencap -p > " + name)
// await delay(5000);
console.log(`stdout: ${stdout1}`);
await delay(1000);
await delay(5000);
}
async sliding_captcha(device, onResult) {
// take screenshot
console.log("take screenshot")
console.log("sliding_captcha.take screenshot")
let blockedFileName = uuidv4();
let blockedImageFileName = "blocked_" + blockedFileName + ".png"
await this.take_screen_shot(blockedImageFileName, device)
//get resolution of screen
console.log("sliding_captcha.sendRequest:" + blockedImageFileName)
await this.sendRequest(blockedImageFileName, async (detectedPositionList) => {
console.log("detectedPosition: " + device.model() + ":" + detectedPositionList);
if (detectedPositionList.length >= 2) {
@@ -81,6 +82,7 @@ class SlidingCaptchaSolver {
})
}
async deleteFile(filename) {
try {
fs.unlinkSync(filename)
@@ -89,6 +91,7 @@ class SlidingCaptchaSolver {
return OCRResult.TERMINAED
}
}
async sendRequest(fileName, callback) {
const fileStream = fs.createReadStream(fileName);
let response = await axios.post(DEEPLEARNING_CAPTCHA_HOST, fileStream, {