赛普拉斯:在JSON响应中存根特定键

安迪山

我想在赛普拉斯中添加以下响应。特别是对key进行存根ds_version,其可能的值为0、1或2。每个值将在UI上显示不同的元素。

fixtures/user.json

{
"email": "[email protected]",
"firstName": "Test",
"lastName": "Test",
"ds_version": 0, --> switch this value to 0, 1, 2 for different test scenarios
"country": "Indonesia",
...
}

我能够通过提供整个JSON作为cy.route()参数来实现这一点,如下所示:

cy.server();
cy.route('GET', '/users/current', 'fixture:user.json').as('getUsersCurrent');

看来,如果我想测试ds_version=1ds_version=2,则需要提供具有更改值的其他JSON。有没有一种方法可以提供不同的ds_version值,而其余的JSON保持不变?

Alapan das

您可以使用cypresscy.route2()

  1. cypress.json"experimentalNetworkStubbing": true

  2. 在您的test.spec文件中写:

    cy.route2('GET', '/users/current', (req) => {
        req.reply((res) => {
            let list = JSON.parse(res.body)
            list.ds_version = 1
            res.send(list)
        })
    })
    

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章