2

I'm trying to break my Protractor tests up into manageable files. Can someone tell me what I'm doing wrong with Require?

Here is an example:

Variables: C:/tests/variables/signInVariables.js

var emailAddress = element(by.model('loginData.userName'));
var password = element(by.model('loginData.password'));
var signInButton = element(by.css('[data-auto-field="SignIn"]'));

Functions: C:/tests/functions/signInFunctions.js

var signInVariables = require ('../variables/signInVariables.js');

function signIn(a, b) {
    browser.get ('https://www.website.com');
    emailAddress.sendKeys(a);
    password.sendKeys(b);
    signInButton.click();
};

Tests: C:/tests/protractor/conf.js

var signInFunctions = require ('../functions/signInFunctions.js');

it(' should sign in ', function() {
    signIn("someusername", "somepassword");
});

I run it and this is what I get:

Failed: signIn is not defined

I'm sure this is a simple fix. I just don't really know what I'm doing.

Defpotec2020
  • 257
  • 2
  • 18
  • I'm sure some of this logic doesn't make sense. I'm looking at it now and seeing things I've screwed up. But is the way I'm doing requires right or wrong? – Defpotec2020 Oct 20 '15 at 13:30

2 Answers2

3

you can read more about require and modules here, your mistake is that signInVariables is not a module, there is no exports object to use. you could do:

exports.emailAddress = element(by.model('loginData.userName'));
exports.password = element(by.model('loginData.password'));
exports.signInButton = element(by.css('[data-auto-field="SignIn"]'));

or define signInVariables as a module and export it as whole. hope i helped.

vrachlin
  • 817
  • 5
  • 15
2

require doesn't quite work how you are expecting. It does not simply inline the required file the way you are using it. require is looking for either a JSON file, or a module.exports object. Here is a simple example.

require_me.js

module.exports = {a: "foo", b: "bar"};

index.js

var imports = require('require_me.js');
console.log(imports); // Will produce {a: "foo", b: "bar"}
Brian Glaz
  • 15,468
  • 4
  • 37
  • 55
  • 2
    Based on answers by @vrachlin and Brian Glaz, I read about require and modules, threw a module.exports = { }; around the variables and functions in my separate files, and reformatted my equal signs to colons and my semicolons to commas. Now it's working. Thanks! – Defpotec2020 Oct 20 '15 at 14:54