2017-09-01 33 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) }