I try to read out a table and compare all textvalues. If there are two times the same value, the test should fail. I try this by using a counter. If counter == 2 then fail. But I cant enter the if-statement, cause cypress cant compare the textvalue..
let duplicateCounter = 0
cy.get('[data-cy="projectList"]')
.find('[data-cy="project.name"]') // I'm using my own marker for my table
.each((MyElement, MyIndex, MyContentOfArray) => {
cy.wrap(MyElement.text())
.then( (tmp) => {
cy.log('tmp = ' tmp)
if(tmp == "001_AA_Testproject") // it never enters the if-Block, cause textcomparing isnt working
{
cy.log('============================================ if Block ' )
duplicateCounter = 1
}
cy.log('duplicateCounter = ' duplicateCounter)
})
cy.get(duplicateCounter ).should('eq',1)
How can I use if-Statements in .each()-loops?
HERE IS THE SOLUTION:
var projectName = '00_AA_Testprojekt Duplikatscheck';
cy.get('[data-cy="projectList"]')
.find('[data-cy="project.name"]')
// .filter(':contains("00_AA_Testprojekt Duplikatscheck")')
.filter(':contains("' projectName '")')
.should('have.length', 1)
THANKS to Alapan Das
CodePudding user response:
- You can do something like this:
var duplicateCounter = 0
cy.get('[data-cy="projectList"]')
.find('[data-cy="project.name"]')
.each(($ele, index, $list) => {
if ($ele.text() == '001_AA_Testproject') {
duplicateCounter
}
})
expect(duplicateCounter).to.equal(1) //If passed then no duplicate, if failed that means duplicate
So now with each iteration, the value of the duplicateCounter will increase. And then when the looping finishes we are asserting the value of duplicateCounter to be 1. If passed, then no duplicated; if failed it means we have a duplicate.
- An easier way would be to use the
filter()
command to just search for the desired text and then check the length. Something like:
cy.get('[data-cy="projectList"]')
.find('[data-cy="project.name"]')
.filter(':contains("001_AA_Testproject")')
.should('have.length', 1)
CodePudding user response:
To compare for any project duplicates, use a unique array.
cy.get('[data-cy="projectList"]')
.find('[data-cy="project.name"]')
.then($els => [...$els].map(el => el.innerText.trim())) // convert to texts
.then(texts => {
// Find the unique texts
const unique = texts.filter(item, index) => texts.indexOf(item) === index)
const duplicateCounter = texts.length - unique.length;
// Assert that all the texts are unique
expect(duplicateCounter).to.eq(0)
// or
expect(texts).to.deep.eq(unique)
})
For one project name only, your method is ok but
if(tmp == "001_AA_Testproject")
maybe does not work because of surrounding white space. Try
if(tmp.trim() === "001_AA_Testproject")
Full test
let duplicateCounter = 0
cy.get('[data-cy="projectList"]')
.find('[data-cy="project.name"]')
.each((MyElement, MyIndex, MyContentOfArray) => {
const text = MyElement.text()
cy.log('text = ' text)
if(tmp.trim() === "001_AA_Testproject") {
cy.log('============================================ if Block ' )
duplicateCounter = 1
}
})
.then() => { // Check counter after each() has finished
cy.log('duplicateCounter = ' duplicateCounter)
cy.wrap(duplicateCounter).should('eq',1)
// or
expect(duplicateCounter).to.eq(1)
})
// Does not work here, this will only check initial value 0
expect(duplicateCounter).to.eq(1)