[英]Cypress: same tests that pass in browser fail in headless mode due to timeout
When I run Cypress with vue-cli-service test:e2e --headless
some tests inside a register request callback fail:当我使用
vue-cli-service test:e2e --headless
运行 Cypress 时,注册请求回调中的一些测试失败:
whereas normally when run in browser ( vue-cli-service test:e2e
) they pass:而通常在浏览器中运行时(
vue-cli-service test:e2e
)他们通过:
it('Submit WITHOUT promo code', () => {
cy.server();
cy.route({method: 'POST', url: 'api/register'}).as('register');
cy.get('.thz-items-loading').should('not.be.visible');
cy.get('#btn-submit').should('not.be.disabled');
cy.get('iframe[name^="__privateStripeFrame"]').then(($iframe) => {
const $body = $iframe.contents().find('body');
cy.wrap($body).find('input:eq(1)').click({force: true}).clear();
cy.wrap($body).find('input:eq(1)').type('4000000000009979'); // stolen card
cy.get('#btn-submit').click(); // SUBMIT FORM
cy.wait('@register').then(xhr => {
cy.contains('#card-errors', 'Your card was declined.');
// cy.get('.thz-items-loading').should('not.be.visible'); // FAILS
// cy.get('.thz-items-loading').to.have.style('display', 'none'); // FAILS
cy.get('#btn-submit').should('not.be.disabled'); // FAILS
(...)
});
return null;
});
(...)
Stack trace:堆栈跟踪:
1) Trial registration form Submit WITHOUT promo code: CypressError: Timed out retrying: expected
<button#btn-submit.thz-button.thz-radius-50.thz-btn-border-2.thz-align-center.thz-ff-g-hind-vadodara-600>
not to be 'disabled' at Object.cypressErr ( https://localhost:8000/__cypress/runner/cypress_runner.js:82944:11 ) at Object.throwErr ( https://localhost:8000/__cypress/runner/cypress_runner.js:82909:18 ) at Object.throwErrByPath ( https://localhost:8000/__cypress/runner/cypress_runner.js:82936:17 ) at retry ( https://localhost:8000/__cypress/runner/cypress_runner.js:76454:16 ) at https://localhost:8000/__cypress/runner/cypress_runner.js:68529:18 at tryCatcher ( https://localhost:8000/__cypress/runner/cypress_runner.js:131381:23 ) at Promise._settlePromiseFromHandler ( https://localhost:8000/__cypress/runner/cypress_runner.js:129399:31 ) at Promise._settlePromise ( https://localho1) 试用注册表提交时没有促销代码:CypressError:重试超时:预期
<button#btn-submit.thz-button.thz-radius-50.thz-btn-border-2.thz-align-center.thz-ff-g-hind-vadodara-600>
不要在 Object.cypressErr ( https://localhost:8000/__cypress/runner/cypress_runner.js:82944:11 ) 在 Object.throwErr ( Object.throwErr ( 885205638 localhost:8000/__cypress/runner/cypress_runner.js:82909:18 ) 在 Object.throwErrByPath ( https://localhost:8000/__cypress/runner/cypress_runner.js:82936:17 ) 在重试 ( 88105683835270: 8000/__cypress/runner/cypress_runner.js:76454:16 )在https://localhost:8000/__cypress/runner/cypress_runner.js:68529:18在 tryCatcher( https://localhost:8000/__cypress/runner/cypress_runner .js:131381:23 ) 在 Promise._settlePromiseFromHandler ( https://localhost:8000/__cypress/runner/cypress_runner.js:129399:31 ) 在 Promise._settlePromise ( 88520583 38520563 st:8000/__cypress/runner/cypress_runner.js:129456:18 ) at Promise._settlePromise0 ( https://localhost:8000/__cypress/runner/cypress_runner.js:129501:10 ) at Promise._settlePromises ( https://localhost:8000/__cypress/runner/cypress_runner.js:129576:18 ) at Async._drainQueue ( https://localhost:8000/__cypress/runner/cypress_runner.js:126305:16 ) at Async._drainQueues ( https://localhost:8000/__cypress/runner/cypress_runner.js:126315:10 ) at Async.drainQueues ( https://localhost:8000/__cypress/runner/cypress_runner.js:126189:14 ) atST:8000/__ CYPRESS/RUNNER/CYPRESS_RUNNER.JS:129456:18 )致电Promise._settlePromise0( https:// localhost:8000/__ cypress/runner/cypress/cypress/cypress_runner.js:1295011: 12959595959595959595959595959595959595959590.1959595959595909. localhost:8000/__cypress/runner/cypress_runner.js:129576:18 ) 在 Async._drainQueue ( https://localhost:8000/__cypress/runner/cypress_runner.js:126305:16 ) 在 Async._drainQueues ( https:// localhost:8000/__cypress/runner/cypress_runner.js:126315:10 ) 在 Async.drainQueues ( https://localhost:8000/__cypress/runner/cypress_runner.js:126189:14 ) 在
Following lines inside cy.wait('@register')
callback should pass in headless mode: cy.wait('@register')
回调中的以下几行应该以无头模式传递:
cy.get('.thz-items-loading').should('not.be.visible');
cy.get('.thz-items-loading').to.have.style('display', 'none');
cy.get('#btn-submit').should('not.be.disabled');
Looks like the timeout happens because elements #btn-submit
and .thz-items-loading
are not reachable in @register
callback, even though in both cases they exist in DOM, but why?看起来超时是因为元素
#btn-submit
和.thz-items-loading
在@register
回调中无法访问,即使在这两种情况下它们都存在于 DOM 中,但为什么呢? They are reachable before the request just fine.在请求之前可以访问它们就好了。
I'm running Cypress 3.2.0 from a project created with vue-cli on Windows 10. Tested app is not a part of the project, is hosted elsewhere and is a static html page.我在 Windows 10 上从使用 vue-cli 创建的项目运行 Cypress 3.2.0。经过测试的应用程序不是该项目的一部分,托管在其他地方,是一个 static html 页面。 Headless browser is Electron 59.
无头浏览器是 Electron 59。
It turns out that Electron browser silently fails at executing newer syntax like Promises, that's why code in resolved Promise's then()
callback which was supposed to change display property of .thz-items-loading
and #btn-submit
was never executed and tests expecting that change never passed.事实证明,Electron 浏览器在执行像 Promises 这样的新语法时默默地失败了,这就是为什么在已解决的 Promise 的
then()
回调中的代码应该改变.thz-items-loading
和#btn-submit
显示属性从未执行过并且测试期望这种变化从未过去。
Adding babel's polyfills before main.js
of tested APP fixed this issue:在测试APP的
main.js
之前添加babel的main.js
修复了这个问题:
<script src="../../node_modules/@babel/polyfill/dist/polyfill.min.js"></script> <!-- or copy to root dir in build process -->
<script src="./main.js"></script>
This fixed it for me:这为我修好了:
"experimentalFetchPolyfill": true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.