问题描述 投票:0回答:1

在下拉列表中,其余值在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>

 -Test

javascript json cypress fixtures
1个回答
3
投票

result

当缺少选项

texttextenter image description here 当您的选项文本被截断时,但是您想匹配固定装置中的文本,

"clarif..." -> "clarification" 您可以在比较之前转换固定数据。

也是,由于有124个条目,最好只记录任何失败而不是整个列表。

<select> <option>ALL</option> <option>clarif...</option> <option>a</option> <option>d</option> <option>b</option> </select> enter image description here 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()) } }) })

测试跑者:

	
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.