Slimming down your ember-cli-page-object components with a custom `component` helper
Every Ember developer who takes acceptance testing seriously surely knows the brilliant ember-cli-page-object addon. It’s a must-have tool to improve readability and maintainability of your tests.
This article suggests a way to make your page object components more concise, readable and maintainable.
In my projects, page object components quickly get out of hand:
Lots of code duplication! In order to make a point, this example has been exaggerated. Typically, you would use a more concise approach, as suggested by ember-cli-page-object authors:
But as your acceptance test becomes more elaborate, you’ll find yourself using more and more common checks on every element of your page object components.
It’s absolutely tedious to update your page object components by hand every time you need another common check in your tests. Instead, you want typical checks available for any element— and your components start to look like the big example above. :(
In order to resolve this problem and minimize code duplication, I created a custom `component` helper:
Note that ember-cli-page-object 0.x used to have its own `component` helper, but in 1.x it was replaced with a plain object.
This custom helper will decorate your components with a bunch of handful checks, eliminating the need to type them by hand every time.
See how more compact and readable the example page object becomes when used with the `component` helper:
We no longer have to specify common predicates, queries or methods — they are now provided by the `component` helper. Note that we can still provide custom ones if we want to: just add them to a component descriptor (second argument).
This is how you can the revised page object component can be used in a test, async-await style:
Note how all the elements obtained `isActive`, `focus()` and `blur()`.
If your app’s components are robust and build with reusability in mind, you can optimize your page object components even further:
Compare this to the very first example above. 😎
PPS The ember-cli-page-object team are working on a public API that will allow injecting custom checks and methods into default plain object components. You’ll be able to use the concise approach suggested in this article without a need in a custom `component` helper. The default variety of component checks and methods will also become richer.