Cypress:有什么方法可以用测试用例中生成的数据替换固定字段以生成动态有效负载

问题描述 投票:0回答:1

我是 Cypress 的新手。我想知道是否有任何方法可以通过用 cypress 测试中以编程方式生成的值替换 JSON 文件的值来生成动态有效负载。我们在放心中所做的事情是替换 JSON 文件的 %s。 我在网上搜索了很多但没有找到。一些类似的问题是 这对我不起作用 我想将动态 json 正文传递给 cypress request() 函数并定义有效负载值

我有以下 json 文件

{
    "name": "Ganesh vishal kumar",
    "gender": "Male",
    "email": "[email protected]",
    "status": "active"
}

我想要一个动态 JSON 文件装置。在下面的测试中,我以编程方式生成电子邮件 ID 并直接在 JSON 正文中使用它。这里我想使用JSON文件fixture

it('first post test', () => {
        var pattern = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        var emailId = "";

        for (var x = 0; x < 10; x++) {
            emailId = emailId + pattern.charAt(Math.floor(Math.random() * pattern.length));
        }
        emailId = emailId + '@test.org'
        cy.request({
            method: 'POST',
            url: 'https://gorest.co.in/public/v2/users',
            body: {
                "name": "Ganesh Kumar",
                "gender": "Male",
                "email": emailId,
                "status": "active"
            },
            headers: {
                'authorization': 'Bearer 18806c8605b08cabb3c9ce642cbc3a21e1a8942a96c3b908a7e0e27c3b5cf354'
            }
        }).then((res) => {
            expect(res.status).to.eq(201)
        })
    })

json typescript cypress web-api-testing
1个回答
0
投票

您是否会不做同样的事情,而是使用夹具数据?

因此,我们在这里依赖于使用 spread 运算符 混合对象的属性,然后在列表末尾添加全新的属性。

请参考以下页面来了解:扩展语法 (...)

  ...
  let emailId =  .. // calculation for email

  cy.fixture('my-fixture-file-that-has-most-of-the-data-required.json')
    .then(fixtureData => {
      const body = {...fixtureData, email: emailId}
      cy.request({
        ..
        body,              // add the body variable from above line
        headers: {
          ...
      })
© www.soinside.com 2019 - 2024. All rights reserved.