2017-09-01 76 views
4

我有一个测试,我试图检查,看看模式是否打开。如果模态打开,则测试工作正常,未打开时测试失败,并显示异常。获取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() 
     }) 

在块以下的// Handle分享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) 
        }) 

       } 
      }) 

我不太确定该从哪里尝试。如果模态不存在,那么测试就会失败。我做错了什么?

回答

1

下面是我终于解决了它:

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

请与共享sharing.modal具有一定的价值是它未定义,那么你可以根据需要进行。 喜欢: if(sharing != undefined) { if(sharing.modal != undefined) }