0

When trying to pass in a simple object into a async function that runs puppeteer, i get this error:

(node:4000) UnhandledPromiseRejectionWarning: Error: Evaluation failed: ReferenceError: userInfo is not defined
    at __puppeteer_evaluation_script__:1:19
    at ExecutionContext._evaluateInternal (C:\Users\frank\OneDrive\Documents\SSW-215\Assignments\SupremeBot\node_modules\puppeteer\lib\ExecutionContext.js:93:19)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async ExecutionContext.evaluate (C:\Users\frank\OneDrive\Documents\SSW-215\Assignments\SupremeBot\node_modules\puppeteer\lib\ExecutionContext.js:32:16)
    at async ElementHandle.evaluate (C:\Users\frank\OneDrive\Documents\SSW-215\Assignments\SupremeBot\node_modules\puppeteer\lib\JSHandle.js:39:16)
    at async ElementHandle.$eval (C:\Users\frank\OneDrive\Documents\SSW-215\Assignments\SupremeBot\node_modules\puppeteer\lib\JSHandle.js:372:24)
    at async checkout (C:\Users\frank\OneDrive\Documents\SSW-215\Assignments\SupremeBot\backend\safeBackend.js:93:3)
    at async startSafeBot (C:\Users\frank\OneDrive\Documents\SSW-215\Assignments\SupremeBot\backend\safeBackend.js:162:3)            

Here is my code:

const checkout = async (page, userInfo) => {
  await page.click('#checkout-now'); // click checkout now after adding to cart is done
  console.log("Added to cart, going to checkout...");

  // waits for form to load
  let checkoutFormVisible = await isElementVisible(page, '#order_bn');

  while (!checkoutFormVisible) {
    checkoutFormVisible = await isElementVisible(page, '#order_bn');
  }

  console.log("Checkout loaded, filling payment...");

  // filling in billing
  await page.$eval('#order_bn', el => el.value = userInfo.name);
  await page.$eval('#order_email', el => el.value = userInfo.email);
  await page.$eval('#order_tel', el => el.value = userInfo.phoneNumber);
  await page.$eval('#order_billing_address', el => el.value = userInfo.billingAddress1);
  await page.$eval('#order_billing_address_2', el => el.value = userInfo.billingAddress2);
  await page.$eval('#obz', el => el.value = userInfo.zipCode);
  await page.$eval('#order_billing_city', el => el.value = userInfo.billingCity);
  await page.select('#order_billing_state', userInfo.billingState);
  await page.select('#order_billing_country', userInfo.billingCountry);
  await page.$eval('#cnid', el => el.value = userInfo.creditCardNum); // this is a prepaid card with like 30 cents on it
  await page.select('#credit_card_month', userInfo.creditCardMonth);
  await page.select('#credit_card_year', userInfo.creditCardYear);
  await page.$eval('#vvv-container > input[type=tel]', el => el.value = userInfo.cvv);
  await page.click("#order_terms");
  await page.$eval('#g-recaptcha-response', el => el.value = userInfo.gRecaptchaRes);
  await wait(DELAY); // wait a little before button is clicked
  await page.click("#submit_button");
  await page.waitForSelector('#checkout-loading-message > span > span'); // let payment processing page load before checking

async function startSafeBot () {

  const browser = await puppeteer.launch({ 
    headless: false,
  });

  const browserPage = await browser.newPage();

  const userData = {
    'name': 'frank ied223213',
    'email': 'frankied324234234@gmail.com',
    'phoneNumber': '914-123-1234',
    'billingAddress1': '123 testing lane',
    'billingAddress2': '',
    'zipCode': '12345',
    'billingCity': 'test',
    'billingState': 'NY',
    'billingCountry': 'USA',
    'creditCardNum': '12323123123123123213',
    'creditCardMonth': '04',
    'creditCardYear': '2025',
    'cvv': '123',
    'gRecaptchaRes': "3e232d32cf4d34343d434132d4124c234d24"
  };

  await generateSupremeBrowser(browserPage,1,"beaded","Black","N/A");
  await addToCart(browserPage);
  await checkout(browserPage, userData);
  await processPayment(browserPage);
  await browser.close();
}

startSafeBot();

I've tried declaring userinfo first, then assigning values to it. Didn't work. Also, when i set a breakpoint on console.log("checkout loaded, filling payment...") user info is defined with all the values i assigned it. It's weird because its already defined. I believe it's something to do with puppeteer. I also tried doing ${userInfo.name} no luck either.

frankied003
  • 466
  • 6
  • 26

1 Answers1

3

userInfo isn't accessible in puppeteer evaluation

you can use evaluate method with pushing a param, see here for it Puppeteer: pass variable in .evaluate()

crystalbit
  • 664
  • 2
  • 9
  • 19