我想获取属性值并将其存储在变量中,我们如何在Cypress中实现这一目标。
在我的情况下,我想获取完整的类值并将其存储在变量中。
这段代码只给出了属性类的值,但是我该如何将提取的值存储在变量中。
cy.get('div[class*="ui-growl-item-container ui-state-highlight ui-corner-all ui-shadow ui-growl-message"]').invoke('attr', 'class')
我想获取属性值并将其存储在变量中,我们如何在Cypress中实现这一目标。
在我的情况下,我想获取完整的类值并将其存储在变量中。
这段代码只给出了属性类的值,但是我该如何将提取的值存储在变量中。
cy.get('div[class*="ui-growl-item-container ui-state-highlight ui-corner-all ui-shadow ui-growl-message"]').invoke('attr', 'class')
在您的用例中,这将会像:
cy.get('.searchable-group-selector-card-image')
.eq(4)
.invoke('attr', 'style')
.as('style_1')
cy.get('.another-element')
.invoke('attr', 'style')
.as('style_2')
// later on for example you could do
cy.get('@style_1').then(style_1 => {
cy.get('@style_2').then(style_2 => {
// Both values are available and any kind of assertion can be performed
expect(style_1).to.include(style_2)
});
});
cy.get('.searchable-group-selector-card-image')
.eq(4)
.invoke('attr', 'style')
.then(($style1) => {
const style1 = $style1
})
以下是我获取带有文本“Eat”的标签中for属性值的方法。
cy.contains('Eat').then(($label) => {
const id = $label.attr('for');
}
then()
,您可以将其存储在一个变量中。cy.get('div[class*="ui-growl-item-container ui-state-highlight ui-corner-all ui-shadow ui-growl-message"]').invoke('attr', 'class')
.then($growl-message => {
const message = $growl-message.text()
//do the checks with the variable message. For example:
cy.contains(message)
})
text()
将产生的对象 (cy.get('div[class*="ui-growl-item-container ui-state-highlight ui-corner-all ui-shadow ui-growl-message"]').invoke('attr', 'class')
) 转换为字符串。但不知道那是否会保留类名。 - Mr. J.