我有自己的函数(调用测试控制器服务器来重置数据库)。它返回一个承诺,该承诺在数据库成功重置后完成。
function resetDatabase(){
// returns a promise for my REST api call.
}
我的目标是能够在所有测试之前执行它。
describe('Account test suite', function () {
// how can I call resetDb here and wait for the result
// before the tests below are invoked?
it('can log in', function () {
cy.visit(Cypress.config().testServerUrl + '/Account/Login/')
cy.get('[name="UserName"]').type("admin");
cy.get('[name="Password"]').type("123456");
cy.get('#login-button').click();
});
// .. and more test
})
我怎样才能在赛普拉斯中做到这一点?
Update
我试过了
before(() => {
return resetDb(Cypress.config().apiServerUrl);
});
但随后我收到一条警告:
Cypress 检测到您在测试中返回了一个 Promise,但还在该 Promise 中调用了一个或多个 cy 命令
我没有调用cy
in resetDb()
.
赛普拉斯有承诺(Cypress.Promise https://docs.cypress.io/api/utilities/promise.html#Syntax),但它们不是真正的承诺,更像是鸭子打字 https://en.wikipedia.org/wiki/Duck_typing。事实上,Cypress 并不是 100% 与真正的承诺兼容,它们可能有效,也可能无效。
考虑到Cypress.Promise
作为任务或操作。它们与所有其他 cypress 命令一起顺序执行。
要将您的函数放入 Cypress 管道中,您可以使用自定义命令 https://docs.cypress.io/api/cypress-api/custom-commands.html#Syntax。文档没有说明,但你可以返回一个Cypress.Promise
从他们。
Cypress.Commands.add('resetDb', function () {
var apiServerUrl = Cypress.config().apiServerUrl;
return new Cypress.Promise((resolve, reject) => {
httpRequest('PUT', apiServerUrl + "/api/test/reset/")
.then(function (data) {
resolve();
})
.catch(function (err) {
reject(err);
});
});
});
然后可以从测试本身执行该命令,或者在我的例子中从before()
.
describe('Account', function () {
before(() => {
cy.resetDb();
});
it('can login', function () {
// test code
});
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)