Dan*_*ain 2 automated-tests cypress
我在cypress / support / commands.js文件中创建了以下自定义命令。
Cypress.Commands.add("login", (username, password) => {
cy.request({
method: 'POST',
form: true,
url: '/test/login/',
body: {'username': username, 'password': password}
})
})
Run Code Online (Sandbox Code Playgroud)
在将登录功能移至此自定义命令之前,我已经通过了测试并进行了登录工作。我正在使用cy.login(testuser,testpwd)在我的规范中调用它,但是我收到以下错误消息:TypeError:cy.login不是函数。该文件说任何测试文件进行评估之前/cypress/support/commands.js被加载,所以我认为简单地将一个自定义命令在那里会使命令可用。我正在通过本地(GUI)测试运行程序运行测试。
为了扩大@Dinesh库马尔的优秀的答案,这也很重要,你有你的内部默认支持文件(我知道,不幸的命名方案,在这种情况下)不支持残疾人cypress.json通过添加一行:supportFile: false。
cypress.json如果它在那里,请从您的行中删除。如果您对使用默认路径不满意,也可以指定不同的路径cypress/support/index.js。
使用 commands.js 文件处理 index.js - 都在support文件夹中:
// index.js
const customCommands = require('./commands.js')
module.exports = {
commands: customCommands
}
Run Code Online (Sandbox Code Playgroud)
并仔细检查您的设置:
// cypress.json
{
"baseUrl": "http://demo.your-domain.test",
"supportFile": false, // <-- delete this line present
...
}
Run Code Online (Sandbox Code Playgroud)
index.js测试文件之前已加载其中的所有代码和引用的模块。因此,您需要commands.js在index.js文件中引用(要求)。但是,您可以将commands.js模块直接导入测试文件中,但随后需要在每个测试文件中都包含该模块。推荐的方法是将其包含在index.js文件中,并且您不必担心在测试文件中显式引用。