如何捕获CypressError测试失败以切换执行另一支付提供商的测试逻辑?
我正在用Cypress测试支付页面,我们有两个第三方支付系统,用户支付时会从我的网站跳转到支付提供商页面,之后再返回。这里假设支付提供商的域名是https://demo.paymentprovider1.com和https://demo.paymentprovider2.com。
麻烦的是,目前系统会随机跳转到其中一个提供商,我没法提前预测。虽然我之后会改这个逻辑,但现在打算先找个临时方案。我知道官方文档里关于条件测试的警告,但短期内打算忽略它(毕竟用Selenium做这个很简单)。
如果提前知道会跳转到哪个支付提供商,我会这么写测试代码:
it.only('Makes a Visa/Mastercard payment', () => { clickPaymentTypeRadioButtonAndSubmit(0); cy.url().should('not.contain', 'test.myapp.com'); cy.origin( 'https://demo.paymentprovider1.com', () => { cy .get('body', {timeout: 5000}) .should('contain.text', 'Select payment method'); } ); });
但如果猜错了,跳转到另一个提供商,测试就会失败,报错信息如下:
CypressError: Timed out retrying after 5000ms: The command was expected to run against origin
demo.paymentprovider1.combut the application is at origindemo.paymentprovider2.com.This commonly happens when you have either not navigated to the expected origin or have navigated away unexpectedly.
我希望能处理这种情况,实现这样的逻辑:
- 如果第一个
origin()里的get()断言成功,就执行第一个支付提供商的测试步骤 - 如果失败,就针对另一个域名调用
origin(),如果成功则执行第二个支付提供商的测试步骤
更新1
我试过用下面的代码获取当前页面位置:
cy.window().then((win) => { const l = win.location; cy.task('log', `Location: ${l}`); });
但报错了:
SecurityError: Permission denied to access property Symbol.toPrimitive on cross-origin object
更新2
我发现可以设置chromeWebSecurity为false来解决上述错误,但我用的是Firefox,不想只为了这一点切换浏览器。
更新3
我试过用location()和url()来实现条件测试,但都没成功。接下来我该试试什么方法?
备注:内容来源于stack exchange,提问作者halfer

