I inherited 45 spec files with correlating page object files that are used only to locate elements on the pages. I am trying to create reusable code, is it best practice/can additional exports be added to the same page object file or do they need to exist in a separate 'helper' file?

Article for reference: How to reuse code in Protractor / AngularJS Testing.


The idea is to keep the Page Object abstraction layer as clean and modular as possible avoiding inter-spaghetti-connected page objects with each other.

If you need to use a helper module or function inside a page object, that's fine. If you need to import the nested page objects into the parent page object - this is also perfectly fine, as long as the nestedness follows your page object design and is logical.

For instance, for complicated screens we have this nested page object structure:

- po
  - parent.po
    - child1.po.js
    - child2.po.js
    - index.js

And, then, inside the index.js we may have something like:

var Child1Page = require("./child1.po"),
    Child2Page = require("./child2.po");

var ParentPage = function () {
    this.url = "/#/parent";

    this.someField = $("#myid");

    // child page objects
    this.child1 = new Child1Page(this);
    this.child2 = new Child2Page(this);

module.exports = new ParentPage();

which allows us to access the child page objects from the parent page object with the dot-notation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.