javascript – 在检查模式是否打开时获取NoSuchElementError

我有一个测试,我试图检查并查看模态是否打开.如果模态是打开的,那么测试工作正常,当它没有打开时,测试失败并出现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)
                    })

                }
            })

我不确定在这里尝试什么.如果模态不存在则测试完全失败.我做错了什么?

最佳答案 这是我最终解决它的方式:

sharing.modal.isPresent().then(present => {
    if (present) {
        sharing.modalAcceptButton.click()
    } else {
        console.log('The Modal is not present continuing')
    }
 })
点赞