如何将一些提交的数值定义为变量。

huangapple go评论72阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年7月11日 04:55:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76657286.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定