javascript 如果在Cypress测试期间多次调用请求,如何捕获请求的“最新”示例?

cdmah0mi  于 2022-12-21  发布在  Java
关注(0)|答案(2)|浏览(184)

在我的Cypress Cucumber测试中,下面是我当前的特性文件场景(此场景在最后一步失败):

Given I log in
Then I am on the dashboard
And the dashboard displays 0 peers (@Peers is called here, & 0 is the correct value here)
When I go to User 1 page
And I click the Add Peer button (@AddPeer request is successful here)
And I go to the dashboard
Then the dashboard displays 1 peers (@Peers is called here. The actual value is 0, but I am expecting 1. It looks like the code is using the response body from the 1st `Peers` intercept)

以下是我的步骤定义:

Given('I log in', () => {
    cy.intercept('GET', `**/user/peers`).as('Peers')
    cy.intercept('POST', `**/Peers/add`).as('AddPeer')
});

Then('I am on the dashboard', () => {
    cy.url().should('include', `dashboard`)
})

Then('the dashboard displays {int} peers', expectedPeersCount => {
    cy.wait('@Peers').then(xhr => {
        const peers = xhr.response.body
        expect(peers.length).to.eq(expectedPeersCount)
    });
});

When('I click the Add Peer button', () => {
    dashboard.btnAddPeer().click()
    cy.wait('@AddPeer').then(xhr => {
        expect(xhr.response.statusCode).to.eq(200)
    })
})

When('I go to the dashboard', () => {
    cy.visit('/dashboard');
});

在后端,@AddPeers()向列表中添加一个对等点,而@Peers()返回我的对等点列表。
当我转到 Jmeter 板时,@Peers()返回最新的对等点列表。
但由于某种原因,上面的代码仍然使用了响应体为空的“旧”响应。
有人能指出我如何才能获得“最新”的@Peers响应吗?
以下是第一个空的Peers响应:

下面是第二个Peers响应,其中包含1个数组项:

尝试修复:

Given('I log in', () => {
    cy.intercept('GET', `**/user/peers`).as('Peers0')
    cy.intercept('GET', `**/user/peers`).as('Peers1')
});

Then('the dashboard displays {int} peers', expectedPeersCount => {
    cy.wait(`@Peers${expectedPeersCount }`).then(xhr => {
        const peers = xhr.response.body
        expect(peers.length).to.eq(expectedPeersCount)
    });
});

柏树原木:

Peers1下面看起来是空的,但它不应该是:

然后,看起来Peers0具有填充的数组。请注意 Matched cy.intercepts()

2izufjch

2izufjch1#

我不明白为什么原始代码不工作,它看起来非常好(没有一个运行的系统玩)。
但是"修复"变体是向后的--设置的***最后一个***截距是***第一个匹配***的截距。
来自文档:

此图显示route5是第一个被检查的(非中间件)路由,接着是route3,然后是route1
此外,由于每个拦截只打算捕获一个调用,因此添加{ times: 1 },使其成为这样。

Given('I log in', () => {
  cy.intercept('GET', `**/user/peers`, {times: 1})
    .as('Peers1')                                     // catches 2nd call
  cy.intercept('GET', `**/user/peers`, {times: 1})
    .as('Peers0')                                     // catches 1st call
})

您可以在此屏幕截图中看到

其中,先匹配Peers1,然后匹配Peers0,但下一个块期望相反的顺序

Then('the dashboard displays {int} peers', expectedPeersCount => {
  cy.wait(`@Peers${expectedPeersCount }`).then(xhr => {
      const peers = xhr.response.body
      expect(peers.length).to.eq(expectedPeersCount)
  });
})

And the dashboard displays 0 peers使用0调用,然后Then the dashboard displays 1 peers使用1调用

    • 动态别名**

您可以根据响应中的对等方数量动态设置别名。

Given('I log in', () => {
  cy.intercept('GET', `**/user/peers`, (req) => {
    req.continue().then(res => {
      if (re.body.length === 0) {
        req.alias = 'Peers0'
      }
      if (re.body.length === 1) {
        req.alias = 'Peers1'
      }
    })
  })
})

如果这行得通,那么你就不需要担心安装的顺序了。

jbose2ul

jbose2ul2#

这里有一点误解。别名拦截的cy.wait()只会等待第一个与给定参数匹配的请求。

cy.intercept('request').as('call')
// some actions to trigger request
cy.wait('@call')
// later in the test trigger same request

// this will refer to the first request made
// by the app earlier in the test
cy.wait('@call')

您可以使用唯一别名进行另一次拦截,然后对新的唯一别名使用.wait()

cy.intercept('request').as('call')
// some actions to trigger request
cy.wait('@call')
// later in the test trigger same request

cy.intercept('request').as('newCall')
// some actions to trigger same request
cy.wait('@newCall')

或者您可以使用相同的方法,使用cy.get()来获取匹配请求的列表,但是,这可能是一个不错的选择,因为cy.get()不会等待新请求完成。

cy.intercept('request').as('call')
// some actions to trigger request
cy.wait('@call')
// later in the test trigger same request

// have to ensure the request is completed by this point
// to get the list of all matching and hopefully
// the last request will be the one you seek
cy.get('@call.all')
  // get last matching request
  .invoke('at', -1)

相关问题