2017-04-21 24 views
0

在我的守夜測試中,每個運行前都有一個運行並從Facebook中刪除應用程序。如果應用程序不存在,我只想停止之前的測試。如果在NightwatchJS中不存在元素如何在沒有錯誤的情況下停止測試

.waitForElementPresent('#root div.touchable a', 2000, false, function (result) { 
    if (result.value === false) { 
    browser.saveScreenshot('./test/e2e/img/element-not-there.png') 
    browser.end() 
    } 
}) 
.doSomethingIfElementIsThere() // not real ;) 

即使設置了false參數,這似乎也不起作用。我得到這個錯誤:

ERROR: Unable to locate element: "#root div.touchable a" using: css selector 
    at Object.before (/Users/user/projects/project/test/e2e/specs/testy-mc-test-face.js:30:8) 
✖ Timed out while waiting for element <#root div.touchable a> to be present for 2500 milliseconds. - expected "found" but got: "not found" 
    at Object.before (/Users/user/projects/project/test/e2e/specs/testy-mc-test-face.js:30:8) 

只是想知道如何檢查的東西,而不是錯誤。或者如果元素存在,繼續進行斷言?

的Merci桶

+0

是不是元素缺少測試失敗條件的事實?它應該是你正在測試的代碼的工作,以便優雅地處理這個問題,以便測試可以與它進行交互,而不是測試有責任選擇性地不運行測試。 – Claies

回答

3

我查了code of the elementPresent assertion

this.command = function(callback) { 
    return this.api.elements(this.client.locateStrategy, selector, callback); 
}; 

this.value = function(result) { 
    return (result.status !== 0 || result.value.length === 0) ? 'not present' : 'present'; 
}; 

它採用了low-level api,你可以用它來測試元素是否存在:

.elements()

Search for multiple elements on the page, starting from the document root. The located elements will be returned as WebElement JSON objects. First argument to be passed is the locator strategy, which is detailed on the WebDriver docs.

我想該解決方案可看起來像這樣:

browser.elements('css selector', '#root div.touchable a', result => { 
    const isPresent = result.status === 0 && result.value.length > 0; 
    // doStuff 
}); 
+0

非常感謝,工作就像一個魅力。讓我意識到我應該更多地跳入源代碼。再次感謝 –

+0

我想你可以驗證我的答案:-) – ghusse

相關問題