量角器+黄瓜-如果断言失败,测试执行会突然停止 [英] Protractor + cucumber - if assertion fails test execution stops abruptly
问题描述
- 黄瓜测试方案
@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
只要chai /'Chai as promise'断言失败,我的测试执行就会突然停止,而不是使相应的黄瓜步骤失败。如果一个方案有5个黄瓜DSL步骤,并且在第二步测试执行中断言失败,我希望测试结果应该是
Hi, Whenever chai/'Chai as promise' assertion fails my test execution stops abruptly, instead of making the corresponding cucumber step fail. If a scenario has 5 cucumber DSL step and if assertion fails in 2nd step test execution I expect test result should be
- 1个方案(1个失败)
- 5个步骤(1个失败,3个跳过,1个通过)
但是我明白了测试结果如下,错误代码为199
But I get test result like below with error code 199
- 步骤定义
- Step Definition
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
请帮助我获得正确的测试结果,例如
Please help me to get proper test result like
- 1个场景(1个失败)
- 5步骤(1个失败,3个跳过,1个通过)
推荐答案
我认为我看到了问题,似乎您没有实现 browser.wait()
正确的方式。根据文档,它应该由以下内容组成:
I think I see the problem, it looks like you are not implementing the browser.wait()
in a correct way. According to the docs it should consist out of a:
- condition :要等待的条件,定义为承诺,条件对象或要作为条件求值的函数。
- opt_timeout :等待条件变为真的时间。
- condition: The condition to wait on, defined as a promise, condition object, or a function to evaluate as a condition.
- opt_timeout: How long to wait for the condition to be true.
您的代码是这个
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()
;
Keep in mind that isPresent()
checks if the element is present in the DOM, isDisplayed()
checks if the element is present in the DOM AND visible. If you do a check on isDisplayed()
you need to do the catch()
;
希望这会有所帮助。
这篇关于量角器+黄瓜-如果断言失败,测试执行会突然停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!