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

时间:2017-04-11 03:26:04

标签: selenium protractor cucumber

  • 黄瓜测试场景



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

  • 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
&#13;
&#13;
&#13;

请帮助我获得正确的测试结果,如

  • 1个场景(1个失败)
  • 5个步骤(1个失败,3个跳过,1个过去)

1 个答案:

答案 0 :(得分:1)

我认为我发现了问题,看起来您没有以正确的方式实施browser.wait()。根据文档,它应该包括:

  1. 条件:等待的条件,定义为承诺,条件对象或要评估为条件的函数。
  2. opt_timeout :等待条件成立的时间。
  3. 您的代码就是这个

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

    希望这有帮助。