量角器 + 黄瓜 - 如果断言失败,测试执行会突然停止

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 步骤,并且如果断言在第二步测试执行中失败,我希望测试结果应该是

但我得到如下测试结果,错误代码为 199

@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

请帮助我获得像

这样的正确测试结果

我想我看到了问题,您似乎没有以正确的方式实施 browser.wait()。根据文档,它应该包含一个:

  1. 条件:要等待的条件,定义为承诺、条件对象或作为条件求值的函数。
  2. 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();

希望对您有所帮助。