File Structure
01.spec.js - - - I call helpers from the protractor specs which is fine
describe('should click on element', function () {
var helper1 = require('../../modules/helpers/helper1.js');
it('should click and assert...', function() {
helper1.clickOnCircle('Accounts');
});
});
...But to use any helper functions from another helper file...
helpers1.js - - - I have to require the helper in every function
module.exports = {
clickOnCircle: clickOnCircle,
clickOnBox : clickOnBox
};
var helper2 = require('./helper2.js'); //node require doesn't hit something like this
function clickOnCircle(circleText) {
var helper2 = require('./helper2.js'); //needed in every function
helper2.doSomething(circleText);
}
function clickOnBox(boxText) {
var helper2 = require('./helper2.js'); //needed in every function
helper2.doSomething(boxText);
}
It's almost like I want the helper files to be available globally. I've messed around using configuration parameters but I still end up having to require the helper from each function.
here is a brief example of how we use our helpers and page objects. We have a file called helper.js and all of our page objects are in the pages folder. We are using the require-all node module to help us include all our page objects.
// Require all application pages
global.pages = require('require-all')(__dirname + '/../pages/');
global.EC = protractor.ExpectedConditions;
// Instantiate all pages in our applications, and make them globally available to the tests
//***** User/Nav Pages *****
global.loginPage = new pages.Navigation.LoginHomePage();
global.instructorHomePage = new pages.Navigation.InstructorHomePage();
global.studentHomePage = new pages.Navigation.StudentHomePage();
global.studentAccessPage = new pages.Misc.StudentAccessPage();
global.selfEnrollPage = new pages.User.SelfEnrollPage();
global.instructorNavPanelPage = new pages.Navigation.InstructorNavPanelPage();
global.studentNavPanelPage = new pages.Navigation.StudentNavPanelPage();
global.createInstructorPage = new pages.Admin.CreateInstructorPage();
global.addUserPage = new pages.User.AddUserPage();
then this is a template of how we would have one of those pages
var TemplatePage = function() {
//***** Buttons & Links *****
this.link = element(by.linkText('link'));
this.link2 = element(by.linkText('link2'));
//***** Input Fields *****
//***** Drop-Downs *****
//***** Check Boxes *****
//***** Page Elements *****
//***** Helpers *****
this.clickLink = function() {
return link.click(); };
};
module.exports = TemplatePage;
You can load other modules just once at the beginning of your module and then reference them anywhere from within that module.
This should work just fine:
// require in any other modules we need here
// modules are cached by the system
var helper2 = require('./helper2.js');
function clickOnCircle(circleText) {
helper2.doSomething(circleText);
}
function clickOnBox(boxText) {
helper2.doSomething(boxText);
}
module.exports = {
clickOnCircle: clickOnCircle,
clickOnBox : clickOnBox
};
What about using inheritance,
for example:
function shapeModel() {
var helper = require('./helper2.js');
function clickOnCircle() {
return helper.doSomething(circleText);
}
function clickOnBox() {
return helper.doSomething(boxText);
}
return {
clickOnCircle: clickOnCircle(),
clickOnBox : clickOnBox()
}
}
module.exports = shapeModel
This also makes a class for you to use in the module.
This places the exports
at the bottom of the page allowing the helper
variable to stay scoped as global instead of scoped outside your module.exports