首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Cypress -在同一个规范文件中截取两个端点

Cypress -在同一个规范文件中截取两个端点
EN

Stack Overflow用户
提问于 2022-10-06 20:58:04
回答 2查看 62关注 0票数 0

我在同一个规范文件中截取2个api有困难。端点是

  1. client/users
  2. client/users/ipuser

问题:它捕获了ipuser json中的用户响应。有人能帮助如何在端点中使用regex吗?

代码语言:javascript
运行
复制
cy.intercept('GET', '/client/users/ip_user', { fixture: 'ipuser.json' }).as('ipuser')
cy.intercept('GET', '/client/users', { fixture: 'users.json' }).as(
  'user'
)
cy.wait('@ipuser').then((interception) => {
    interception.response.body.data.attributes.limit = 10000
    interception.response.body.data.attributes.amount = 10000
    cy.log(JSON.stringify(interception.response.body))
    cy.writeFile(filename, JSON.stringify(interception.response.body))
 )

 cy.intercept('GET', '/client/users/ip_user', {
   fixture: 'ipuser.json',
 }).as('ipuser')        
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-10-06 22:16:38

您将使用regex匹配您的urls的结尾,并将需要转义斜杠。

代码语言:javascript
运行
复制
cy.intercept('GET', /\/client\/users\/ipuser$/, { fixture: 'ipuser.json' }).as('ipuser')
cy.intercept('GET', /\/client\/users$/, { fixture: 'users.json' }).as('user')
票数 1
EN

Stack Overflow用户

发布于 2022-10-06 22:29:52

似乎有一些问题,

@ipuser

  • 您有两个拦截器

  • 如果您正在动态地编写ipuser.json,则需要在截取.

中动态分配它。

假设触发请求的是cy.visit('/'),它应该是这样的

代码语言:javascript
运行
复制
// set up both intercepts at the top of the test
// the more specific URL (/client/users/ip_user) should go last

cy.intercept('GET', '/client/users', {fixture: 'users.json'}).as('user')

cy.intercept('GET', '/client/users/ip_user', req => {
  req.reply({fixture: 'ipuser.json'})                  // responds after the fixture is written
}).as('ipuser')  

// trigger the fetches
cy.visit('/')

// wait on the 1st - presume it creates the fixture for the second
const filename = './cypress/fixtures/ipuser.json'
cy.wait('@user').then((interception) => {
  interception.response.body.data.attributes.limit = 10_000
  interception.response.body.data.attributes.amount = 10_000
  cy.writeFile(filename, interception.response.body)  // JSON.stringify not needed
})

// wait on the 2nd and check it's result
cy.wait('@ipuser')
  .its('response.body')
  .should('have.property', 'data')
  .should('have.property', 'attributes')
  .should('have.property', 'limit', 10_000)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73979952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档