Getting NoSuchElementError on check to check if modal is open or not

I have a test where I am trying to check and see if a modal is open or not. If modally open, the test runs fine when it is not open, the test ends with an exception NoSuchElementError.

Here is my current version of my test:

            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()
        })

The problem is in the block after the comment // HandleShare with Everyone.

I tried to do the following, and none of them work, if the modal does not appear, it just fails.

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)
                    })

                }
            })

I'm not quite sure what to try from here. If there is no modal, then the test simply fails. What am I doing wrong?

+4
source share
2 answers

, , :

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

share.modal Undefined, . : if(sharing != undefined) { if(sharing.modal != undefined) }

0

Source: https://habr.com/ru/post/1684918/


All Articles