cypress - 从页面中获取文本,然后根据 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
来验证它。
解决方案
您必须像这样提取文本:
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)
})
})
推荐阅读
- ios - 如何在点击的光线触摸而不是来自中心视图的光线处生成 Reality Composer 场景
- apache - 确定原因并修复 302 重定向
- word2vec - 为什么“[UNK]”这个词在 word2vec 词汇表中排在第一位?
- typescript - 打字稿(tsc)显示编译时间
- reactjs - 如何在反应多轮播中居中图像?
- reactjs - 类型错误:Object(...) 不是函数 React js
- python-3.x - 在 Spyder (Python 3.8) 中使用环境时出错
- python - __getattr__ 用于类属性?
- javascript - 在 ajax 调用中启动的函数只工作一次
- memory - 虚拟内存到物理翻译?