从另一个模块调用节点模块,而无需在任何地方使用 require() [英] Call a node module from another module without using require() everywhere

查看:28
本文介绍了从另一个模块调用节点模块,而无需在任何地方使用 require()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文件结构

01.spec.js - - - 我从量角器规范中调用助手,这很好

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 - - - 我必须在每个函数中都需要 helper

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.

推荐答案

这里是我们如何使用帮助器和页面对象的简短示例.我们有一个名为 helper.js 的文件,我们所有的页面对象都在 pages 文件夹中.我们正在使用 require-all 节点模块来帮助我们包含所有页面对象.

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;

这篇关于从另一个模块调用节点模块,而无需在任何地方使用 require()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆