Search code examples
javascriptnode.jsasynchronoustesseractpuppeteer

How to execute something similar to a goto statement in node js or how to create and call a function within an asynchronous function?


I am running an automated test through puppeteer that fills up a form and checks for captcha as well. If the captcha is incorrect, it refreshes to a new image but then I need to process the whole image again and reach the function which was used earlier to process it.

(async function example() {
  const browser = await puppeteer.launch({headless: false})
  const page = await browser.newPage()

  /*-----------NEED TO COME BACK HERE-----------*/
  const tessProcess = utils.promisify(tesseract.process);
  await page.setViewport(viewPort)
  await page.goto('http://www.example.com')
  await page.screenshot(options)
  const text = await tessProcess('new.png');
  console.log(text.trim());
  await page.$eval('input[id=userEnteredCaptcha]', (el, value) => el.value = value, text.trim())
  await page.$eval('input[id=companyID]', el => el.value = 'val');
  const submitBtn = await page.$('[id="data"]');
  await submitBtn.click();


  try {
   var x =  await page.waitFor("#msgboxclose");
    console.log("Captcha error")
}
catch (e) {
    console.error('No Error');
}
if(x){
  await page.keyboard.press('Escape');

  /*---------GO FROM HERE--------*/
}

})()

I want to sort of create a loop so that the image can be processed again whenever the captcha is wrong


Solution

  • Declare a boolean variable that indicates whether you need to try again or not, and put the repeated functionality inside a while loop that checks that variable. If the x condition at the end of the loop is not fulfilled, set tryAgain to false, so that no further iterations occur:

    (async function example() {
      const browser = await puppeteer.launch({headless: false})
      const page = await browser.newPage()
      let tryAgain = true;  // <--------------------------
      while (tryAgain) {    // <--------------------------
        /*-----------NEED TO COME BACK HERE-----------*/
        const tessProcess = utils.promisify(tesseract.process);
        await page.setViewport(viewPort)
        await page.goto('http://www.example.com')
        await page.screenshot(options)
        const text = await tessProcess('new.png');
        console.log(text.trim());
        await page.$eval('input[id=userEnteredCaptcha]', (el, value) => el.value = value, text.trim())
        await page.$eval('input[id=companyID]', el => el.value = 'val');
        const submitBtn = await page.$('[id="data"]');
        await submitBtn.click();
    
    
        try {
          var x =  await page.waitFor("#msgboxclose");
          console.log("Captcha error")
        }
        catch (e) {
          console.error('No Error');
        }
        if(x){
          await page.keyboard.press('Escape');
          /*---------GO FROM HERE--------*/
        } else {
          tryAgain = false;   // <--------------------------
        }
      }
    
    })()