Mano Kugan
Mano Kugan

Reputation: 219

Not a function Error in Protractor POM

I am new to Protractor and I ve been trying to do a sample project with POM . I have my page objects and functions in spec.js and trying to access it in additionTest.js , but I get

spec.clickGoButton is not a function

My code structure looks like below

spec.js

   var spec=function(){
   this.goButton=element(by.id(goButton));
   this.clickGoButton=function(){
   this.goButton.click();
    };

module.exports= spec;

additionTest.jp

   var spec = require('./spec.js');

   function add(a, b) {
    spec.clickGoButton();
   }

I get error as spec.clickGoButton is not a function

Upvotes: 1

Views: 453

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 370689

Assuming that spec.js is exporting spec properly - it's exporting a class, which is meant to be called with new in the consuming module. You need to create a new instantiation before using its methods:

const Spec = require('./spec.js');
const mySpec = new Spec();
function add(a, b) {
  mySpec.clickGoButton();
}

But do you really need a class? You could just export an object instead in spec.js, and then use it without instantiation:

const spec = {
  goButton: element(by.id(goButton)),
  clickGoButton: function(){
    this.goButton.click();
  },
};

Upvotes: 1

Related Questions