简体   繁体   English

如何从赛普拉斯工具中的元素获取价值?

[英]How to take value from the element in the Cypress tool?

I have a problem with taking value of the element like: 我对像这样的元素的值有疑问:

<div class="Test">Number 10</div>

Let say, that I have 10-20 classes with values like here, then I can use: 假设我有10-20个类,其值类似于此处,那么我可以使用:

cy.get('.Test').invoke('text').as.('Field1')

to get proper value, but it is only possible in different test by using this.Field1. 以获得适当的值,但只有在使用this.Field1的其他测试中才有可能。 How to get value in the same test without using: then and .text() ? 如何在不使用then和.text()的情况下在同一测试中获得价值?

Is it possible? 可能吗? I have many fields and I would like do it in one single test to check proper values in the next view. 我有很多字段,我想通过一个测试来检查下一个视图中的正确值。

Did anyone has similar problem? 有没有人有类似的问题? Thanks 谢谢

It sounds as though you may want to use .its 听起来好像您可能要使用.its

cy.get('selector').its(propertyName).should('contain', 'string')

its needs to be chained off a previous command like get. 它需要与以前的命令(如get)链接在一起。 You can also use this in a function as per Cypress's example 您也可以按照赛普拉斯的示例在函数中使用它

Cypress shows an example for DOM elements 赛普拉斯展示了DOM元素的示例

Get the length property of a DOM element 获取DOM元素的length属性

cy
  .get('ul li')       // this yields us a jquery object
  .its('length')      // calls 'length' property returning that value
  .should('be.gt', 2) // ensure the length is greater than 2
})

You can access functions to then drill into their own properties instead of invoking them. 您可以访问函数,然后深入研究它们自己的属性,而不是调用它们。

// Your app code
// a basic Factory constructor
const Factory = (arg) => {
  // ...
}

Factory.create = (arg) => {
  return new Factory(arg)
}

// assign it to the window
window.Factory = Factory

cy
  .window()                 // yields window object
  .its('Factory')           // yields Factory function
  .invoke('create', 'arg')  // now invoke properties on itv

You can drill into nested properties by using dot notation. 您可以使用点表示法深入嵌套属性。

const user = {
  contacts: {
    work: {
      name: 'Kamil'
    }
  }
}

cy.wrap(user).its('contacts.work.name').should('eq', 'Kamil') // true

For the full list of examples and rules check out Cypress.io documents https://docs.cypress.io/api/commands/its.html#Syntax 有关示例和规则的完整列表,请查看Cypress.io文档https://docs.cypress.io/api/commands/its.html#Syntax

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM