2015-04-27 22 views
0

我在我的角度測試之一這一行:NoSuchElementError當元素實際上有

element(by.id('male')).click(); 

而且測試與此消息失敗:

NoSuchElementError: No element found using locator: By.id("male") 

但是當我添加browser.sleep()到測試我可以看到頁面上有#male元素。

所以我試圖添加明確的等待,它沒有幫助。什麼可能是錯的?

+0

你試圖點擊的元素,最有可能在一個框架內..你能證實嗎? – sircapsalot

+0

另外,你能告訴我們,目標元素是否有'id =「male」',或者它是'name =「male」 – sircapsalot

回答

0

我經常使用下面的輔助函數來等待一個元素的存在或顯示。也許給他們一個嘗試,量角器將阻止,直到這些返回true。也許給他們一個去。

module.exports = { 
    /** 
    * Block until an element is displayed on the page. 
    * 
    * @param {object} elem 
    * The element to watch for. 
    * 
    * @param {number} [customTimeout=10000] 
    * How long to wait for `elem` to appear. 
    */ 
    waitForElementToBeDisplayed: function (elem, customTimeout) { 
     var timeout = customTimeout || 10000; 
     browser.wait(function() { 
      return elem.isDisplayed().then(
       function (visible) { 
        return visible; 
       }, 
       function() { 
        return false; 
       } 
      ); 
     }, timeout, 'failed to find element ' + elem.locator()); 
    }, 

    /** 
    * Block until an element is present on the page. 
    * 
    * @param {Object} elem 
    * The element to watch for. 
    * 
    * @param {Number} [customTimeout=10000] 
    * How long to wait for 'elem' to appear. 
    */ 
    waitForElementToBePresent: function (elem, customTimeout) { 
     var timeout = customTimeout || 10000; 
     browser.wait(function() { 
      return elem.isPresent().then(
       function (visible) { 
        return visible; 
       }, 
       function() { 
        return false; 
       } 
      ); 
     }, timeout, 'Element not present: ' + elem.locator()); 
    } 
}