Codeceptjs on Google Cloud Function goto of undefined

276 Views Asked by At

I'm trying to automate a web activity using CodeceptJS (with Puppeteer) running in a Google Cloud Function. My index.js is:

const Container = require('codeceptjs').container;
const Codecept = require('codeceptjs').codecept;
const event = require('codeceptjs').event;
const path = require('path');

module.exports.basicTest = async (req, res) => {  

  let message = '';

  // helpers config
  let config = { 
    tests: './*_test.js',
    output: './output',
    helpers: { 
      Puppeteer: { 
        url: 'https://github.com', // base url
        show: true,
        disableScreenshots: true, // don't store screenshots on failure
        windowSize: '1200x1000', // set window size dimensions
        waitForAction: 1000, // increase timeout for clicking
        waitForNavigation: [ 'domcontentloaded', 'networkidle0' ], // wait for document to load
        chrome: {
          args: ['--no-sandbox'] // IMPORTANT! Browser can't be run without this!
        }
      } 
    },
    include: {
      I: './steps_file.js'
    },
    bootstrap: null,
    mocha: {},
    name: 'basic_test',

    // Once a tests are finished - send back result via HTTP
    teardown: (done) => {
      res.send(`Finished\n${message}`);
    }
  };

  // pass more verbose output
  let opts = {
    debug: true,
    steps: true
  };

  // a simple reporter, let's collect all passed and failed tests
  event.dispatcher.on(event.test.passed, (test) => {
    message += `- Test "${test.title}" passed `;
  });
  event.dispatcher.on(event.test.failed, (test) => {
    message += `- Test "${test.title}" failed `;
  });

  // create runner
  let codecept = new Codecept(config, opts);
  // codecept.init(testRoot)

  codecept.initGlobals(__dirname);

  // create helpers, support files, mocha
  Container.create(config, opts);

  try {
    // initialize listeners
    codecept.bootstrap();
  
    // load tests
    codecept.loadTests('*_test.js');
  
    // run tests
    codecept.run();
  } catch (err) {
    printError(err)
    process.exitCode = 1
  } finally {
    await codecept.teardown()
  }
}

and my simple test is:

Feature('Basic Automation');

Scenario('Basic Test', async ({ I }) => {
    // ===== Login =====
    pause()
    I.amOnPage('https://cotps.com');
    I.see('Built for developers', 'h1');
});

If I run it using npx codeceptjs run --steps it works but if I run it using node -e 'require("./index").basicTest() I get the error: Cannot read property 'goto' of undefined. I also get the error if I deploy it to GCP and run it. I've looked through the docs for both Codecept and Puppeteer but found nothing and the only examples online are for previous versions of the libraries.

1

There are 1 best solutions below

0
On

I had the same error, but I could fix it with this code:

const { codecept: Codecept } = require('codeceptjs');

const config = { 
    helpers: { 
        Puppeteer: { 
        url: 'https://github.com', 
        disableScreenshots: true, 
        windowSize: '1200x1000',
        waitForAction: 1000, 
        waitForNavigation: 'domcontentloaded', 
        chrome: {
            args: ['--no-sandbox'] 
        }
        } 
    }
};

const opts = { steps: true };

module.exports.browserTest = async (req, res) => {  
(async () => {
  const codecept = new Codecept(config, opts);
  codecept.init(__dirname);

  try {
    await codecept.bootstrap();
    codecept.loadTests('**_test.js');
    // run all tests
    await codecept.run();
  } catch (err) {
    console.log("error: "+err);
    process.exitCode = 1;
  } finally {
    await codecept.teardown();
  }    
})();
};

You can run the code successfully locally using: node -e 'require("./index").browserTest()