最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to pass integer values in cucumber test and verify the result - Stack Overflow

programmeradmin0浏览0评论

How do I call a simple addition function and assert the result of two values using selenium-cucumber-js framework with a test written below. While running the below it says TypeError: TypeError: Cannot read property 'addvalues' of undefined at 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

// Here is my 'addvalues-steps.js' file

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);     
    })
};

// Following is my '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;

How do I call a simple addition function and assert the result of two values using selenium-cucumber-js framework with a test written below. While running the below it says TypeError: TypeError: Cannot read property 'addvalues' of undefined at 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

// Here is my 'addvalues-steps.js' file

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);     
    })
};

// Following is my '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;

Share Improve this question edited Nov 14, 2018 at 3:50 shkaper 5,0181 gold badge29 silver badges40 bronze badges asked Nov 13, 2018 at 3:39 soccerwaysoccerway 12k23 gold badges81 silver badges160 bronze badges 12
  • Can you show where you define page that you're accessing as this.page? Probably in your World object? – shkaper Commented Nov 13, 2018 at 3:51
  • The World.js file sits under the support folder... const { setWorldConstructor } = require('cucumber') class page { constructor() { this.variable = 0 } addValues(x,y) { this.variable = x this.variable = y } } setWorldConstructor(page) – soccerway Commented Nov 13, 2018 at 4:30
  • one more thing: can you add your project structure and the mand you're using to run cucumber? – shkaper Commented Nov 14, 2018 at 2:43
  • From windows mand prompt : C:\Tests\cucumber>npm test – soccerway Commented Nov 14, 2018 at 2:50
  • if it's npm test, what's in your package.json then? – shkaper Commented Nov 14, 2018 at 2:51
 |  Show 7 more ments

1 Answer 1

Reset to default 3

Note: the below example is for an old version of cucumber-js: 1.3.3. With cucumber.js, when you're referencing this from inside step definitions, you're actually referencing the World context. So, for this.page.addvalues.addValues(x,y); to work properly, you'll first need to create page that has a reference to your addvalues.js. Something along these lines:

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 ;
    }
};

There's also a couple of things to correct in your steps.js.

  1. Don't pass arrow functions into the steps, as this will remove the this context that you're setting in World.js.
  2. If you want to share variables between steps (as you do in your example), you need to store them somewhere. One such place, again, would be the World context. Note how in my version I set this.prevResult
  3. When the variables are injected into your steps, they are injected as strings. Note the parseInt() in my version.

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: It turns out that the question is about selenium-cucumber-js, which is a framework on top of cucumber-js. Disregard the ments about the world.js.

According to selenium-cucumber-js docs, you don't need this to access the page objects in your step definitions:

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));
    })
}
发布评论

评论列表(0)

  1. 暂无评论