SomeshKumar N
SomeshKumar N

Reputation: 91

Not able to refer a function written in other js file

When I am using the below code I am getting an error.

Tool Used: protractor
framework: jasmine
IDE: Webstorm

//Page object file - newPage.js
newPage = function(){
    function clickCandidate(){
        //All your code that you want to call from the test spec
    });
};
module.exports = new newPage();

//Test Spec file - test.js
var newPage = require('./newPage.js'); //Write the location of your javascript file
it('Click on candidate status Screened', function () {
    //Call the function
    newPage.clickCandidate();
});

I am getting error that:

Error: Cannot find module 'scenarios/newPage.js'

Upvotes: 1

Views: 32

Answers (1)

Gunderson
Gunderson

Reputation: 3268

Well first, of course make sure that require path is correct and relative to the file you are running.

Other than that, just a few things that catch my attention:

  1. Add var before newPage = function()... (I think this is the source of your issue)
  2. Also try using either this or Prototypal inheritance

So it could look either like this:

var newPage = function () {
    this.clickCandidate(){
        //All your code that you want to call from the test spec
    });
};
module.exports = new newPage();

OR

var newPage = function() {};

newPage.prototype.clickCandidate = function() {
    // all your code
};

module.exports = new newPage();

Upvotes: 1

Related Questions