通过testcafe-browser-tools将自定义args传递给Chrome二进制文件时,无头运行testcafe [英] Run testcafe headless when passing custom args to Chrome binary via testcafe-browser-tools

查看:41
本文介绍了通过testcafe-browser-tools将自定义args传递给Chrome二进制文件时,无头运行testcafe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Vagrant VM内运行testcafe,主要可以正常工作.

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

但是,在启用了硬件加速的情况下,Chrome无法在这种环境下正常启动,因此我必须使用命令行标志-disable-gpu 来启动它.

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.

我正在利用"testcafe-browser-tools"包来实现此目的,方法是通过TestCafe API中的Runner类覆盖默认的浏览器命令.

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.

对于通过打开浏览器窗口来运行TestCafe的情况,这一切都很好,但是我无法弄清楚如何使用相同的设置在无头模式下运行测试.我尝试在修改浏览器命令时简单地添加-headless arg,但是它只是挂起,并且测试从未开始.

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.

testcafe CLI命令具有一个用于无头模式的开关,例如 testcafe"chrome:headless" test.js ,并在似乎设置了一些内部配置变量的代码中进行了挖掘,以完成神奇的工作,但是当通过API自定义浏览器命令时,我一直无法弄清楚如何获得相同的行为.

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?

推荐答案

您可以使用以下代码在

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?

这篇关于通过testcafe-browser-tools将自定义args传递给Chrome二进制文件时,无头运行testcafe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆