admin管理员组文章数量:1318770
Below is my html for a dropdown control:
I want to verify that drop down has following items: ["5","15","30","45"]
I have written following piece of code:
value=["5","15","30","45"]
cy.get(seelctor).invoke('val').should('deep.equal', value)
I do not want to select all of these items just want to verify that drop down has them. Also, My html does not has the value attribute. How do i assert based on text ??
Below is my html for a dropdown control:
I want to verify that drop down has following items: ["5","15","30","45"]
I have written following piece of code:
value=["5","15","30","45"]
cy.get(seelctor).invoke('val').should('deep.equal', value)
I do not want to select all of these items just want to verify that drop down has them. Also, My html does not has the value attribute. How do i assert based on text ??
Share Improve this question asked Mar 18, 2021 at 9:07 bucky barnsbucky barns 3938 silver badges19 bronze badges2 Answers
Reset to default 4The values between tags like <li>15</li>
are accessed on individual element with .invoke('text')
.
invoke('val')
is for <input>
and <textarea>
elements.
But if you select multiple elements, the texts will mash together
cy.get('li') // multiple elements selected
.invoke('text') // joins all text together, result is "5153045"
.should('eq', '5153045') // passes
In your case it's ok, but feels a little clumsy. In another scenario you can get additional white space between texts which makes it harder to pare.
You can use jQuery to extract individual texts, and they can be trimmed to make a better match when extra white-space is present
cy.get('li')
.then($els => Cypress.$.map($els, (el) => el.innerText.trim())) // uses jQuery .map()
.should(values => {
expect(values).to.deep.eq(["5","15","30","45"])
})
But if your dropdown items are fetched from an API, you should put as much of the calculation as possible inside the .should()
callback, because Cypress retries until passing
cy.get('li')
.should($els => { // retries here
const values = [...$els].map(el => el.innerText.trim()) // uses Array .map()
expect(values).to.deep.eq(["5","15","30","45"])
})
Create a fixture file data.json
inside cypress/fixtures
and write:
{
"dropdownValues": ["5","15","30","45"]
}
Your test will be like:
describe('SO Question', function() {
beforeEach(function() {
//Load Fixture File
cy.fixture('data').then(function(testdata) {
this.testdata = testdata
})
})
it('Iterate dropdown and validate', function() {
cy.get(selector).each(($ele, i) => {
expect($ele).to.have.text(this.testdata.dropdownValues[i])
})
})
})
本文标签: javascriptcypress how to verify all dropdown items without selecting any itemStack Overflow
版权声明:本文标题:javascript - cypress how to verify all dropdown items without selecting any item - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742047814a2417892.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论