Using query with relative paths

4 answers

I had the same problem and I got the following solution. In my Protractor configuration file, I have a variable that stores the path to the base folder of my e2e tests. In addition, the Protractor configurator provides an onPrepare where you can use a variable called global to create global variables for your tests. You define them as properties of this global variable and use the same as in the tests using the global variables browser or element . I used it to create custom global functions for loading various types of objects:

 // __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 immediately use these methods of the global utility in the test files:

 "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(); }); // ... }); 
+24
Jul 18 '15 at 21:22
source share

We faced the same problem and decided to turn all the page objects and auxiliary files into node packages. Requiring them in tests is now as simple as var Header = require('header-po') . Another benefit of converting to packages is that you can use the correct version control.

Here is a simple example:

./page-objects/heading-ro/index.js

 //page-objects/header-po/index.js 'use strict'; var Header = function () { this.goHome = function () { $('#logo a').click(); }; }; module.exports = Header; 

./page-objects/heading-ro/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/headings 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); }); }); 
+17
Jul 25 '15 at 8:17
source share

I had the same problem. A similar decision was made by Michael Radionov, but did not establish a global function, but set the property of the protractor itself.

protractor.conf.js

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

test e2e.js

 require(protractor.basePath+'/helpers.js'); describe('test', function() { ....... }); 
+10
Jul 21 '15 at 4:07
source share

I think the method we use where I work may be a good solution for you. I posted a brief example of how we handle everything. This is pretty good b / c, you can just call the page object functions in any spec file, and you do not need to use require in the specification.

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

+2
Jul 24 '15 at 20:56
source share



All Articles