如何在黄瓜测试中传递整数值并验证结果
How to pass integer values in cucumber test and verify the result
如何使用 selenium-cucumber-js 框架调用简单的加法函数并断言两个值的结果以及下面编写的测试。虽然下面的 运行 说
类型错误:类型错误:无法读取未定义的 属性 'addvalues'
在 createWorld.When (C:\Tests\cucumber\step-definitions\addvalues-steps.js:5:25)
Feature:
Scenario: Addition of two values
When Add two values 5 and 10
Then I should get result 15
// 这是我的 'addvalues-steps.js' 文件
const expect = require('chai').expect;
module.exports = function () {
this.When(/^Add two values (-?\d+) and (-?\d+)$/, (x, y) =>{
this.page.addvalues.addValues(x,y);
})
this.Then(/^I should get result (-?\d+)$/, (ans) =>{
let tot = this.page.addvalues.addValues(x, y);
expect(tot).to.be.eql(ans);
})
};
// 以下是我的'addvalues.js file'
module.exports = {
addValues(x,y){
var total = x + y ;
return total ;
}
};
// world.js >>
const { CustomWorld } = require('cucumber')
function CustomWorld() {
console.log('overriding the world')
this.page = {
addvalues: require('../page-objects/addvalues')
}
console.log("This is the recent error log:"+this.page.addvalues)
}
module.exports = function() {
this.World = CustomWorld;
注意:下面的示例是针对旧版本的 cucumber-js:1.3.3。
使用 cucumber.js,当您从内部步骤定义中引用 this
时,您实际上是在引用 World 上下文。因此,要使 this.page.addvalues.addValues(x,y);
正常工作,您首先需要创建 page
并引用您的 addvalues.js
。沿着这些线的东西:
world.js:
function CustomWorld() {
console.log('overriding the world')
this.page = {
addvalues: require('../page-objects/addvalues')
}
}
module.exports = function() {
this.World = CustomWorld;
};
addvalues.js:
//addvalues.js
module.exports = {
addValues(x,y){
var total = x + y ;
return total ;
}
};
您的 steps.js
中还有几处需要更正。
- 不要将箭头函数传递到步骤中,因为这将删除您在 World.js 中设置的
this
上下文。
- 如果你想在步骤之间共享变量(就像你在你的例子中所做的那样),你需要将它们存储在某个地方。同样,一个这样的地方就是 World 上下文。请注意我在我的版本中是如何设置
this.prevResult
- 当变量被注入到您的步骤中时,它们被作为字符串注入。请注意我的版本中的
parseInt()
。
addvalues-steps.js:
const expect = require('chai').expect;
module.exports = function() {
this.When(/^Add two values (-?\d+) and (-?\d+)$/, function (x, y) {
this.prevResult = this.page.addvalues.addValues(parseInt(x, 10), parseInt(y, 10));
})
this.Then(/^I should get result (-?\d+)$/, function (ans) {
let tot = this.prevResult;
expect(tot).to.be.eql(parseInt(ans, 10));
})
}
UPD: 原来问题是关于selenium-cucumber-js的,是在cucumber-js
之上的框架。忽略关于 world.js
.
的评论
根据 selenium-cucumber-js
文档,您不需要 this
访问步骤定义中的页面对象:
Page objects are accessible via a global page object and are
automatically loaded from ./page-objects
.
const expect = require('chai').expect;
module.exports = function() {
this.When(/^Add two values (-?\d+) and (-?\d+)$/, function (x, y) {
this.prevResult = page.addvalues.addValues(parseInt(x, 10), parseInt(y, 10));
})
this.Then(/^I should get result (-?\d+)$/, function (ans) {
let tot = this.prevResult;
expect(tot).to.be.eql(parseInt(ans, 10));
})
}
如何使用 selenium-cucumber-js 框架调用简单的加法函数并断言两个值的结果以及下面编写的测试。虽然下面的 运行 说 类型错误:类型错误:无法读取未定义的 属性 'addvalues' 在 createWorld.When (C:\Tests\cucumber\step-definitions\addvalues-steps.js:5:25)
Feature:
Scenario: Addition of two values
When Add two values 5 and 10
Then I should get result 15
// 这是我的 'addvalues-steps.js' 文件
const expect = require('chai').expect;
module.exports = function () {
this.When(/^Add two values (-?\d+) and (-?\d+)$/, (x, y) =>{
this.page.addvalues.addValues(x,y);
})
this.Then(/^I should get result (-?\d+)$/, (ans) =>{
let tot = this.page.addvalues.addValues(x, y);
expect(tot).to.be.eql(ans);
})
};
// 以下是我的'addvalues.js file'
module.exports = {
addValues(x,y){
var total = x + y ;
return total ;
}
};
// world.js >>
const { CustomWorld } = require('cucumber')
function CustomWorld() {
console.log('overriding the world')
this.page = {
addvalues: require('../page-objects/addvalues')
}
console.log("This is the recent error log:"+this.page.addvalues)
}
module.exports = function() {
this.World = CustomWorld;
注意:下面的示例是针对旧版本的 cucumber-js:1.3.3。
使用 cucumber.js,当您从内部步骤定义中引用 this
时,您实际上是在引用 World 上下文。因此,要使 this.page.addvalues.addValues(x,y);
正常工作,您首先需要创建 page
并引用您的 addvalues.js
。沿着这些线的东西:
world.js:
function CustomWorld() {
console.log('overriding the world')
this.page = {
addvalues: require('../page-objects/addvalues')
}
}
module.exports = function() {
this.World = CustomWorld;
};
addvalues.js:
//addvalues.js
module.exports = {
addValues(x,y){
var total = x + y ;
return total ;
}
};
您的 steps.js
中还有几处需要更正。
- 不要将箭头函数传递到步骤中,因为这将删除您在 World.js 中设置的
this
上下文。 - 如果你想在步骤之间共享变量(就像你在你的例子中所做的那样),你需要将它们存储在某个地方。同样,一个这样的地方就是 World 上下文。请注意我在我的版本中是如何设置
this.prevResult
- 当变量被注入到您的步骤中时,它们被作为字符串注入。请注意我的版本中的
parseInt()
。
addvalues-steps.js:
const expect = require('chai').expect;
module.exports = function() {
this.When(/^Add two values (-?\d+) and (-?\d+)$/, function (x, y) {
this.prevResult = this.page.addvalues.addValues(parseInt(x, 10), parseInt(y, 10));
})
this.Then(/^I should get result (-?\d+)$/, function (ans) {
let tot = this.prevResult;
expect(tot).to.be.eql(parseInt(ans, 10));
})
}
UPD: 原来问题是关于selenium-cucumber-js的,是在cucumber-js
之上的框架。忽略关于 world.js
.
根据 selenium-cucumber-js
文档,您不需要 this
访问步骤定义中的页面对象:
Page objects are accessible via a global page object and are automatically loaded from
./page-objects
.
const expect = require('chai').expect;
module.exports = function() {
this.When(/^Add two values (-?\d+) and (-?\d+)$/, function (x, y) {
this.prevResult = page.addvalues.addValues(parseInt(x, 10), parseInt(y, 10));
})
this.Then(/^I should get result (-?\d+)$/, function (ans) {
let tot = this.prevResult;
expect(tot).to.be.eql(parseInt(ans, 10));
})
}