Cypress:信用卡支付的自动测试 [英] Cypress: automatic tests for credit card payment

查看:22
本文介绍了Cypress:信用卡支付的自动测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做信用卡支付的自动测试.不幸的是,测试在 Expiry Date 项目中失败了

I am doing the automatic test for payment by credit card. Unfortunately the test fails me in the Expiry Date item

我的代码:

    cy.getWithinIframe('[name="cardnumber"]').type('4242424242424242');
    cy.getWithinIframe('[name="exp-date"]').type('122024');
    cy.getWithinIframe('[name="cvc"]').type('987');

给出以下错误:

测试结束的地方

推荐答案

所以对于 iframe,我们必须寻找解决方法,因为 cypress 不提供对 iframe 的开箱即用支持.

So for iframes, we have to look for workarounds as cypress doesn't provide out of the box support for iframes.

1. 我们将创建一个自定义命令,该命令将帮助我们遍历 iframe 并有助于代码的可重用性.在 cypress/support/command.js 下写:

1. We will create a custom command which will help us to traverse through iframes and also helps in code reusability. Under cypress/support/command.js write:

Cypress.Commands.add('getIframe', (iframe) => {
    return cy.get(iframe)
        .its('0.contentDocument.body')
        .should('be.visible')
        .then(cy.wrap);
})

2. 我看到测试有时会随机失败,所以如果我们使用 cypress 的重试选项.为此,请转到 cypress.json 并编写:

2. I saw that the tests are randomly failing sometimes, so it would be good if we use the retry option from cypress. For that go to cypress.json and write:

重试":2

此选项仅适用于 cypress v5.0 及更高版本.

This option will only work with cypress v5.0 and above.

3. 在您的测试文件中,输入卡号使用:

3. In your test file, for inputting card number use:

cy.getIframe('#stripe-card-element > .__PrivateStripeElement > iframe').click().type('4242 4242 4242 4242')

输入有效期使用:

cy.getIframe('#stripe-exp-element > .__PrivateStripeElement > iframe').click().type('0921')

输入CVC号码使用:

cy.getIframe('#stripe-cvc-element > .__PrivateStripeElement > iframe').click().type('123')

这篇关于Cypress:信用卡支付的自动测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆