nik*_*kar -2 json typescript web-api-testing cypress
我是赛普拉斯的新手。我想知道是否有任何方法可以通过用 cypress 测试中以编程方式生成的值替换 JSON 文件的值来生成动态有效负载。我们在放心中所做的事情是替换 JSON 文件的 %s。我在网上搜索了很多但没有找到。类似的一些问题是这对我不起作用 我想将动态 json 正文传递给 cypress request() 函数并定义有效负载值
我有以下 json 文件
{
"name": "Ganesh vishal kumar",
"gender": "Male",
"email": "ganesh.kumar234@test.org",
"status": "active"
}
Run Code Online (Sandbox Code Playgroud)
我想要一个动态 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)
})
})
Run Code Online (Sandbox Code Playgroud)
您是否会不做同样的事情,而是使用夹具数据?
因此,在这里我们依靠使用扩展运算符来混合对象的属性,然后在列表的末尾添加我们全新的属性。
请参考以下页面以获取理解:扩展语法 (...)
...
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: {
...
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
347 次 |
| 最近记录: |