2

我在 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并等待它完成 没有问题。第一次测试 - beforeEach() 方法运行 问题从第二次测试开始,因为看起来GetArticles没有被拦截,它根本没有被调用,尽管它应该是。手动单击应用程序时不存在该问题,并且始终调用 /articles/get。 在此处输入图像描述 测试以错误消息结束

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

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

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

4

2 回答 2

0

你解决了吗?

我正在使用这个配置:

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

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

times文档中找到。

于 2021-11-04T18:51:12.837 回答
0

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

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

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

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

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

于 2021-09-10T23:32:21.633 回答