Run testcafe headless when passing custom args to Chrome binary via testcafe-browser-tools

强颜欢笑 提交于 2021-01-27 12:10:25

问题


Running testcafe inside a Vagrant VM, which is mostly working.

However, Chrome doesn't start properly in this environment with hardware acceleration enabled, so I have to start it with the command line flag --disable-gpu.

I'm leveraging the 'testcafe-browser-tools' package to accomplish this, by overriding the default browser command via the Runner class in the TestCafe API.

This all works fine for the case of running TestCafe with it opening a browser window, but I've not been able to figure out how to use this same setup to run tests in headless mode. I tried simply adding the --headless arg when modifying the browser command, but it just hangs and the tests never start.

The testcafe CLI command has a switch for headless mode, like testcafe "chrome:headless" test.js, and digging through the code that seems to set some internal config variable that does the magic stuff, but I've been unable to figure out how to get that same behavior when customizing the browser command via the API.

For reference, here's the script I wrote up to handle starting my tests:

const format = require('util').format;
const programName = process.argv[1];

const usage = () => {
  console.log("Configures Chrome and runs all passed tests.\n\n");
  console.log(format('Usage: %s [--headless] <file_pattern_1> [file_pattern_N]', programName));
}

const args = process.argv.slice(2);
const testFilePatterns = [];
let headless = '';

for (let filePattern of args) {
  if (filePattern == '--headless') {
    console.log('Headless mode enabled');
    headless = '--headless';
  }
  else {
    console.log(format('Adding file pattern %s for testing', filePattern));
    testFilePatterns.push(filePattern);
  }
}

if (testFilePatterns.length < 1) {
  usage();
  process.exit(1);
}

const testcafeBrowserTools = require('testcafe-browser-tools');
const createTestCafe = require('testcafe');

(async () => {
  const info = await testcafeBrowserTools.getBrowserInfo('/usr/bin/chromium');
  info.cmd = `${info.cmd} ${headless} --no-sandbox --disable-gpl`;
  console.log(format('Running chrome with command: %s', info.cmd));
  const testcafe = await createTestCafe();
  const failedCount = await testcafe
    .createRunner()
    .src(testFilePatterns)
    .browsers(info)
    .run();
  testcafe.close();
})();

Is there some modification to that script that would solve my issue, or another approach that is needed?


回答1:


You can use the following code to run tests in headless mode:

await testcafe.createRunner()
    .src('test.js')
    .browsers('chrome:headless --no-sandbox --disable-gpu')
    .run();

     It's the way how we use the headless mode internally, so it'll be parsed correctly. Could you please check this approach?



来源:https://stackoverflow.com/questions/53405790/run-testcafe-headless-when-passing-custom-args-to-chrome-binary-via-testcafe-bro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!