英文:
How to define some post values as variable
问题
我有一个像这样的帖子响应
const requestbody = {
"days": x,
"amount": x,
"administrationFee": 2.99,
"conclusionFee": 100,
"currency": "EUR"
}
每次都会有不同的天数和金额。如何在每个请求上分别定义它们?
我尝试过将它们设置为常量,并在每个请求之前定义它们。
英文:
I have a post response like this
const requestbody=
{
"days": x,
"amount": x,
"administrationFee": 2.99,
"conclusionFee": 100,
"currency": "EUR"
}
The days and amount are different each time. How can I define them differently on each request?
I've tried setting them as constant and defining them before each request
答案1
得分: 4
创建一个自定义命令来添加这些数值。
这样,每次调用命令时,您将封装保持不变的数值。
Cypress.Commands.add('requestWith', (days, amount) => {
const requestBody = {
days, // 从参数获取
amount, // 从参数获取
administrationFee: 2.99,
conclusionFee: 100,
currency: "EUR"
}
return cy.request({
url: 'my-url',
body: requestBody,
})
})
cy.requestWith(3, 200)
.then(response => {
...
})
英文:
Create a custom command to add the values.
That way you will encapsulate the values that remain the same each time you call the command.
Cypress.Commands.add('requestWith', (days, amount) => {
const requestbody= {
days, // from parameter
amount, // from parameter
administrationFee: 2.99,
conclusionFee: 100,
currency: "EUR"
}
return cy.request({
url: 'my-url',
body: requestBody,
})
})
cy.requestWith(3, 200)
.then(response => {
...
})
答案2
得分: 0
const requestbody= { "days": 0, "amount": 0, "administrationFee": 2.99, "conclusionFee": 100, "currency": "EUR" } ;
//for each Post
requestbody.days = 10;
requestbody.amount = 1.50;
// Do the Post
英文:
const requestbody= { "days": 0, "amount": 0, "administrationFee": 2.99, > "conclusionFee": 100, "currency": "EUR" } ;
//for each Post
requestbody.days = 10;
requestbody.amount = 1.50;
// Do the Post
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论