量角器+黄瓜 - 如果断言失败,测试执行突然停止
- 黄瓜测试方案
@login
Scenario: Test signin link
Given the user goes to "example.com"
When the user clicks on login button
Then the current page is the login page
嗨,每当柴/“柴作为承诺”断言失败我测试停止执行突然,而不是使相应的黄瓜步骤失败。如果方案有5个黄瓜DSL一步,如果断言在第2步测试执行失败,我希望测试结果应该是
- 1方案(1失败)
- 5个步骤(1失败,3跳过1后)
,但我得到的测试结果如下图所示,错误代码199
- 步骤定义
this.When(/^the user clicks on login button$/, function() { browser.ignoreSynchronization = false; return browser.wait(wagHomePage.elements.signIn.isDisplayed().then(function(visible) { if (visible) { wagHomePage.elements.signIn.click().then(function() { expect(visible).to.be.true; }); } else { chai.assert.isTrue(false); } })); }); this.Then(/^the current page is the login page$/, function() { expect(wagLoginPage.elements.pageIdentifier.isDisplayed()).to.eventually.be.true; });
@login
Scenario: Test signin link
√ Given the user goes to "example.com"
[19:58:02] E/launcher - expected false to be true
[19:58:02] E/launcher - AssertionError: expected false to be true
at doAsserterAsyncAndAddThen (C:\JS_UIAutomation\node
_modules\chai-as-promised\lib\chai-as-promised.js:293:29)
at .<anonymous> (C:\JS_UIAutomation\node_modules\chai
-as-promised\lib\chai-as-promised.js:283:21)
at get (C:\JS_UIAutomation\node_modules\chai\lib\chai
\utils\overwriteProperty.js:50:37)
at Function.assert.isTrue (C:\JS_UIAutomation\node_mo
dules\chai\lib\chai\interface\assert.js:332:31)
at C:\JS_UIAutomation\example_site_tests\step_defin
itions\wagLogin_definition.js:23:29
at elementArrayFinder_.then (C:\Users\username\AppData\Roaming\npm\node_modules\protractor\lib\element.ts:840:
22)
at ManagedPromise.invokeCallback_ (C:\Users\username\AppData\Roaming\npm\node_modules\protractor\node_modules\
selenium-webdriver\lib\promise.js:1366:14)
at TaskQueue.execute_ (C:\Users\username\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-web
driver\lib\promise.js:2970:14)
at TaskQueue.executeNext_ (C:\Users\username\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium
-webdriver\lib\promise.js:2953:27)
at asyncRun (C:\Users\username\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib
\promise.js:2813:27)
[19:58:02] E/launcher - Process exited with error code 199
请帮我得到正确的测试结果类似
- 1方案(1失败)
- 5个步骤(1失败,3跳过,1通过)
我想我看到了问题,看起来您没有正确实施browser.wait()
。根据该文件,应该由出来的:
- 条件:条件上等待,定义为一个承诺,条件对象,或作为评估条件的功能。
- opt_timeout:等待条件成立的时间需要多长时间。
你的代码是这样
return browser.wait(wagHomePage.elements.signIn.isDisplayed().then(function(visible) {
if (visible) {
wagHomePage.elements.signIn.click().then(function() {
expect(visible).to.be.true;
});
}
else {
chai.assert.isTrue(false);
}
}));
它应该更像这个
// Wait 3 seconds for the element to appear and click on it
// If not the wait wail fail by rejecting the promise with the custom message
return browser.wait(function(){
return wagHomePage.elements.signIn.isDisplayed()
.then(function(visible){
if (visible) {
// click on the element
wagHomePage.elements.signIn.click();
return true;
}
// Not visible yet, but it is in the DOM, then try again
return false;
}).catch(function(notFound){
// Element not found in the DOM, try again
return false;
});
}, 3000, 'Element not found within 3 seconds');
记住isPresent()
检查该元素存在于DOM,isDisplayed()
检查元素存在于DOM中并且可见。如果您在isDisplayed()
上进行检查,则需要执行catch()
;
希望这会有所帮助。
非常感谢你:)。这解决了我的问题。现在,如果该元素不可见,则会抛出错误消息“在3秒内未找到元素”,并使相应的DSL步骤失败并继续执行下一个测试用例。测试执行没有突然停止。谢谢 !!! :) –
@ThangakumarD,不客气。是[这](http://stackoverflow.com/questions/43333350/protractor-cucumber-element-not-visible-even-though-element-is-visible/43333484?noredirect=1#comment73767697_43333484)也相关,如果所以,你能链接到这个答案?然后圆圈再次变圆;-) – wswebcreation
是的。两者都有关系。我复制了一个链接[here](http://stackoverflow.com/questions/43333350/protractor-cucumber-element-not-visible-even-though-element-is-visible/43333484?noredirect=1#comment73775395_43333484)。这够了吗 ?或者是以任何其他方式链接答案? –
我没有得到这段代码'return browser.wait(wagHomePage.elements.signIn.isDisplayed()。然后(函数(可视){ 如果(可视){ wagHomePage.elements.signIn.click(),然后(函数(){ 预期(可见).to.be.true; });} 其他 { chai.assert.isTrue(false); } }));' 您首先等待显示元素,如果它是可见的,您点击某个东西(解析为承诺),然后在预期中使用以前的结果。你能解释一下吗? – wswebcreation
有时候,点击操作甚至无法执行 - 虽然元素是可见的。所以为了确保执行点击操作,我添加了一个promise期望(可见).to.be.true;但我可以删除它。不管这条线我面对这个问题。请帮助 –
失败的确切代码行是什么,它在日志中显示'C:\ JS_UIAutomation \ example_site_tests \ step_definitions \ wagLogin_definition.js:23:29'。你能添加一个字符串到你希望的期望像这样'expect(visible).to.equal(true,'Descriptive message:');' – wswebcreation