簡體   English   中英

對相對路徑使用 require

[英]Using require with relative paths

我們對 Protractor 進行了大量的端到端測試。 我們遵循頁面對象模式,這有助於我們保持測試干凈和模塊化。 我們還有一組幫助函數來幫助我們遵循DRY 原則

問題:

單個規范可能需要多個頁面對象和輔助模塊。 例如:

"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();
    });

    // ...

});

您可以看到我們在每個 require 語句中都有該目錄遍歷: ./../.. 這是因為我們有一個specs目錄,我們將 specs 和多個目錄保存在里面,按測試中的應用程序功能分組。

問題:

在量角器中處理相對路徑問題的規范方法是什么?

換句話說,我們希望避免遍歷樹,向上導入模塊。 相反,從基本應用程序目錄下下來會更干凈。


嘗試和想法:

有一篇關於解決這個問題的很棒的文章: Better local require() paths for Node.js ,但我不確定在使用 Protractor 開發測試時哪些選項是推薦的。

我們也嘗試使用require.main來構建路徑,但它指向node_modules/protractor目錄而不是我們的應用程序目錄。

我遇到了同樣的問題,最終得到了以下解決方案。 在我的Protractor配置文件中,我有一個變量,用於存儲 e2e 測試的基本文件夾的路徑。 此外,量角器配置提供了onPrepare回調,在那里你可以使用一個變量叫global ,為你的測試創建全局變量。 您將它們定義為該global變量的屬性,並使用與在測試中使用全局browserelement相同的方式。 我用它來創建自定義全局要求函數來加載不同類型的實體:

// __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');
        };

    }

};

然后您可以立即在測試文件中使用這些全局實用程序方法:

"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();
    });

    // ...

});

我們一直面臨同樣的問題,並決定將所有頁面對象和幫助文件轉換為節點包。 在測試中要求它們現在就像var Header = require('header-po') 轉換為包的另一個好處是您可以使用正確的版本控制。

這是一個簡單的例子:

./page-objects/header-po/index.js

//page-objects/header-po/index.js

'use strict';

var Header = function () {
    this.goHome = function () {
        $('#logo a').click();
    };
  };

module.exports = Header;

./page-objects/header-po/package.json

{
    "name": "header-po",
    "version": "0.1.1",
    "description": "Header page object",
    "main": "index.js",
    "dependencies": {}
}

./package.json

{
    "name": "e2e-test-framework",
    "version": "0.1.0",
    "description": "Test framework",
    "dependencies": {
        "jasmine": "^2.1.1",
        "header-po": "./page-objects/header-po/",
    }
}

./tests/header-test.js

'use strict';

var Header = require('header-po');

var header = new Header();

describe('Header Test', function () {
    it('clicking logo in header bar should open homepage', function () {
        browser.get(browser.baseUrl + '/testpage');
        header.goHome();
        expect(browser.getCurrentUrl()).toBe(browser.baseUrl);
    });
});

我有同樣的問題。 對 Michael Radionov 做了類似的解決方案,但不是設置全局函數,而是設置量角器本身的屬性。

量角器配置文件

onPrepare: function() {
  protractor.basePath = __dirname;
}

測試-e2e.js

require(protractor.basePath+'/helpers.js');

describe('test', function() {
   .......
});

我認為我們在我工作的地方使用的方法對您來說可能是一個很好的解決方案。 我已經發布了一個關於我們如何處理所有事情的簡短示例。 這是非常好的 b/c 你可以在任何規范文件中調用頁面對象函數,你不需要在規范中使用 require 。

從另一個模塊調用節點模塊,而無需在任何地方使用 require()

所有的答案似乎更多的是解決方法

實際的工作解決方案是這樣的:

    "_moduleAliases": {
        "@protractor": "protractor/_protractor",
        "@tmp": "protractor/.tmp_files",
        "@test_data": "protractor/.tmp_files/test_data",
        "@custom_implementation": "protractor/custom_implementation",
    },
  • 將此添加為量角器配置的第一行
require('module-alias/register');
  • 像這樣在項目的任何地方使用它
const params = require('@test_data/parameters');
const customImplementation require('@custom_implementation')
// etc

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM