是的,您可以查看有关浏览器的 TestCafe docs。
简单地使用:testcafe "firefox:path/to/firefox:headless" tests/sample-fixture.js 您的测试将在 Firefox 中运行(需要安装浏览器)
但是,还有另一个选项可以使用多个浏览器运行测试。另一种方式是使用runner 执行测试。
按照文档(我的项目也是这样实现的),可以创建这样的文件:
const createTestCafe = require('testcafe');
let testcafe = null;
createTestCafe()
.then(tc => {
testcafe = tc;
// Start the runner
const runner = testcafe.createRunner();
// Start APP and wait 10s to ensure is up
runner.startApp('npm run dev', 10000);
return runner
// test folder
.src('./e2e/src/app.e2e-spec.js')
// Browsers where tests will be executed
.browsers([ /* List with your browsers */])
// Reporter to use if you want (optional)
.reporter('html', "./html-reporter/reports.html")
// Start tests
.run();
})
.then(_ => {
testcafe.close();
});
内联解释,但想法很简单。使用TestCafe API,创建runner,设置选项并运行测试。
我没有添加浏览器列表。这是个人选举,比如你想要 FireFox 和 Edge。
您可以查看docs 的不同浏览器和选项。
另外,我的清单是这样的:
.browsers([
'chrome:headless:emulation:device=iphone X',
'firefox:headless:emulation:device=Samsung Galaxy S9',
// And so on ...
])
第一个选项是浏览器本身。
headless这是可选的,用于在不打开浏览器窗口的情况下运行(Safari 不支持它)。
而device 则是模拟设备。
此外,在文档中,以更好的方式进行了解释。
并使用简单的node runner.js 执行此文件,所有测试将在添加到列表中的每个浏览器中执行。