【发布时间】:2021-08-28 12:32:05
【问题描述】:
我正在尝试获取属性的值并对其进行处理。我阅读了文档,我认为我非常严格地遵循它所说的内容,这是我想要实现的非常简单的事情。
我有一个这样的 HTML 元素:<span class="mySpan" data-test-count="8" />,我想读取 data-test-count 的值,所以我喜欢:
cy.get('.mySpan').invoke('attr', 'data-test-count').then((count) => cy.log(count))
它没有记录任何内容,但在我看来,这与我在文档 here 上找到的方法相同。
cy.get('input')
.invoke('getKendoDropDownList')
.then((dropDownList) => {
// yields the return of $input.getKendoDropDownList()
return dropDownList.select('apples')
})
奇怪的是,我实际上能够得到那个属性的值,这运行得很顺利:
cy.get('.mySpan').invoke('data-test-count').should('equal', '8') // this passes :O
更新:
似乎该问题仅发生在通过映射数组创建的元素上。下面显示一些更详细的代码:
这是我的组件(它是 React)
<Container>
<span className="mySpan" data-test-count="8">
ciao
</span>
{counts.map((count) => (
<Info
className={`counter-${count.name}`}
key={count.name}
data-test={`counter-${count.name}`}
data-test-count={count.count}
>
{`${t(`${modelName}.${count.name}`)} (${count.count ?? 0})`}
</Info>
))}
</Container>
这不会记录任何内容(试图获取Infos 之一的值)但是元素是可见的并且.should('be.visible') 通过了。
cy.get('.counter-active')
.should('be.visible')
.invoke('attr', 'data-test-count')
.then((count) => cy.log(count))
这会记录8:
cy.get('.mySpan')
.should('be.visible')
.invoke('attr', 'data-test-count')
.then((count) => cy.log(count))
【问题讨论】:
标签: javascript jquery cypress