【问题标题】:Cucumber-Protractor tests skipping over assertions, falsely passingCucumber-Protractor 测试跳过断言,错误地通过
【发布时间】:2018-09-07 04:32:39
【问题描述】:

所以我是 Javascript 的新手,在此之前我唯一的语言是 Ruby。多年来,我已经使用 Cucumber 和 Ruby 编写 API 测试,但现在我正在尝试使用 Protractor 和 Cucumber.js 找出 Angular 应用程序的 UI 测试。我已经设置了框架并且运行的测试步骤通过了,但是错误地如此。

这是我的步骤定义的一个 sn-p,其中进行了一些编辑以更改断言中的数据,并且断言的字符串故意错误以触发失败。他们运行并且正在通过,但这只是因为它忽略了断言。我没有看到它实际上在浏览器中做任何事情,但是如果我输入 console.log 消息,我确实会在控制台中看到它们。但是,如果我注释掉最后一个回调,那么我可以看到它在浏览器中运行,它实际上会检查断言并失败。

Cucumber 不需要回调,删除它们会导致它以完全相同的方式运行...只是我当然不能注释掉回调并观察它像我上面提到的那样运行。

如果我没有在第一步设置超时,那么整个事情在第一步都会出错,并显示“错误:函数在 5000 毫秒后超时”

为什么?!?谢谢!!

带有 Cucumber 4.0.0 和 protractor-cucumber-framework 4.2.0 的量角器 5.3.0

Given('I am on the home page', {timeout: 30000}, (callback) => {
    browser.waitForAngularEnabled(false);
    browser.get(browser.params.env.int).then(callback);
});

Then('the log in form is displayed', callback => {
    expect(element(by.id('email')).isPresent()).to.eventually.be.true;
    expect(element(by.id('password')).isPresent()).to.eventually.be.true;
    callback();
});

When('I enter my user name', callback => {
    element(by.name('email')).sendKeys('my_addy@example.com');
    expect(element(by.id('email')).getAttribute('value')).to.eventually.equal('something that does match');
    callback();
});

When('I enter my password', callback => {
    element(by.name('password')).sendKeys('blah');
    callback();
});

When('I click the log in button', callback => {
    element(by.buttonText('Log In')).click();
    callback();
});

Then('I am on the X page', callback => {
    expect(browser.getCurrentUrl()).to.eventually.contains('Y');
    // callback();
});

【问题讨论】:

    标签: javascript testing automation protractor cucumber


    【解决方案1】:

    1) 针对问题:“错误:函数在 5000 毫秒后超时

    这是由于您的步骤定义函数执行时间超过了默认超时时间:5 秒。

    您可以通过关注my another post 来全局更改此时间,或者像您一样 仅在需要的步骤定义函数上单独添加超时。

    2) 对于 Cucumber 回调问题, 您可以选择使用回调,或者选择在每个步骤定义函数中可能返回一个promise。我更喜欢后一种方法。

    var { Given, Then, When } = require("cucumber");
    
    Given(/^open cucumberjs github page$/, ()=> {
        browser.get("https://github.com/cucumber/cucumber-js");
        return expect(browser.getTitle()).to.eventually
              .equal("cucumber/cucumber-js: Cucumber for JavaScript");
    }); 
    
    When('I enter my password', ()=> {
        return element(by.name('password')).sendKeys('blah');
    });
    
    When('I click the log in button', ()=> {
        return element(by.buttonText('Log In')).click();
    
    });
    

    更多代码示例,可以从我的示例项目here中找到

    【讨论】:

      猜你喜欢
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-22
      • 1970-01-01
      • 2011-04-14
      相关资源
      最近更新 更多