How to instantiate a class that requires DOM with Puppeteer?

26 Views Asked by At

I want to instantiate a class called DataBinding that uses document.querySelector inside a CoffeeScript file. To achieve this, I'm using Puppeteer to create the DOM environment. However, inside page.evaluate, the code didn't recognize the class DataBinding. I'm performing this to test the DataBinding class. I tried to run the following code, but it didn't work:

import puppeteer from 'puppeteer'
import { DataBinding } from // correct path

browser = await puppeteer.launch({headless: 'new', executablePath: "/usr/bin/google-chrome-stable"});
page = await browser.newPage();
await page.goto 'http://127.0.0.1:9000';

await page.evaluate(() =>
    data_binding_instance = new DataBinding();
) 

await browser.close();
  1. I'm running a blank index.html page just to create the DOM.
  2. This code is transpiled by esbuild to generate a .js file.
  3. I'm running the transpiled code with node test_data_binding.js (where test_data_binding.js is the transpiled file).

I tried a lot of things, such as using page.exposeFunction to pass a function to the Puppeteer context that returns a DataBinding class, but it didn't work.

0

There are 0 best solutions below