我嘗試讀出表格并比較所有文本值。如果有兩次相同的值,則測驗應該失敗。我通過使用計數器來嘗試這個。如果 counter == 2 則失敗。但是我無法輸入 if 陳述句,導致 cypress 無法比較 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)
如何在 .each()-loops 中使用 if-Statements?
uj5u.com熱心網友回復:
- 你可以這樣做:
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
所以現在每次迭代,duplicateCounter 的值都會增加。然后當回圈結束時,我們將重復計數器的值斷言為 1。如果通過,則沒有重復;如果失敗,則意味著我們有重復。
- 更簡單的方法是使用該
filter()命令只搜索所需的文本,然后檢查長度。就像是:
cy.get('[data-cy="projectList"]')
.find('[data-cy="project.name"]')
.filter(':contains("001_AA_Testproject")')
.should('have.length', 1)
uj5u.com熱心網友回復:
要比較任何專案重復項,請使用唯一陣列。
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) ?
?})
僅對于一個專案名稱,您的方法還可以,但是
if(tmp == "001_AA_Testproject")
由于周圍的空白,可能不起作用。嘗試
if(tmp.trim() === "001_AA_Testproject")
全面測驗
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/336534.html
上一篇:將命令列引數字串決議為posix_spawn/execve的陣列
下一篇:取決于錯誤輸出的條件操作
