如何从 Cypress 工具中的元素中取值?
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。
如何在不使用的情况下在同一测试中获得价值:
然后.text()?
可能吗?我有很多字段,我想在一个测试中完成,以在下一个视图中检查正确的值。
有没有人遇到过类似的问题?谢谢
听起来您可能想使用 .its
cy.get('selector').its(propertyName).should('contain', 'string')
它需要链接到之前的命令,如 get。您也可以按照赛普拉斯的示例在函数中使用它
Cypress 显示了 DOM 个元素的示例
Get the length property of a DOM element
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
有关示例和规则的完整列表,请查看 Cypress.io 文档 https://docs.cypress.io/api/commands/its.html#Syntax
我在获取元素的值时遇到问题,例如:
<div class="Test">Number 10</div>
比方说,我有 10-20 类 具有像这里这样的值,那么我可以使用:
cy.get('.Test').invoke('text').as.('Field1')
获得正确的值,但只能在不同的测试中使用this.Field1。 如何在不使用的情况下在同一测试中获得价值: 然后.text()?
可能吗?我有很多字段,我想在一个测试中完成,以在下一个视图中检查正确的值。
有没有人遇到过类似的问题?谢谢
听起来您可能想使用 .its
cy.get('selector').its(propertyName).should('contain', 'string')
它需要链接到之前的命令,如 get。您也可以按照赛普拉斯的示例在函数中使用它
Cypress 显示了 DOM 个元素的示例
Get the length property of a DOM element
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
有关示例和规则的完整列表,请查看 Cypress.io 文档 https://docs.cypress.io/api/commands/its.html#Syntax