javascript - พบข้อผิดพลาด NoSuchElementError เมื่อตรวจสอบว่าม็อดัลเปิดอยู่หรือไม่
ฉันมีการทดสอบที่ฉันพยายามตรวจสอบว่า modal เปิดอยู่หรือไม่ หาก modal เปิดอยู่ การทดสอบจะทำงานได้ตามปกติ แต่เมื่อมันไม่เปิด การทดสอบจะล้มเหลวพร้อมข้อยกเว้น NoSuchElementError
นี่คือเวอร์ชันปัจจุบันของการทดสอบของฉัน:
fit('Share is actually shared', () => {
console.log(`\n ### Share is actually shared ${entity.name} ### \n`)
listView.clickSharing(entity.name)
const sharedWithBefore = sharing.sharedUsers.count()
sharing.createShare(sharee)
sharing.shareButton.click()
// Handle 'Share with Everyone'
const isPresent = browser.isElementPresent(sharing.modal.getWebElement())
isPresent.then(result => {
console.log('is the modal present: ' + result)
if (result) {
sharing.modalAcceptButton.click()
}
})
const sharedWithAfter = sharing.sharedUsers.count()
Promise.all([sharedWithBefore, sharedWithAfter]).then(results => {
expect(results[0] != results[1]).toBe(true)
})
sharing.title.click()
common.escapeFromBody()
})
ปัญหาอยู่ในบล็อกที่ตามหลังคอมเมนต์ // HandleShare with Everyone`
ฉันได้ลองทำตามขั้นตอนต่อไปนี้แล้วแต่ไม่มีอะไรได้ผล ถ้าโมดอลไม่ปรากฏ มันก็ล้มเหลวทันที
const isPresent = sharing.modal.isPresent()
if (isPresent) {
sharing.modalAcceptButton.click()
} //THIS FAILS WHEN MODAL NOT PRESENT
const isPresent = sharing.modal.isPresent()
isPresent.then(result => {
if (result) {
sharing.modalAcceptButton.click()
} //THIS FAILS WHEN MODAL NOT PRESENT
})
const isPresent = sharing.modal.isPresent()
const isDisplayed = sharing.modal.isDisplayed()
if (isPresent && isDisplayed) {
sharing.modalAcceptButton.click()
} //THIS FAILS WHEN MODAL NOT PRESENT
// THIS ALSO FAILS
const isPresent = browser.isElementPresent(sharing.modal.getWebElement())
isPresent.then(present => {
if (present) {
sharing.modalAcceptButton.click()
const sharedWithAfter = sharing.sharedUsers.count()
Promise.all([sharedWithBefore, sharedWithAfter]).then(results => {
expect(results[0] != results[1]).toBe(true)
})
} else {
const sharedWithAfter = sharing.sharedUsers.count()
Promise.all([sharedWithBefore, sharedWithAfter]).then(results => {
expect(results[0] != results[1]).toBe(true)
})
}
})
// This is likewise failing
const isPresent = browser.isElementPresent(sharing.modal.getWebElement())
isPresent.then(present => {
try {
if (present) {
sharing.modalAcceptButton.click()
const sharedWithAfter = sharing.sharedUsers.count()
Promise.all([sharedWithBefore, sharedWithAfter]).then(results => {
expect(results[0] != results[1]).toBe(true)
})
}
} catch (NoSuchElementError) {
console.log('The Modal is not present continuing')
const sharedWithAfter = sharing.sharedUsers.count()
Promise.all([sharedWithBefore, sharedWithAfter]).then(results => {
expect(results[0] != results[1]).toBe(true)
})
}
})
ฉันไม่ค่อยแน่ใจว่าควรลองอะไรจากตรงนี้ ถ้าตัว modal ไม่มีอยู่ การทดสอบก็ล้มเหลวทันที ฉันทำอะไรผิดอยู่หรือ?
คำตอบ #1
นี่คือวิธีที่ฉันแก้ปัญหานี้ได้ในที่สุด:
sharing.modal.isPresent().then(present => {
if (present) {
sharing.modalAcceptButton.click()
} else {
console.log('The Modal is not present continuing')
}
})
คำตอบ #2
ตรวจสอบกับ sharing และ sharing.modal ว่ามีค่าหรือไม่ หากเป็น Undefined จากนั้นคุณสามารถดำเนินการตามที่คุณต้องการได้ เช่น: if(sharing != undefined) { if(sharing.modal != undefined) }
