首页 > 解决方案 > 从页面中获取文本,然后根据 API 调用对其进行验证

问题描述

我正在寻找拦截 API 调用,从中获取一些值,然后检查所有值是否都显示在页面上。

我能够拦截 API 调用并对其进行检查。我能够从页面中获取文本,但是我似乎无法验证一个与另一个。

it('Check dashboard stats', () => {
    cy.intercept('GET', 'api/dashboard/kpi').as('kpi')

    cy.visit('/dashboard')
    cy.get('[data-cy=incomeThisMonthValue]').invoke('text').as('incomeThisMonth')
    cy.get('[data-cy=bookingsThisMonthValue]').invoke('text').as('bookingsThisMonth')
    cy.get('[data-cy=newCustomerThisMonthValue]').invoke('text').as('newCustomersThisMonth')

    cy.get('@kpi').should(({ response }) => {
      expect(response.body).to.have.property('success', true)
      cy.log(cy.get('@bookingsThisMonth'))
    })
  })
});

cy.log(cy.get('@bookingsThisMonth'))控制台中显示一个对象,而不是我期望的文本。我不知道如何访问里面的文本.should来验证它。

标签: cypress

解决方案


您必须像这样提取文本:

cy.get('@kpi').should(({response}) => {
  expect(response.body).to.have.property('success', true)
  cy.get('@bookingsThisMonth').then((bookingsThisMonth) => {
    cy.log(bookingsThisMonth) //prints the value
    expect(response.body.bookings).to.equal(bookingsThisMonth)
  })
})

推荐阅读