在下拉列表中,其余值在JSON文件中。 我已经完成了以下操作,但它不起作用。
cy.fixture('label.json')
.then(function (category) {
this.cat = category
})
})
cy.get('#labels').each(($ele, i) => {
expect($ele).to.have.text(this.category.label[i])
})
})
任何帮助都将受到高度赞赏,谢谢
一种方法是将选项元素映射到标签文本的数组 html
<select name="labels" id="labels">
<option value="" selected="">ALL</option>
<option value="a">a</option>
<option value="c">c</option>
<option value="b">b</option>
<option value="d">d</option>
</select>
当缺少选项
当
texttext
当您的选项文本被截断时,但是您想匹配固定装置中的文本,
"clarif..." -> "clarification"
您可以在比较之前转换固定数据。
<select>
<option>ALL</option>
<option>clarif...</option>
<option>a</option>
<option>d</option>
<option>b</option>
</select>
cy.get('option')
.then($options => Cypress.$.map($options, (option => option.innerText)))
.then(console.log)
.then(optionLabels => {
cy.fixture('my-labels.json').then(fixture => {
const truncate = (text) => text.length > 6 ? text.slice(0,6) + '...' : text;
const expected = fixture.label.concat(['ALL']) // add ALL
.map(truncate) // convert fixture to short strings
const notInDropdown = expected.filter(exp => !optionLabels.includes(exp))
const extraInDropdown = optionLabels.filter(label => !expected.includes(label))
if (notInDropdown.length) {
throw `Not in dropdown: ${notInDropdown.join(' ')}`
}
if (extraInDropdown.length) {
throw `Extra in dropdown: ${extraInDropdown.join(' ')}`
}
cy.log('Dropdown passes')
})
})
})
cy.fixture("labels.json").then((labels) => {
cy.get("select#labels option").each(($ele, i) => {
if ($ele.text().trim() == "ALL") {
expect(labels.label).to.not.include($ele.text().trim())
} else {
expect(labels.label).to.include($ele.text().trim())
}
})
})
测试跑者: