使用相对路径要求 [英] Using require with relative paths

查看:163
本文介绍了使用相对路径要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个相当大的一套关于量角器终端到终端的测试。我们下面的页面对象模式,它可以帮助我们保持我们的测试干净和模块化。我们也有一组辅助功能,帮助我们遵循 DRY原则

We have a rather big set of end-to-end tests on Protractor. We are following the Page Object pattern which helps us to keep our tests clean and modular. We also have a set of helper functions which help us to follow the DRY principle.

问题:

一个单一的规范可能需要多个页面对象和助手​​模块。例如:

A single spec may require multiple page objects and helper modules. For instance:

"use strict";

var helpers = require("./../../helpers/helpers.js");
var localStoragePage = require("./../../helpers/localStorage.js");
var sessionStoragePage = require("./../../helpers/sessionStorage.js");

var loginPage = require("./../../po/login.po.js");
var headerPage = require("./../../po/header.po.js");
var queuePage = require("./../../po/queue.po.js");

describe("Login functionality", function () {

    beforeEach(function () {
        browser.get("/#login");

        localStoragePage.clear();
    });

    // ...

});

您可以看到我们有一个目录遍历每个需要的语句: ./../ .. 。这是因为我们有我们保持规格和多个目录内通过应用功能测试分组的规格目录。

You can see that we have that directory traversal in every require statement: ./../... This is because we have a specs directory where we keep the specs and multiple directories inside grouped by application functionality under test.

问题:

什么是接近量角器中的相对路径问题的正规途径?

What is the canonical way to approach the relative path problem in Protractor?

在换句话说,我们想避免遍历树,往上走导入模块。这将是更清洁,从基本应用程序目录,而不是往下走。

In other words, we'd like to avoid traversing the tree, going up to import modules. It would be much cleaner to go down from the base application directory instead.

的尝试和思考:

有大约处理这个问题一个伟大的文章:Node.js的<更好的地方需要()的路径/一>,但我不知道哪个选项是用量角器开发测试时建议之一。

There is a great article about approaching this problem: Better local require() paths for Node.js, but I'm not sure which of the options is a recommended one when developing tests with Protractor.

我们也试着使用 require.main 构建路径,但它指向 node_modules /量角器目录,而不是我们的应用程序目录。

We've also tried to use require.main to construct the path, but it points to the node_modules/protractor directory instead of our application directory.

推荐答案

我有同样的问题,我结束了以下解决方案。
在我的量角器的配置文​​件,我有它存储的端到端我测试基地文件夹的路径的变量。此外,的量角器的配置提供了<一个href=\"https://github.com/angular/protractor/blob/2.1.0/docs/referenceConf.js#L196\"><$c$c>on$p$ppare回调,在那里你可以使用一个名为全球变量为你的测试创建全局变量。您可以定义他们,像全球变量的性质和使用您使用全局变量相同的方式浏览器元素在测试中。我用它来创建自定义的全球需要的功能加载不同类型的实体:

I had the same problem and I ended up with the following solution. In my Protractor config file I have a variable which stores a path to a base folder of my e2e tests. Also, Protractor config provides the onPrepare callback, where you can use a variable called global to create global variables for your tests. You define them as a properties of that global variable and use the same way you use globals browser or element in tests. I've used it to create custom global require functions to load different types of entities:

// __dirname retuns a path of this particular config file
// assuming that protractor.conf.js is in the root of the project
var basePath = __dirname + '/test/e2e/';
// /path/to/project/test/e2e/

exports.config = {

    onPrepare: function () {

        // "relativePath" - path, relative to "basePath" variable

        // If your entity files have suffixes - you can also keep them here
        // not to mention them in test files every time

        global.requirePO = function (relativePath) {
            return require(basePath + 'po/' + relativePath + '.po.js');
        };

        global.requireHelper = function (relativePath) {
            return require(basePath + 'helpers/' + relativePath + '.js');
        };

    }

};

然后你就可以使用测试文件,这些全球性的工具方法的时候了:

And then you can use these global utility methods in your test files right away:

"use strict";    

var localStorageHelper = requireHelper('localStorage');
// /path/to/project/test/e2e/helpers/localStorage.js 

var loginPage = requirePO('login');
// /path/to/project/test/e2e/po/login.po.js

var productShowPage = requirePO('product/show');
// /path/to/project/test/e2e/po/product/show.po.js


describe("Login functionality", function () {

    beforeEach(function () {
        browser.get("/#login");

        localStorageHelper.clear();
    });

    // ...

});

这篇关于使用相对路径要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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