单击以发送请求时出现 Cypress 400 XHR 错误

时间:2021-02-09 18:03:35

标签: automated-tests cypress end-to-end

我已经与 cypress 合作了一天。我有一个按钮,一旦你点击它就会刷新页面,之后的一些测试仍然会通过。

单击此按钮将引发 400 错误。 (发布请求)

我可以单击页面上的其他按钮,也可以在浏览器中的新标签页中手动执行此操作。但是一旦测试自动化,它就会失败。

我看了几个案例: #1951 #2001

但似乎没有人能够解决这个问题。

index.js

module.exports = (on, config) => {
  on('before:browser:launch', (browser, launchOptions) => {
    if (browser.name === 'chrome') {
      launchOptions.args.push('--disable-site-isolation-trials');
      launchOptions.args.push('--incognito')
      return launchOptions
    }
  })
}

test.js

context('Checkout Purchase', () => {
    describe('Checkout Form', () => {
        it('Tests Promo Codes', () => {
            cy.wait(2000);
            cy.get('#promoCode')
            .type('bw-nw-testing')
            .should('have.value', 'bw-nw-testing')
            cy.get('#applyPromoCodeButton')
              .click()
              .wait(2000)
        })
        })
})

cypress.json

{
  "projectId": "lkj8fdj",
  "chromeWebSecurity": false,
  "baseUrl": "http://localhost:3000"
}

1 个答案:

答案 0 :(得分:1)

HTTP 400 状态代码代表 Bad-Request。 (即发送到 API 端点的请求不是 API 期望的请求)。 如果上述 HTTP 请求是从您的应用程序发送的,而不是直接从 Cypress 发送的,则用于发出请求的应用程序代码可能有些错误。

检查浏览器上的网络选项卡,并尝试手动复制发送到外部(?)API 的请求。

注意:通常在测试期间,通常的做法是存根(模拟)来自外部实体的响应,但首先您必须确保发出的请求是正确的。否则,您可能会掩盖应用程序中的错误。