【问题标题】:Cypress intercept blocks the request when it's called several times in a test run赛普拉斯拦截在测试运行中多次调用时阻止请求
【发布时间】:2021-09-10 10:15:18
【问题描述】:

我在 Cypress 中创建了一些测试,以便在我们的 Angular 应用程序中添加和复制文章。测试代码 ArticlesTest.js

describe('Shop articles single station', () => {
const productManagementPage = new ProductManagementPage()
const shopArticlesPage = new ShopArticlesPage()

before(() => {
    var credentials = 
    {
        "username": "someusername@user.name",
        "password": "strongPassword!"
    }

    cy.navigateToProductManagement(credentials)
})

beforeEach(() => {
    productManagementPage.shopArticlesProgrammingButton().click()
    shopArticlesPage.WaitUntilPageLoaded('/productmanagement/api/v1/articles/get', 'GetArticles')
})

it('Add article', () => {
    var randomNumber = RandomDataGenerator.GenerateRandomInt(1000,4999)
    var randomName = RandomDataGenerator.GenerateRandomString(20)
    var randomPrice = RandomDataGenerator.GenerateRandomDecimal(1,99)

    shopArticlesPage.newArticleButton().click()
    shopArticlesPage.saveButton().should('be.disabled')
    shopArticlesPage.undoButton().should('be.disabled')
    shopArticlesPage.deleteButton().should('be.disabled')
    shopArticlesPage.articlesList().should('not.exist')
    shopArticlesPage.articleNumberTextBox().should('be.enabled')

    shopArticlesPage.articleNumberTextBox().type(randomNumber)
    shopArticlesPage.articleNameTextBox().type(randomName)
    shopArticlesPage.articleUnitPriceTextBox().type(randomPrice)

    shopArticlesPage.undoButton().should('be.enabled')

    shopArticlesPage.saveButton().click()

    shopArticlesPage.newArticleButton().should('exist')
    shopArticlesPage.articlesList().should('exist')
    shopArticlesPage.saveButton().should('be.disabled')
    shopArticlesPage.undoButton().should('be.disabled')

})

it('Duplicate article', () => {
    var articleNumber = RandomDataGenerator.GenerateRandomInt(51,65)
    var newArticleNumber = RandomDataGenerator.GenerateRandomInt(1000, 4999)
    var newArticleName = RandomDataGenerator.GenerateRandomString(20)
    
    shopArticlesPage.articlesList().selectFromList(articleNumber)

    const articleUnitPrice = shopArticlesPage.articleUnitPriceTextBox().invoke('text')
    const vatCodeValue = shopArticlesPage.vatCodeDropDown().invoke('text')
    const cardCodeValue = shopArticlesPage.cardCodeDropDown().invoke('text')

    shopArticlesPage.duplicateArticleButton().click()
    shopArticlesPage.WaitUntilPageLoaded()
    shopArticlesPage.articleNumberTextBox().type(newArticleNumber)
    shopArticlesPage.articleNameTextBox().type(newArticleName)
    shopArticlesPage.saveButton().click()

    shopArticlesPage.newArticleButton().should('be.enabled')
})

WaitUntilPageLoaded()方法代码为:

WaitUntilPageLoaded(path, alias) {
    return cy.waitForRequestToComplete(path, alias)
}

这又是自定义赛普拉斯命令:

Cypress.Commands.add('waitForRequestToComplete', (path, alias) => {
  cy.intercept('POST', path).as(alias)
  cy.wait('@' + alias).its('response.statusCode').should('be.ok')
})

在第一次 beforeEach() 运行中,拦截 GetArticles 并等待它完成没有问题。 问题从第二次测试开始,因为它看起来像 GetArticles 没有被拦截,它根本没有被调用,虽然它应该是。手动单击应用程序时不存在该问题,并且始终调用 /articles/get。 测试以错误消息结束

在 30000 毫秒后重试超时:cy.wait() 超时等待 30000 毫秒以等待对路由的第一个请求:GetArticles。从未发生过请求。

我也尝试过使用其他端点,例如vatcodes/get,它运行良好。该问题仅发生在 articles/get 中,但我没有看到任何线索可以告诉我为什么文章端点会发生这种情况。

有什么问题?为什么赛普拉斯“阻止”第二次调用此端点?更有趣的是,GetFeatures 别名不存在这个问题,它以相同的方式创建。

【问题讨论】:

    标签: javascript angular cypress cy.intercept


    【解决方案1】:

    如果我正确地阅读了情况,最后的日志图像就是失败的测试。

    那里没有显示(xhr) 200 /productmanagement/api/v1/articles/get

    它直接从api/v1/subscriptionfeatures/getapi/v1/vatcodes/get,但在第一次测试中api/v1/articles/get 位于这两个调用之间。

    如果它出现在屏幕截图的后面,添加一个增加的超时来捕捉它(相同的拦截可以在两个测试中使用更长的超时,但不会延迟第一次测试)。

    这可能意味着您在应用程序中发现了一个错误 - 似乎“复制”操作应该与“添加”操作具有相同的 POST。

    【讨论】:

      【解决方案2】:

      你解决了吗?

      我正在使用这个配置:

      Given('a GraphQL service error is thrown', () => {
        cy.intercept({ method: 'POST', url: '/uat/graphql', times: 1 }, { forceNetworkError: true });
      });
      

      times: 1。但是现在拦截并没有阻塞请求。

      我在the docs 中找到了times

      【讨论】:

        猜你喜欢
        • 2021-10-05
        • 1970-01-01
        • 1970-01-01
        • 2019-12-27
        • 1970-01-01
        • 2023-02-14
        • 1970-01-01
        • 2023-02-10
        • 1970-01-01
        相关资源
        最近更新 更多