javascript - 使用 BDD 时 Expect() 不进行比较
问题描述
我正在使用 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);
},
它似乎工作正常,因为输出通知所有场景都已通过。但是,如果我们更改“选项”列中的任何值使其失败,则输出相同,所有场景都通过。为什么?
提前致谢。
解决方案
尝试:
checkDropdown: function (value,dropdown) {
var text = element(by.id(dropdown)).getText();
return expect(text).to.eventually.equal(value);
}
这是因为你没有返回期望。你总是必须在你的步骤定义中返回一个承诺。如果函数返回 undefined 它将通过无论如何。
如果您使用这两种技术来节省大量样板文件和工作量,我强烈建议您使用https://github.com/canvaspixels/cucumber-protractor 。您可以保留现有测试并逐步迁移。
推荐阅读
- sql-server - 视图和使用 SELECT INTO 创建的表之间的主要区别?
- automated-tests - 在 OpenTest 中,如何使用包含十几个测试的单个模板跨多个参与者运行测试?
- python - 在 RGBA PNG 上训练 Tensorflow 2.0?
- visual-studio-code - Visual Studio Code:是否可以将任务添加到 tasks.json 以重新运行上一个任务?
- acumatica - 提交更改时自定义查询屏幕选项卡消失
- git - Git克隆:克隆后增加深度
- reactjs - 无法在 redux 开发工具中查看状态数据
- scala - 尝试在使用细化时保持类型未包装
- loops - FOR/NEXT 带有多个 next 命令
- excel - 如何仅匹配单元格值的一部分而不是 VBA for Excel 中的完全匹配?