【发布时间】:2024-01-14 00:05:01
【问题描述】:
我们正在使用 Cypress.io 来构建我们的自动化套件。我们需要在每次测试之前为我们的数据库播种并在之后清除数据。这可以像下面那样完成。
describe('Our test suite', function() {
before(function () {
//loadDbSeed is a custom command that will load the seed file based on the spec file
seed = cy.loadDbSeed()
cy.task('seed:db', seed)
})
it('should be true', function() {
//Some test with some action followed by an assertion
cy.visit('/some-page')
cy.get('[data-cy="identifier"]')
.click()
expect(true).to.equal(true)
})
after(function () {
// clearDb is a custom command that will clear out the DB.
// We are still debating if we must clear the DB after the tests.
// But we might still need to do some common actions for the entire suite after
cy.clearDb()
})
})
我们看到的问题是我们所有的测试套件都需要相同的before 和after 操作。所以我们想覆盖这些方法,以便我们的测试是这样的。
describe('Our test suite', function() {
before(function () {
// DB seeding is done automatically
// Write only custom before steps required for this test
})
it('should be true', function() {
//Some test with some action followed by an assertion
cy.visit('/some-page')
cy.get('[data-cy="identifier"]')
.click()
expect(true).to.equal(true)
})
after(function () {
// DB clearing is done automatically
// Write only custom after steps required for this test
})
})
我们如何实现这一目标?我一直在研究 Cypress 代码,但没有发现任何明显的东西。
【问题讨论】:
-
您只需将
before()和after()方法添加到 '/cypress/support/index.js` 中,它们就会在每个套件中运行。请注意,您仍然可以在套件本身内部拥有特定于套件的before()和after(),事实上您可以拥有任意多次 - 它们按照它们出现的顺序被调用(首先支持它们)。 -
理查德,非常感谢。我们将试试这个,看看它是否有效!
标签: testing automation cypress