Merge branch 'master' into feature/test_accepted_contact

This commit is contained in:
Lei PAN
2023-04-21 16:29:06 +02:00
5 changed files with 117 additions and 54 deletions
+81 -41
View File
@@ -37,7 +37,8 @@ const CAPTCHA_ERROR_MESSAGE = "Error verifying captcha, please try again"
const CAPTCHA_ERROR_MESSAGE_FR = "La vérification du captcha a échoué"
const REGEX_RDV_URL = "https:\/\/rendezvousparis\.hermes\.com\/client\/register\/[A-Z0-9]+"
const DEFAULT_STORE = 'faubourg';
const searchTexts = ['hermes+rdv', 'hermes+rendezvous', 'hermes+appointment', 'hermes+appointment+online', 'appointment+hermes', 'hermes+rendez+vous', 'hermes+rendez+vous+paris', 'online+appointment+hermes', 'hermes+online+appointment', 'paris+hermes+online+appointment']
const searchTexts = ['hermes+rdv+online+paris', 'hermes+rdv+enligne+paris', 'hermes+rdv+en+ligne+paris', 'hermes+rendezvous+en+ligne+paris', 'hermes+appointment+online+paris', 'hermes+appointment+online+paris', 'appointment+hermes+paris+on+line', 'hermes+rendez+vous+online+paris', 'hermes+rendez+vous+paris+en+ligne', 'hermes+rendez+vous+paris+enligne', 'hermes+rendez+vous+paris+online', 'online+appointment+hermes+paris', 'hermes+online+appointment+paris', 'paris+hermes+online+appointment']
function delay(delayInMs) {
return new Promise(resolve => {
@@ -82,18 +83,23 @@ class CommandorPage {
// Connect to the device.
log("loadPage() called");
// await this.device.shell('am force-stop com.android.chrome');
const context = await this.device.launchBrowser();
// await context.clearCookies()
try {
this.context = await this.device.launchBrowser();
// await context.clearCookies()
// Use BrowserContext as usual.
this.page = await this.context.newPage();
this.page.on("load", (loadedPage) => {
this.onPageLoad(loadedPage)
})
// Use BrowserContext as usual.
this.page = await context.newPage();
this.page.on("load", (loadedPage) => {
this.onPageLoad(loadedPage)
})
this.page.on("response", (response) => {
this.onResponse(response)
})
} catch (e) {
log(e)
this.isTerminated = true
}
this.page.on("response", (response) => {
this.onResponse(response)
})
try {
const item = searchTexts[Math.floor(Math.random() * searchTexts.length)];
await this.page.goto("https://www.google.com/search?q=" + item + "&lr=lang_en", {timeout: 90 * 1000});
@@ -104,17 +110,11 @@ class CommandorPage {
try {
if (this.page.url().includes("google"))
this.page.locator('button >> nth=3').click()
// this.page.locator('button:has-text("Tout accepter")').click()
} catch (e) {
log(e)
}
// try {
// this.page.locator('button:has-text("Accept all")').click()
// } catch (e) {
// log(e)
// }
try {
this.page.locator(':nth-match(:text("Online Appointment"), 1)').click()
this.page.locator(':nth-match(:text("rendezvousparis"), 1)').click()
} catch (e) {
log(e)
if (!this.page.url().includes(RDV_URL)) {
@@ -126,12 +126,18 @@ class CommandorPage {
const intervalTask = setInterval(() => {
if (this.isTerminated) {
log(this.device.model() + ":request terminated, will close device");
context.close();
if (this.context !== undefined)
this.context.close();
// this.page.close()
this.device.close()
// this.device.close()
clearInterval(intervalTask)
cancel()
return context
return this.context
} else {
if (this.page.url() === RDV_URL) {
if (!this.isFillingFields)
this.fillFields(this.page);
}
}
}, 10 * 1000)//interval of 10 seconds
@@ -314,27 +320,36 @@ class CommandorPage {
await this.push_message_to_queue(PublishType.SUCCESS)
return
}
this.captchaSolver = new SolveCaptcha(page);
await this.captchaSolver.start((solution) => {
log("solution is: " + solution);
if (solution !== ERROR_CAPTCHA_UNSOLVABLE && solution !== TWO_CAPTCHA_CONNECTION_FAILED) {
try {
if (!page.isClosed()) {
page.evaluate((solution) => {
let element = document.getElementById("g-recaptcha-response");
if (element != null)
document.getElementById("g-recaptcha-response").innerHTML = solution;
}, solution)
this.clickValid();
//check whether there is captcha
let pageContent = await page.content()
let hasCaptcha = pageContent.includes("g-recaptcha-response")
if (hasCaptcha) {
await this.clickCheckbox()
await delay(1000)
this.captchaSolver = new SolveCaptcha(page);
await this.captchaSolver.start((solution) => {
log("solution is: " + solution);
if (solution !== ERROR_CAPTCHA_UNSOLVABLE && solution !== TWO_CAPTCHA_CONNECTION_FAILED) {
try {
if (!page.isClosed()) {
page.evaluate((solution) => {
let element = document.getElementById("g-recaptcha-response");
if (element != null)
document.getElementById("g-recaptcha-response").innerHTML = solution;
}, solution)
this.clickValid();
}
} catch (e) {
log(e)
this.isTerminated = true;
}
} catch (e) {
log(e)
} else {
this.isTerminated = true;
}
} else {
this.isTerminated = true;
}
})
})
} else {
await this.clickValid();
}
}
async isBlocked() {
@@ -365,8 +380,8 @@ class CommandorPage {
} else {
if (currentPage.url() === RDV_URL) {
await this.fillFields(this.page);
if (this.isFillingFields)
await this.getErrors()
// if (this.isFillingFields)
// await this.getErrors()
} else {
if (content.includes(MESSAGE_URL_VALIDATION_FR) || content.includes(MESSAGE_URL_VALIDATION_EN)) {
log("successful");
@@ -385,6 +400,20 @@ class CommandorPage {
}
}
async saveCookies() {
log("saveCookies() called.")
try {
let cookies = await this.page.context().cookies();
let cookiesInJson = [];
cookies.forEach((cookie) => {
cookiesInJson.push(JSON.stringify(cookie))
})
await require("fs").writeFileSync(this.contact.mail + '.txt', cookiesInJson.join('\n'));
} catch (e) {
console.log(e)
}
}
async checkAudioBtn() {
let isBlocked = await this.isBlocked()
if (!isBlocked) {
@@ -423,6 +452,8 @@ class CommandorPage {
// log("onResponse with url:" + response.body())
if (rex.test(response.url())) {
log("rdv url found:" + response.url())
// save cookies
await this.saveCookies();
await this.push_message_to_db(PublishType.SUCCESS, response.url())
}
}
@@ -473,6 +504,15 @@ class CommandorPage {
}
}
async clickCheckbox() {
try {
// let errorItem = await this.page.click("#recaptcha-anchor > div.recaptcha-checkbox-border")
await this.page.frameLocator('[title="reCAPTCHA"]').getByRole('checkbox', {name: 'I\'m not a robot'}).click();
} catch (e) {
log(e);
}
}
async handleError(errorContent) {
log("handle error:" + errorContent);
if (errorContent.includes(DOUBLE_REQUEST_ERROR_MESSAGE) || errorContent.includes(DOUBLE_REQUEST_ERROR_MESSAGE_FR)) {