Google云功能上的codeceptj goto doto

发布于 2025-01-25 00:07:33 字数 1454 浏览 4 评论 0原文

我正在尝试使用在Google Cloud功能中运行的CodeCeptJS(带有Puppeteer)自动化Web活动。 我的index.js是:

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 
              

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倦话 2025-02-01 00:07:33

我遇到了相同的错误,但是我可以使用此代码进行修复:

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();
  }    
})();
};

您可以使用:node -e'require(“ ./ index”)。browsertest()

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()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文