在Cypress中运行测试并链接多个Promises(我用cypress-promise封装它们,这只是new Cypress.Promise
的语法糖)时,我的断言被触发得太早了,这会带来错误的结果。
我希望执行顺序如下:
checkAutoSuggest
checkIfSelectedValueMatches
clearFilter
checkOffer
已解决,checkOffer.then
的正文已准备好执行相反,它会触发:
checkAutoSuggest
checkOffer
已解决(它不等待链接的Promises,并且cy.log('checkOffer is now resolved')
出现在整个测试执行的中间,而不是最后)。 checkIfSelectedValueMatches
clearFilter
已解决的响应的值来自最后一个承诺。
it('Approvals', function () {
cy.visit('/approvals');
openFilters();
const checkOffer = (async () => {
return await promisify(
checkAutoSuggest('#offer__name', 'test', '**/offers?limit=200&name=test')
.then((selectedOffer) => checkIfSelectedValueMatches('#offer__name', selectedOffer))
.then(() => clearFilter('#offer__name', 'test', 'Offer', '**offset=0&limit=10&sort=-created'))
);
})();
checkOffer.then(response => {
console.log(response) // returns value resolved from clearFilter promise (correct)
cy.log('checkOffer is now resolved') // appears right after checkAutoSuggest - too early!
expect(cy.state('requests').filter(request => request.alias === 'loadList').length).to.equal(4); // appears right after checkAutoSuggest - too early!
});
});