使用BDD时Expect()不进行比较

时间:2018-08-30 06:48:39

标签: javascript angularjs protractor angular-promise cucumberjs

我正在使用Protractor 5.4.0和黄瓜。

protractor.conf.js文件为:

global.expect = require('chai').expect;
var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);

exports.config = {
    seleniumAddress: 'http://localhost:4444/wd/hub', // This is targetting your local running instance of the selenium webdriver

    specs: [
        '../Features/UI_Tests.feature'
    ],

    capabilities: {
        browserName: 'chrome' // You can use any browser you want. On a CI environment you're going to want to use PhantomJS
    },

    framework: 'custom', //We need this line to use the cucumber framework

    frameworkPath: require.resolve('protractor-cucumber-framework'), // Here it is

    cucumberOpts: {
        //format:  'pretty',
        require: '../Features/step_definitions/my_steps.js', // This is where we'll be writing our actual tests
        // tags: ['@basic'],
        strict: true,
        plugin:"json"
    },
    resultJsonOutputFile:'./testResults.json', //output file path to store the final results in .json format
    params: {
        env: {
            hostname: 'http://0.0.0.0:8000' // Whatever the address of your app is
        }
    }
};

我用一些示例定义了这种情况:

Scenario Outline: dropdown boxes appear and work as expected.
    When go to "URL"
    Then application is running
    When click <box>
    Then <option> is present in <box>

    Examples:
      |box| option|
      |templateSelection| Apparent Energy |
      |templateDeliveryPathSelection| Email |
      |templateLocaleSelection| English |

我正在使用这段代码来检查下拉框的文本是否与选项列相同:

checkDropdown: function (value,dropdown) {
        var text = element(by.id(dropdown)).getText();
        expect(text).to.eventually.equal(value);
    },

它似乎工作正常,因为输出通知所有场景均已通过。但是,如果我们更改“选项”列中的任何值以使其失败,则输出是相同的,所有方案都通过了。为什么?

谢谢。

1 个答案:

答案 0 :(得分:0)

尝试:

checkDropdown: function (value,dropdown) {
        var text = element(by.id(dropdown)).getText();
        return expect(text).to.eventually.equal(value);
}

这是因为您没有返回期望。您始终必须在步骤定义中返回承诺。如果函数返回未定义,则无论如何都会通过。

如果您要使用这两种技术来节省大量的样板工作和工作量,我强烈建议使用https://github.com/canvaspixels/cucumber-protractor。您可以保留现有测试并逐步迁移。

此处有一个演示视频:https://www.youtube.com/watch?v=5vuYL4nxMXs