Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ab31e39

Browse filesBrowse files
committed
Page objects pattern and Dependency Injection
1 parent 2e6b0d5 commit ab31e39
Copy full SHA for ab31e39

File tree

1 file changed

+6
-6
lines changed
Filter options

1 file changed

+6
-6
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+6-6
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ afterEach(function () {
203203

204204
## Page objects pattern
205205

206-
If you are new to the page objects, please follow [great overview](https://martinfowler.com/bliki/PageObject.html) written by [Martin Fowler](https://martinfowler.com/). The basic idea is to keep things [DRY](https://code.tutsplus.com/tutorials/3-key-software-principles-you-must-understand--net-25161) and reusable, but there are few more things to mention: refactoring and readability. Let's have a page object like this:
206+
If you are new to the page objects, please follow [great overview](https://martinfowler.com/bliki/PageObject.html) written by [Martin Fowler](https://martinfowler.com/). The basic idea is to keep things [DRY](https://code.tutsplus.com/tutorials/3-key-software-principles-you-must-understand--net-25161) and reusable, but there are more things to mention: refactoring and readability. Let's have a page object like this:
207207

208208
```js
209209
class LoginPage {
@@ -227,11 +227,11 @@ class LoginPage {
227227
}
228228
```
229229

230-
The example above has following issues:
230+
The example above has the following issues:
231231

232232
* Every time an ID or CSS class change, you have to update the page object.
233-
* If you want to remove ID from the username, you have to check whether is it used in tests or not.
234-
* If you want to remove a CSS class, you have to check whether is it used in tests or not. If it's used in the test, you have to keep class assigned to the element but the class will no longer exist in CSS file. This causes many confusions.
233+
* If you want to remove ID from the username, you have to check whether it is used in tests or not.
234+
* If you want to remove a CSS class, you have to check whether it is used in tests or not. If it's used in the test, you have to keep class assigned to the element but the class will no longer exist in CSS file. This causes many confusions.
235235
* Every time a new object is added to the form, it breaks the XPath for the submit button. Because of it, using XPath instead of CSS selector is always considered a bad idea.
236236

237237
None of these is giving you the confidence to do minor/major refactoring of your CSS or HTML because you might break the tests. To remove this coupling, you can give a unique identifier for your element through `data-` attributes introduces in [HTML5](https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes) or you can use your own attribute. You can choose a name of the attribute such as `data-test-id`, `data-qa` or simply `tid` if you prefer short names. The page object will look like then:
@@ -266,7 +266,7 @@ function findByTId(tid) {
266266
}
267267
```
268268

269-
Adding `*` into the selector allows us to assign more than one identifier to a single element and having better versatility in your page objects, e.g.:
269+
Adding `*` into the selector allows us to assign more than one identifier to a single element and a better versatility in your page objects, e.g.:
270270

271271
```html
272272
<nav>
@@ -306,7 +306,7 @@ class Nav {
306306

307307
## Dependency Injection
308308

309-
Mocking dependencies and imports in tests might be really tedious. Using [inject-loader](https://github.com/plasticine/inject-loader) or [proxyquire](https://github.com/thlorenz/proxyquire) can help a lot but requires lots of ugly coding. Another option is to involve dependency injection, such as [inversify](https://github.com/inversify/InversifyJS). They are primarily focused on Typescript but they support [vanilla JS](https://github.com/inversify/inversify-vanillajs-helpers) too (without any trade offs). In scenarios where you don't have control over instantiating components and services, there is another very handy [toolbox](https://github.com/inversify/inversify-inject-decorators) which gives you a set of decorators usable in Vue components.
309+
Mocking dependencies and imports in tests might be really tedious. Using [inject-loader](https://github.com/plasticine/inject-loader) or [proxyquire](https://github.com/thlorenz/proxyquire) can help a lot but it requires lots of ugly code. Another option is to involve dependency injection, such as [inversify](https://github.com/inversify/InversifyJS). They are primarily focused on Typescript but they support [vanilla JS](https://github.com/inversify/inversify-vanillajs-helpers) too (without any trade offs). In scenarios where you don't have control over instantiating components and services, there is another very handy [toolbox](https://github.com/inversify/inversify-inject-decorators) which gives you a set of decorators usable in Vue components.
310310

311311
### Setting up DI in VUE
312312

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.