Protractor + cucumber - if assertion fails test ex

2019-09-12 23:33发布

  • Cucumber Test Scenario

@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

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 scenario (1 failed)
  • 5 steps (1 failed, 3 skipped, 1 passed)

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 scenario (1 failed)
  • 5 steps (1 failed, 3 skipped, 1 passed)

1条回答
闹够了就滚
2楼-- · 2019-09-13 00:17

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:

  1. condition: The condition to wait on, defined as a promise, condition object, or a function to evaluate as a condition.
  2. opt_timeout: How long to wait for the condition to be true.

Your code is this

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);
    }
}));

It should be more like this

// 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');

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();

Hope this helps.

查看更多
登录 后发表回答