基本上,我想在执行所有测试之前登录一次.我的测试文件分为几个文件.
我应该使用before hook在每个测试文件中调用我的login命令,还是有办法在所有测试之前执行一次?
Jen*_*ane 16
简短回答:您可以before在supportFile(在自己的其他规范文件之前自动加载的文件)中的钩子中编写您的登录命令.此before挂钩将在其他测试文件中的任何代码之前运行.
建议:话虽如此,这种方法对于您将来可能需要的各个测试文件的变化几乎没有灵活性,例如:
onBeforeLoad一次做什么怎么办?我建议只before在每个单独的spec文件的钩子中使用login命令.
我还建议在beforeEach挂钩中使用您的登录命令,以避免在测试之间共享任何状态.
小智 14
现在,在Cypress v12中,执行此操作的适当方法是使用Before Run API。
赛普拉斯.config.js
const { defineConfig } = require('cypress')
module.exports = defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('before:run', (details) => {
/* code that needs to run before all specs */
})
},
experimentalInteractiveRunEvents: true, // use for cypress open mode
},
})
Run Code Online (Sandbox Code Playgroud)
Hos*_*rad 12
describe('Hooks', function() {
before(function() {
// runs once before all tests in the block
})
})
Run Code Online (Sandbox Code Playgroud)
https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests.html#Hooks
小智 7
我会在每次测试之前登录,因为先前测试中发生的事情可能会影响当前测试的结果。通过全新登录,您每次都会以干净的状态开始。如果您想测试不相关操作的“链”(操作 A THEN 操作 B),则将其编写为单独的测试,但在单独的测试中具有基本功能。
describe('/page'), () => {
beforeEach(() => {
cy.login() // custom command that handles login w/o UI
cy.visit('/page') // go to the page you are testing
})
// tests
})
Run Code Online (Sandbox Code Playgroud)
您应该在每个测试文件中包含一个 beforeEach 块。该块应该登录并导航到有问题的页面。