【发布时间】:2018-04-04 08:26:21
【问题描述】:
默认情况下,运行 cypress open 会打开 Cypress 窗口,然后我必须手动点击“运行所有测试”按钮才能运行所有测试。
如何只运行cypress open,无需额外步骤,在浏览器中运行所有测试?
谢谢。
编辑:当我更改测试文件时,我需要重新运行测试,就像 cypress open 所做的那样,所以只运行一次(比如在无头模式下)没有帮助。
【问题讨论】:
标签: cypress
默认情况下,运行 cypress open 会打开 Cypress 窗口,然后我必须手动点击“运行所有测试”按钮才能运行所有测试。
如何只运行cypress open,无需额外步骤,在浏览器中运行所有测试?
谢谢。
编辑:当我更改测试文件时,我需要重新运行测试,就像 cypress open 所做的那样,所以只运行一次(比如在无头模式下)没有帮助。
【问题讨论】:
标签: cypress
如果您无头运行赛普拉斯测试cypress run,它将运行所有测试,而无需单击“运行所有测试”按钮。
我发现使用npx cypress run 是最好的方法。
有关无头运行 cypress 的文档指定了您可以使用的其他选项:https://docs.cypress.io/guides/guides/command-line.html#
【讨论】:
npx cypress run 最好,因为我比较懒:打字更快,而且您不需要包含柏树根目录或$(npm bin)。
--browser 指定它应该打开一个浏览器(Chrome 是目前唯一支持的浏览器)。不确定它是否在手表模式下运行。
使用cypress open 时,您可以使用全局配置选项watchForFileChanges 详细说明here,在每次编辑后在浏览器中重新运行测试
您可以将其作为命令行参数传递:
cypress open --config watchForFileChanges=true
或者您可以在 cypress.json 文件中指定它:
{
"watchForFileChanges": true
}
您仍然需要在首次运行 cypress open 时单击运行所有规范,但之后对测试文件的任何编辑都会导致重新运行测试。
【讨论】:
cypress run 或cypress open?我问的原因是watchForFileChanges 标志与cypress open 一起使用。它不会重新启动cypress run
integration\examples\test.js 文件夹下。我正在使用 cypress 3.1.0 版本并测试运行器 Chrome 68。我已经从 Windows 命令提示符打开了 cypress,例如 `C:\node_modules\.bin>cypress open'
cypress open --config watchForFileChanges=true,然后选择您的项目目录。
您可以告诉 Cypress UI 的界面(请记住,Cypress UI 与任何其他页面一样位于 DOM 中)重新运行该套件 - 只需在您的应用启动时发送一些预定义的信号,然后让 Cypress 直接使用 jQuery(避免一个日志事件)找到它自己的重新加载按钮并单击它。
在我的示例中,“信号”是某个控制台日志消息,我的 React 应用程序在其启动期间会打嗝:
Cypress.on("window:before:load", (win) => { // registers callback early, long before the app is loaded
win.console._log = win.console.log;
win.console.log = function monkeyPatchedConsoleLog() {
if (arguments[0].match(/^MyApp starting: \(.*\)/)) { // match the app's startup message
cy.$$(".restart", top.document).click(); // click Cypress' own reload button
}
return win.console._log.apply(win.console, arguments); // otherwise log normally
};
});
(在您的 support/* 文件中包含此内容)
【讨论】:
如果你想使用npx cypress open,那么在package.json中设置标志“setnonGlobalStepDefinitions”为false,然后在“步骤定义”。
{
"devDependencies": {
"cypress": "^9.1.0",
"cypress-cucumber-preprocessor": "^4.3.0"
},
"cypress-cucumber-preprocessor": {
"nonGlobalStepDefinitions": false,
"stepDefinitions": "cypress/integration/"
}
}
您的测试将一个接一个地运行。
【讨论】: