繁体   English   中英

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

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

我对像这样的元素的值有疑问:

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

假设我有10-20个类,其值类似于此处,那么我可以使用:

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

以获得适当的值,但只有在使用this.Field1的其他测试中才有可能。 如何在不使用then和.text()的情况下在同一测试中获得价值?

可能吗? 我有很多字段,我想通过一个测试来检查下一个视图中的正确值。

有没有人有类似的问题? 谢谢

听起来好像您可能要使用.its

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

它需要与以前的命令(如get)链接在一起。 您也可以按照赛普拉斯的示例在函数中使用它

赛普拉斯展示了DOM元素的示例

获取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
})

您可以访问函数,然后深入研究它们自己的属性,而不是调用它们。

// 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

您可以使用点表示法深入嵌套属性。

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

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

有关示例和规则的完整列表,请查看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