自动从API响应中保存柏树灯具

发布于 2025-02-08 13:52:46 字数 1150 浏览 3 评论 0原文

我有一个非常大的API,每个路径都返回一个唯一的资源值,每当我获得特定路径的资源值时,它总是相同的。将每个响应保存到固定文件中将需要几天和几天。

我想使用我的应用程序代码检索API数据,并且我希望柏树保存对夹具文件夹的API响应。

然后,我想使用固定装置在我正在测试的应用程序运行期间拦截对API的呼叫,并希望应用程序代码更快地运行。固定装置将被返回,而不是等待API加速测试。

    describe('fixture creation', () => {
      it('should save fixtures of every endpoint that matches a url', () => {
        // @ts-ignore
        cy.login()
        cy.intercept('https://covfefe/redfish/v1/Chassis/Asrock/PCIeDevices/**', saveResponse)
        cy.visit('/ubmc/system/pci-topology')
      })
    })

如何编写saverespons以从符合上面的URL模式的端点存储JSON响应?

    const saveResponse = req => {
      req.continue((res) => {
        console.log('saved a file', `cypress/fixtures/${ req.url.replace('https://covfefe/', '') }.json`)
        cy.writeFile(
         `cypress/fixtures/${ req.url.replace('https://covfefe/', '') }.json`,
          res.body)
      })
    }

我得到了“保存的文件...”控制台日志,但是我检查了固定装置目录,它是空的。没有错误,并且在res> res处理程序中放置调试器语句不会告诉我任何有意义的事情。

I have a very large API where every path returns a unique resource value, and each time I get the resource value for a particular path it's always the same. Saving each response into a fixture file would take days and days.

I would like to use my application code to retrieve the API data, and I want Cypress to save the API responses to the fixture folder.

I then want to use the fixtures to intercept calls to the API during application runs where I'm testing and want the app code to run faster. Fixtures will be returned instead of waiting for the API to speed up the tests.

    describe('fixture creation', () => {
      it('should save fixtures of every endpoint that matches a url', () => {
        // @ts-ignore
        cy.login()
        cy.intercept('https://covfefe/redfish/v1/Chassis/Asrock/PCIeDevices/**', saveResponse)
        cy.visit('/ubmc/system/pci-topology')
      })
    })

How do I write saveResponses to store the json responses from the endpoints that match the url pattern above?

    const saveResponse = req => {
      req.continue((res) => {
        console.log('saved a file', `cypress/fixtures/${ req.url.replace('https://covfefe/', '') }.json`)
        cy.writeFile(
         `cypress/fixtures/${ req.url.replace('https://covfefe/', '') }.json`,
          res.body)
      })
    }

I get the "saved a file ..." console logs, but I checked the fixtures directory and it's empty. There are no errors, and placing a debugger statement inside the res handler doesn't tell me anything meaningful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晨与橙与城 2025-02-15 13:52:47

写下这样的拦截以传达对您功能的响应。

cy.intercept('https://covfefe/redfish/v1/Chassis/Asrock/PCIeDevices/**')
  .then( (response) => {
    saveResponse(response)
  })

我不确定响应中返回的内容,但您应该能够拉出完整的路径并使用它来编写您的文件名。

Write your intercept like this to pass the response to your function.

cy.intercept('https://covfefe/redfish/v1/Chassis/Asrock/PCIeDevices/**')
  .then( (response) => {
    saveResponse(response)
  })

Im not sure what is returned in your response but you should be able to pull out the full path and use that to write your file name.

怪异←思 2025-02-15 13:52:47

这对我有用,将API响应保存在固定装置文件夹中。使用一些类似的逻辑在下面的第二个规格中返回灯具。

describe('fixture creation', () => {
  it('should save fixtures of every endpoint that matches a url', () => {
    cy.login()
    cy.visit('/ubmc/system/pci-topology')
    cy.intercept('https://covfefe/redfish/v1/Chassis/Asrock/PCIeDevices/**',
      req => {
        req.continue((res) => {
          cy.now('writeFile',
            `cypress/fixtures/${ req.url.replace('https://covfefe/', '') }.json`,
            res.body)
        })
      })
  })

  it('should produce results from the fixtures directory', () => {
    cy.intercept('**redfish/v1/Chassis/Asrock/PCIeDevices/**', req => {
      req.reply({
        fixture: `${ req.url.replace('https://covfefe/', '') }.json`
      })
    })

    // @ts-ignore
    cy.login()
    cy.visit('/ubmc/system/pci-topology')
  })
})

/*
~$ tree cypress/fixtures
├── redfish
│     └── v1
│         └── Chassis
│             └── Asrock
│                 └── PCIeDevices
│                     ├── 00_00_00.json
│                     ├── ...
*/

This worked for me, saving the API responses in the fixtures folder. The fixtures are returned in the second spec below using some similar logic.

describe('fixture creation', () => {
  it('should save fixtures of every endpoint that matches a url', () => {
    cy.login()
    cy.visit('/ubmc/system/pci-topology')
    cy.intercept('https://covfefe/redfish/v1/Chassis/Asrock/PCIeDevices/**',
      req => {
        req.continue((res) => {
          cy.now('writeFile',
            `cypress/fixtures/${ req.url.replace('https://covfefe/', '') }.json`,
            res.body)
        })
      })
  })

  it('should produce results from the fixtures directory', () => {
    cy.intercept('**redfish/v1/Chassis/Asrock/PCIeDevices/**', req => {
      req.reply({
        fixture: `${ req.url.replace('https://covfefe/', '') }.json`
      })
    })

    // @ts-ignore
    cy.login()
    cy.visit('/ubmc/system/pci-topology')
  })
})

/*
~$ tree cypress/fixtures
├── redfish
│     └── v1
│         └── Chassis
│             └── Asrock
│                 └── PCIeDevices
│                     ├── 00_00_00.json
│                     ├── ...
*/
痴情换悲伤 2025-02-15 13:52:47

文件名称上有很多非法字符,因此您必须将它们转换为其他内容。

我建议删除路径分离器/,以避免将固定装置保存在嵌套的文件结构中。手动清除它们时可能会有许可问题。

构建一个函数以将URL转换为对操作系统友好的名称,您可以使用该名称始终如一地编写读取文件。

const urlToFilename = (url) => {
  const nogoChars = '#%&{}\/<>*?$!:"+|='
  const filename = nogoChars.split('').reduce((fname,char) => {
    return fname.replaceAll(char, '-')
  }, url)
  return filename
}

问题执行cy.writefile()在事件处理程序中

在等待拦截后尝试保存。

cy.intercept('https://covfefe/redfish/v1/Chassis/Asrock/PCIeDevices/**')
  .as('saveMyResponse')
cy.visit('/ubmc/system/pci-topology')
cy.wait('@saveMyResponse').then(interception => {
  saveResponse(interception.request.url, interception.response.body)
})

There's a lot of characters that are illegal in file names, so you would have to transform them into something else.

I'd recommend removing path separators / to avoid the fixture saving in a nested file structure. There can be permission problems when manually clearing them down.

Build a function to transform the URL to an OS-friendly name, which you can use to consistently write and read the file.

const urlToFilename = (url) => {
  const nogoChars = '#%&{}\/<>*?$!:"+|='
  const filename = nogoChars.split('').reduce((fname,char) => {
    return fname.replaceAll(char, '-')
  }, url)
  return filename
}

Problem executing cy.writeFile() in event handler

Try saving after waiting for the intercept.

cy.intercept('https://covfefe/redfish/v1/Chassis/Asrock/PCIeDevices/**')
  .as('saveMyResponse')
cy.visit('/ubmc/system/pci-topology')
cy.wait('@saveMyResponse').then(interception => {
  saveResponse(interception.request.url, interception.response.body)
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文