3

I'm trying to run chromeless on all files in my tests folder
This my index.js so far

const {spawn} = require('child_process');
let fs = require('fs');

const proc = spawn('chrome', [
    '--remote-debugging-port=9222',
    '--disable-gpu',
    '--headless'
]);

console.log(`Spawned child pid: ${proc.pid}`);

async function run() {
    let path = __dirname;
    const files = fs.readdirSync(path);
    for (let filename of files) {
        if (/.js$/.test(filename) && filename !== 'index.js') {
            console.log(`executing: ${path}\\${filename}`);
            const run = require(`${path}\\${filename}`);
            await run();
        }
    }
    process.kill(proc.pid);
}

run().catch(console.error.bind(console));

I couldn't make node wait for the inner scripts execution so the headless process gets killed immediately

What's the right way to do this? Do I need to use another tool on top of chromeless or I just need to improve my entry code?

Edit (solved)
I was doing it wrong, I changed readdir to readdirSync then returned a promise from the "child" script and awaited it
Here's the child script code

const { Chromeless } = require('chromeless')

async function run() {
    const chromeless = new Chromeless()

    const screenshot = await chromeless
        .goto('https://www.google.com')
        .type('chromeless', 'input[name="q"]')
        .press(13)
        .wait('#resultStats')
        .screenshot()

    console.log(screenshot) // prints local file path or S3 url

    // this returns a promise
    return chromeless.end()
}
// run().catch(console.error.bind(console));

// default export the run function
module.exports = run;
unloco
  • 6,928
  • 2
  • 47
  • 58

0 Answers0