我在从Protractor 2.0升级到Protractor 2.1时遇到了麻烦,并怀疑使用Protractor 2.1和CucumberJS存在问题。当我编辑package.json以降级到Protractor 2.0.0并重新安装节点包时,问题就解决了。什么导致Protractor 2.1.0中的错误?如何重写我的项目以使用Protractor 2.1.0和Cucumber JS?
我的项目文件及其内容如下:
/路径/到/ myproject的/的 conf.js
exports.config = {
sauceUser: process.env.SAUCE_USERNAME,
sauceKey: process.env.SAUCE_ACCESS_KEY,
specs: [
'features/*.feature'
],
multiCapabilities: [
{
'browserName': 'chrome'
}
],
framework: 'cucumber',
cucumberOpts: {
require: 'features/stepDefinitions.js',
format: 'summary'
}
};
/路径/到/ myproject的/ 功能/ demo.feature
Feature: Refund item
Scenario: Jeff returns a faulty microwave
Given Jeff has bought a microwave for $100
And he has a receipt
When he returns the microwave
Then Jeff should be refunded $100
/路径/到/ myproject的/ 功能/ stepDefinitions.js
module.exports = function() {
this.Given(/^Jeff has bought a microwave for \$(\d+)$/, function (arg1, callback) {
// Write code here that turns the phrase above into concrete actions
callback.pending();
});
this.Given(/^he has a receipt$/, function (callback) {
// Write code here that turns the phrase above into concrete actions
callback.pending();
});
this.When(/^he returns the microwave$/, function (callback) {
// Write code here that turns the phrase above into concrete actions
callback.pending();
});
this.Then(/^Jeff should be refunded \$(\d+)$/, function (arg1, callback) {
// Write code here that turns the phrase above into concrete actions
callback.pending();
});
};
/路径/到/ myproject的/的的package.json
{
"name": "strato-remake",
"version": "1.0.0",
"description": "",
"main": "conf.js",
"dependencies": {
},
"devDependencies": {
"cucumber": "0.4.9",
"protractor": "2.1.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
安装节点包并运行Protractor时:
$ npm install
$ $(npm bin)/protractor conf.js
以下错误将打印到终端:
Using SauceLabs selenium server at http://ondemand.saucelabs.com:80/wd/hub
[launcher] Running 1 instances of WebDriver
[launcher] Error: TypeError: Cannot read property 'apply' of undefined
at formatter.handleBeforeFeatureEvent (/path/to/myproject/node_modules/protractor/lib/frameworks/cucumber.js:83:41)
at /path/to/myproject/node_modules/protractor/lib/frameworks/cucumber.js:153:15
at Function.promise (/path/to/myproject/node_modules/protractor/node_modules/q/q.js:650:9)
at /path/to/myproject/node_modules/protractor/lib/frameworks/cucumber.js:147:14
at _fulfilled (/path/to/myproject/node_modules/protractor/node_modules/q/q.js:797:54)
at self.promiseDispatch.done (/path/to/myproject/node_modules/protractor/node_modules/q/q.js:826:30)
at Promise.promise.promiseDispatch (/path/to/myproject/node_modules/protractor/node_modules/q/q.js:759:13)
at /path/to/myproject/node_modules/protractor/node_modules/q/q.js:525:49
at flush (/path/to/myproject/node_modules/protractor/node_modules/q/q.js:108:17)
at process._tickCallback (node.js:355:11)
[launcher] Process exited with error code 100
但是,如果我编辑package.json以降级到Protractor 2.0.0,并再次安装节点包:
"devDependencies": {
"cucumber": "0.4.9",
"protractor": "2.0.0"
},
当我运行Protractor时它会起作用:
$ $(npm bin)/protractor conf.js
Using SauceLabs selenium server at http://ondemand.saucelabs.com:80/wd/hub
[launcher] Running 1 instances of WebDriver
1 scenario (1 pending)
4 steps (1 pending, 3 skipped)
SauceLabs results available at http://saucelabs.com/jobs/8a85b85e7a1c5dd344e694e392ec90c3
[launcher] 0 instance(s) of WebDriver still running
[launcher] chrome #1 passed
如何重写此项目以与Protractor 2.1.0和Cucumber JS兼容?谢谢!
答案 0 :(得分:0)
这是Protractor中的一个错误的结果,由this pull request修复。要使用该特定提交,请将package.json更改为包括:
"devDependencies": {
"cucumber": "0.4.9",
"protractor": "git+https://github.com/angular/protractor.git#0262268fa43b9eefac815d986740efa07bb15818"
},