Call node module from another module without using require () everywhere

File structure

enter image description here

01.spec.js - - - I call assistants from the protractor specification, which is good

describe('should click on element', function () { var helper1 = require('../../modules/helpers/helper1.js'); it('should click and assert...', function() { helper1.clickOnCircle('Accounts'); }); }); 

... but use any helper functions from another helper file ...

helpers1.js - - - I have to require an assistant 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 as if I want the supporting files to be available worldwide. I was messing around using configuration options, but I still ended up requiring an assistant from each function

+1
javascript protractor
Jul 24 '15 at 13:50
source share
3 answers

here is a brief example of how we use our helpers and page objects. We have a helper.js file, and all our page objects are in a folder with folders. We use the require-all node module to help us include all of 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 for how we will have one of these 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; 
0
Jul 24 '15 at 16:33
source share

You can load other modules only once at the beginning of your module, and then refer to them anywhere inside this module.

This should work 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 }; 
+1
Jul 24 '15 at 14:11
source share

How about using inheritance,

eg:

 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 

It also makes a class for you in the module. This puts exports at the bottom of the page, allowing the helper variable to remain the scope as global, rather than the scope outside of your module.exports

0
Jul 24 '15 at 14:16
source share



All Articles