我的測驗結果是在表格中添加了一行。在 afterEach 鉤子中,我想添加一個方法來在測驗運行后洗掉最新的行,所以我寫了這段代碼:
deleteTheNewestRequest() {
cy.get('table#return-request-list').find('tr').eq(1).then( firstRow => {
cy.wrap(firstRow).find('td').eq(0).invoke('text').as('rowId')
cy.wrap(firstRow).find('a[title="Delete"]').click()
cy.get('button.btn-delete-return-request').click()
cy.wrap(firstRow).find('td').first().should('not.have.text', this.rowId)
})
}
使用“then”,我將保存一個表格元素以供以后查找和操作其中的元素。我想首先保存測驗成功運行后添加的表中最新行的值,然后洗掉并作為最后一步確保具有已洗掉 ID 的行實際上已從表中消失。問題是 cypress 測驗運行器斷言將其標記為綠色,因為它看到的值不等于“未定義”而不是,比如說 3794。
uj5u.com熱心網友回復:
這看起來像罪魁禍首cy.get('table#return-request-list').find('tr').eq(1)。您獲得要洗掉的行,將其洗掉,然后包裝洗掉行元素,即使它不存在。所以它會回傳undefined。相反,您最初應該獲取所有行。
function deleteTheNewestRequest(){
// get all rows in table
cy.get('table#return-request-list').find('tr').then( rows => {
// get last row and store rowId
cy.wrap(rows).last().find('td').first().invoke('text').as('deletedRowId')
cy.wrap(rows).last().find('a[title="Delete"]').click()
cy.get('button.btn-delete-return-request').click()
// check new last row does not have same rowId
cy.wrap(rows).last().find('td').first().should('not.have.text', this.rowId)
})
}
uj5u.com熱心網友回復:
分解步驟可能會更好。
deleteTheNewestRequest() {
cy.get('table#return-request-list')
.find('tr').eq(1)
.as('firstRow') // save row ref for delete
.find('td').eq(0)
.invoke('text').as('rowId') // save rowId text for check
cy.get('@firstRow').find('a[title="Delete"]').click()
cy.get('button.btn-delete-return-request').click()
cy.get('@rowId').then(rowId => {
cy.contains('tr', rowId).should('not.exist') // "standard" non-existence check
})
}
Ref測驗元素是否不存在
我會檢查是否.find('tr').eq(1)是第一行,也許應該是.find('tr').eq(0)?
uj5u.com熱心網友回復:
當您洗掉該行時,它會從 DOM 中洗掉,但測驗仍然有一個指向它的指標(firstRow 變數),從而阻止垃圾收集器將其從記憶體中洗掉。
所以 cy.wrap(firstRow)仍然指向完整的行,并且它仍然具有相同的 rowId。
您想測驗用戶看到的內容,即firstRow不再存在于 DOM 中的內容。
//cy.wrap(firstRow).find('td').first().should('not.have.text', this.rowId) // not this
cy.wrap(firstRow).should(firstRow => {
expect(Cypress.dom.isAttached(firstRow)).to.eq(false)
})
或者測驗 DOM 中沒有行有 rowId
cy.get('@rowId').then(rowId => {
cy.contains('tr', rowId).should('not.exist')
})
uj5u.com熱心網友回復:
你可以這樣做:
deleteTheNewestRequest() {
cy.get('table#return-request-list').find('tr').eq(1).then(firstRow => {
cy.wrap(firstRow).find('td').eq(0).invoke('text').as('rowId')
cy.wrap(firstRow).find('a[title="Delete"]').click()
cy.get('button.btn-delete-return-request').click()
cy.get('@rowId').then((rowId) => {
cy.wrap(firstRow).find('td').first().should('not.have.text', rowId)
})
})
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/424113.html
標籤:javascript 测试 自动化 柏
上一篇:如何在R中測驗無限回圈錯誤
