将`.then`中的内容分离到Cypress中的一个新函数中。

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

Separating the contents of .then to a new function in Cypress

问题

如果可能的话,我想要为最后两行创建一个新的**自定义命令**

```ts
    animal.animalObj.name = 'Rex';
    animal.animalObj.randomInfo = ["喜欢叫!"];

该命令将需要一个字符串和一个字符串数组,并同时将这两个值分配给namerandomInfo。这样,我只需调用该自定义命令并提供两个动态值即可。


<details>
<summary>英文:</summary>

I have a function:

```ts
let animal: {
    animalObj: {
        name: string;
        randomInfo: string[]
    }
} = {
    animalObj: {
        name: &#39;&#39;,
        randomInfo: [&#39;&#39;]
    }
}

cy.request({
    method: &#39;POST&#39;,
    url: &#39;/api/animal&#39;,
    body: animal
}).then($el =&gt; {
    animal.animalObj = $el.body.Order

    animal.animalObj.name = &#39;Rex&#39;
    animal.animalObj.randomInfo = [&quot;Likes to bark!&quot;]
})

If it's possible, I would like to create a new custom command for the last two lines:

    animal.animalObj.name = &#39;Rex&#39;
    animal.animalObj.randomInfo = [&quot;Likes to bark!&quot;]

Where it will require a string and string[] and at the same time, assigns those two values to name and randomInfo. That way, I will only need to call that custom command and supply 2 dynamic values.

答案1

得分: 5

// 自定义命令紧随`cy.request()`之后,是一个“子”命令。
// 它有一个特殊的参数`{prevSubject:true}`,用于传递请求响应。

Cypress.Commands.add('setAnimal', {prevSubject:true}, ($response, name, randomInfo) => {
  const animal = {}
  animal.animalObj = $response.body.Order || {}
  animal.animalObj['name'] = name 
  animal.animalObj['randomInfo'] = randomInfo 
  return cy.wrap(animal)
})


cy.request({
  method: 'POST',
  url: '/api/animal',
  body: animal
})
.setAnimal('Rex', ["Likes to bark!"])  // 响应会自动传递
.as('animal')

// 稍后

cy.get('@animal')
  .then(animal => {
    expect(animal.animalObj.name).to.eq('Rex')
  })
})
英文:

A custom command that follows cy.request() is a "child" command.

It has a special parameter {prevSubject:true} to pass in the request response.

Cypress.Commands.add(&#39;setAnimal&#39;, {prevSubject:true}, ($response, name, randomInfo) =&gt; {
  const animal = {}
  animal.animalObj = $response.body.Order || {}
  animal.animalObj[&#39;name&#39;] = name 
  animal.animalObj[&#39;randomInfo&#39;] = randomInfo 
  return cy.wrap(animal)
})


cy.request({
  method: &#39;POST&#39;,
  url: &#39;/api/animal&#39;,
  body: animal
})
.setAnimal(&#39;Rex&#39;, [&quot;Likes to bark!&quot;])  // response is passed automatically
.as(&#39;animal&#39;)

// later

cy.get(&#39;@animal&#39;)
  .then(animal =&gt; {
    expect(animal.animalObj.name).to.eq(&#39;Rex&#39;)
  })
})

答案2

得分: 0

相比于创建一个自定义函数你可以简单地创建一个`Animal`并在该类上有一个函数同时更新这两个值

class Animal {
  data = { animalObj: { name: &#39;&#39;, randomInfo: [&#39;&#39;] } };

  constructor(name: string = &#39;&#39;, randomInfo: string[] = [&#39;&#39;]) {
    this.data.animalObj.name = name;
    this.data.animalObj.randomInfo = randomInfo;
  }

  update(name: string, randomInfo: string[]) {
    this.data.animalObj.name = name;
    this.data.animalObj.randomInfo = randomInfo;
  }
}
...
let animal = new Animal();

cy.request({
    method: &#39;POST&#39;,
    url: &#39;/api/animal&#39;,
    body: animal.data // 假设请求体必须为 `{ animalObj: {} }`
}).then($el =&gt; {
    animal.update(&#39;Rex&#39;, [&#39;Likes to Bark!&#39;]);
    // 如果你只需要更新一个值
    animal.data.animalObj.name = $el.body.name
})
英文:

Instead of creating a custom function, you could simply make an Animal class, and have a function on the class that updates those two values at once.

class Animal {
  data = { animalObj: { name: &#39;&#39;, randomInfo: [&#39;&#39;] } };

  constructor(name: string = &#39;&#39;, randomInfo: string[] = [&#39;&#39;]) {
    this.data.animalObj.name = name;
    this.data.animalObj.randomInfo = randomInfo;
  }

  update(name: string, randomInfo: string[]) {
    this.data.animalObj.name = name;
    this.data.animalObj.randomInfo = randomInfo;
  }
}
...
let animal = new Animal();

cy.request({
    method: &#39;POST&#39;,
    url: &#39;/api/animal&#39;,
    body: animal.data // assumes body has to have `{ animalObj: {} }`
}).then($el =&gt; {
    animal.update(&#39;Rex&#39;, [&#39;Likes to Bark!&#39;]);
    // if you need to only update one value
    animal.data.animalObj.name = $el.body.name
})

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

发表评论

匿名网友

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

确定