简体   繁体   English

使用 Cypress 测试 Stripe Checkout

[英]Testing Stripe Checkout w/ Cypress

I have to call stripe.redirectToCheckout ( https://stripe.com/docs/js/checkout/redirect_to_checkout ) in js to take a customer to their stripe checkout page.我必须在 js 中调用stripe.redirectToCheckouthttps://stripe.com/docs/js/checkout/redirect_to_checkout )才能将客户带到他们的条带结帐页面。

I want to use cypress to test the checkout process, but it is not able to handle the stripe redirect as the cypress frame is lost when stripe.redirectToCheckout navigates to the page on Stripe's domain.我想使用 cypress 来测试结帐过程,但它无法处理条带重定向,因为当stripe.redirectToCheckout导航到 Stripe 域上的页面时,cypress 框架丢失。

I also want to test that Stripe redirects us back to the success or error URL.我还想测试 Stripe 是否将我们重定向回成功或错误 URL。

  1. Is there any way to force cypress to "reattach" to the page once we've navigated to the Stripe checkout -or-一旦我们导航到 Stripe 结帐,是否有任何方法可以强制 cypress “重新附加”到页面 - 或 -
  2. Is there any way to get the URL for the Stripe checkout page so we can redirect manually or just know that it was at least called with the right parameters.有什么方法可以为 Stripe 结帐页面获取 URL,这样我们就可以手动重定向,或者只知道它至少是用正确的参数调用的。

I know that testing external sites is considered an "anti-pattern" by the people at cypress ( https://github.com/cypress-io/cypress/issues/1496 ).我知道测试外部站点被赛普拉斯的人认为是“反模式”( https://github.com/cypress-io/cypress/issues/1496 )。 But how can a very standard web process, checkout, be tested (with a very popular and standard payment service, I will add) in that case?但是,在这种情况下,如何才能对非常标准的 web 流程、结帐、测试(使用非常流行和标准的支付服务,我会补充)? I don't buy that this is an "anti-pattern".我不认为这是一种“反模式”。 This is an important step of the end-to-end test, and Stripe specifically gives us a testing sandbox for this kind of thing.这是端到端测试的一个重要步骤,Stripe 专门为我们提供了一个测试沙盒。

One common way to e2e test application with external dependencies like stripe is to make a simple mock version of it, that is then applied in e2e testing.使用像条带这样的外部依赖项对应用程序进行 e2e 测试的一种常见方法是制作一个简单的模拟版本,然后将其应用于 e2e 测试。 The mock can also be applied during development, to speed things up.模拟也可以在开发过程中应用,以加快速度。

Would this help?这会有帮助吗?

it(`check stripe redirection`, () => {
  cy.get('#payButton').click();
  
  cy.location('host', { timeout: 20 * 1000 }).should('eq', STRIPE_REDIRECT_URL);    

  // do some payment stuff here
  // ...
  // after paying return back to local
 

  cy.location({ timeout: 20 * 1000 }).should((location) => {
    expect(location.host).to.eq('localhost:8080')
    expect(location.pathname).to.eq('/')
  })

})

I've used this method to test keyCloak login.我已经使用这种方法来测试 keyCloak 登录。 This is the actual code that worked.这是有效的实际代码。

describe('authentication', () => {
  
  beforeEach(() => cy.kcLogout());

  it('should login with correct credentials', () => {
    cy.visit('/');

    cy.location('host', { timeout: 20 * 1000 }).should('eq', 'keycloak.dev.mysite.com:8443');

    // This happens on https://keycloak.dev.mysite.com:8443/auth/dealm/...
    cy.fixture('userCredentials').then((user) => {
      cy.get('#username').type(user.email);
      cy.get('#password').type(user.password);

      cy.get('#kc-login').click();

      cy.location({ timeout: 20 * 1000 }).should((location) => {
        expect(location.host).to.eq('localhost:8080')
        expect(location.pathname).to.eq('/')
      })
    })
  });

I'm giving it a try to use stripe elements instead since it does not redirect and gives me much more control over it.我正在尝试使用条纹元素,因为它不会重定向并且让我对它有更多的控制权。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM