Mpasw
Mpasw

Reputation: 25

Protractor Function Helper generate same thing

I have following files:

protractor-functions.js

function generateName() {
var names = ["Jewel", "Cesar", "Gita", "Denver", "Necole", "Oscar"];
  return names[Math.floor(Math.random()*names.length)];
}

module.exports = {
  generateName: generateName()
};

SignupTest.js

var functions = require('../protractor-helpers/protractor-functions.js');
global.name1 = functions.generateName;
global.name2 = functions.generateName;

I get same name. If i move that function generateName() to the SignupTest.js it works perfectly fine.

I tried google, asking on protractor IRC but nothing really helpful. How can this be "fixed" to work like when I have function in test file?

Thanks.

Upvotes: 1

Views: 51

Answers (1)

madox2
madox2

Reputation: 51881

It is because you are not exporting function, but the generated value:

module.exports = {
  generateName: generateName() // you are calling function here
};

change it to this:

module.exports = {
  generateName: generateName
};

then you can call it like:

global.name1 = functions.generateName();
global.name2 = functions.generateName(); // it's now different

Upvotes: 4

Related Questions