javascript - Får NoSuchElementError när man kontrollerar om en modal är öppen eller inte


Jag har ett test där jag försöker kontrollera och se om en modal är öppen eller inte. Om modalen är öppen fungerar testet bra, när den inte är öppen misslyckas testet med ett NoSuchElementError-undantag.

Här är min nuvarande version av mitt 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()
        })

Problemet i blocket som följer efter // Handle Dela med alla` kommentaren.

Jag har försökt göra följande och inget av det fungerar, om modalen inte dyker upp misslyckas det helt enkelt.

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

                }
            })

Jag är inte riktigt säker på vad jag ska prova härifrån. Om modalen inte är närvarande misslyckas testet helt enkelt. Vad gör jag fel?

Svar #1

Här är hur jag äntligen löste det:

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

Svar #2

Kontrollera med sharing och sharing.modal har ett värde eftersom Är det odefinierat då kan du fortsätta som du vill. Som: if(sharing != undefined) { if(sharing.modal != undefined) }

Relaterade artiklar