2017-04-21 25 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

相关问题