<tfoot id='XNqTZ'></tfoot>
      • <bdo id='XNqTZ'></bdo><ul id='XNqTZ'></ul>
    1. <legend id='XNqTZ'><style id='XNqTZ'><dir id='XNqTZ'><q id='XNqTZ'></q></dir></style></legend>

      <small id='XNqTZ'></small><noframes id='XNqTZ'>

    2. <i id='XNqTZ'><tr id='XNqTZ'><dt id='XNqTZ'><q id='XNqTZ'><span id='XNqTZ'><b id='XNqTZ'><form id='XNqTZ'><ins id='XNqTZ'></ins><ul id='XNqTZ'></ul><sub id='XNqTZ'></sub></form><legend id='XNqTZ'></legend><bdo id='XNqTZ'><pre id='XNqTZ'><center id='XNqTZ'></center></pre></bdo></b><th id='XNqTZ'></th></span></q></dt></tr></i><div id='XNqTZ'><tfoot id='XNqTZ'></tfoot><dl id='XNqTZ'><fieldset id='XNqTZ'></fieldset></dl></div>

        将 require 与相对路径一起使用

        时间:2023-06-15

          <tfoot id='82RB7'></tfoot>

        1. <legend id='82RB7'><style id='82RB7'><dir id='82RB7'><q id='82RB7'></q></dir></style></legend>
                <tbody id='82RB7'></tbody>
                <i id='82RB7'><tr id='82RB7'><dt id='82RB7'><q id='82RB7'><span id='82RB7'><b id='82RB7'><form id='82RB7'><ins id='82RB7'></ins><ul id='82RB7'></ul><sub id='82RB7'></sub></form><legend id='82RB7'></legend><bdo id='82RB7'><pre id='82RB7'><center id='82RB7'></center></pre></bdo></b><th id='82RB7'></th></span></q></dt></tr></i><div id='82RB7'><tfoot id='82RB7'></tfoot><dl id='82RB7'><fieldset id='82RB7'></fieldset></dl></div>
                  <bdo id='82RB7'></bdo><ul id='82RB7'></ul>

                  <small id='82RB7'></small><noframes id='82RB7'>

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

                  问题描述

                  我们在 Protractor 上进行了相当多的端到端测试.我们遵循页面对象模式,这有助于我们保持测试的清洁和模块化.我们还有一组辅助函数来帮助我们遵循 DRY 原则.p>

                  问题:

                  单个规范可能需要多个页面对象和帮助模块.例如:

                  "使用严格";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("登录功能", function () {之前(函数(){browser.get("/#login");localStoragePage.clear();});//...});

                  您可以看到我们在每个 require 语句中都有该目录遍历:./../...这是因为我们有一个 specs 目录,我们将规范和多个目录保存在其中,这些目录按正在测试的应用程序功能分组.

                  问题:

                  在 Protractor 中解决相对路径问题的规范方法是什么?

                  换句话说,我们希望避免遍历树,向上导入模块.而是从基本应用程序目录中下去会更干净.

                  <小时>

                  尝试和想法:

                  关于解决这个问题有一篇很棒的文章:更好的 Node.js 本地 require() 路径,但我不确定在使用 Protractor 开发测试时推荐使用哪个选项.

                  我们也尝试过使用 require.main 来构造路径,但它指向 node_modules/protractor 目录而不是我们的应用程序目录.

                  解决方案

                  我遇到了同样的问题,我最终得到了以下解决方案.在我的 Protractor 配置文件中,我有一个变量,它存储了我的 e2e 测试的基本文件夹的路径.此外,Protractor 配置提供了 onPrepare 回调,您可以在其中使用名为 global 的变量为您的测试创建全局变量.您将它们定义为该 global 变量的属性,并使用与在测试中使用全局 browserelement 相同的方式.我用它来创建自定义全局 require 函数来加载不同类型的实体:

                  //__dirname 重新调整此特定配置文件的路径//假设 protractor.conf.js 在项目的根目录下var basePath = __dirname + '/test/e2e/';///path/to/project/test/e2e/出口.config = {onPrepare: 函数 () {//relativePath" - 相对于basePath"变量的路径//如果你的实体文件有后缀 - 你也可以把它们放在这里//更不用说每次都在测试文件中global.requirePO = function (relativePath) {return require(basePath + 'po/' + relativePath + '.po.js');};global.requireHelper = function (relativePath) {return require(basePath + 'helpers/' + relativePath + '.js');};}};

                  然后您可以立即在测试文件中使用这些全局实用方法:

                  "使用严格";var localStorageHelper = requireHelper('localStorage');///path/to/project/test/e2e/helpers/localStorage.jsvar loginPage = requirePO('登录');///path/to/project/test/e2e/po/login.po.jsvar productShowPage = requirePO('product/show');///path/to/project/test/e2e/po/product/show.po.jsdescribe("登录功能", function () {之前(函数(){browser.get("/#login");localStorageHelper.clear();});//...});

                  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.

                  The Problem:

                  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.

                  The Question:

                  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.


                  Attempts and thoughts:

                  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.

                  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.

                  解决方案

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

                  这篇关于将 require 与相对路径一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Protractor 和 Karma 可以一起使用吗? 下一篇:如何在 WebStorm 中调试角度量角器测试

                  相关文章

                    <bdo id='EHl1S'></bdo><ul id='EHl1S'></ul>
                  <i id='EHl1S'><tr id='EHl1S'><dt id='EHl1S'><q id='EHl1S'><span id='EHl1S'><b id='EHl1S'><form id='EHl1S'><ins id='EHl1S'></ins><ul id='EHl1S'></ul><sub id='EHl1S'></sub></form><legend id='EHl1S'></legend><bdo id='EHl1S'><pre id='EHl1S'><center id='EHl1S'></center></pre></bdo></b><th id='EHl1S'></th></span></q></dt></tr></i><div id='EHl1S'><tfoot id='EHl1S'></tfoot><dl id='EHl1S'><fieldset id='EHl1S'></fieldset></dl></div>
                      <tfoot id='EHl1S'></tfoot>
                    1. <legend id='EHl1S'><style id='EHl1S'><dir id='EHl1S'><q id='EHl1S'></q></dir></style></legend>

                    2. <small id='EHl1S'></small><noframes id='EHl1S'>