Skip to content

Commit 8565d90

Browse files
authored
Update cypress.md
1 parent 5d83b8c commit 8565d90

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

docs/testing/cypress.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,38 @@ Cypress tests are compiled / packed and run in the browser. So feel free to impo
133133

134134
For example you can share Id values between UI and Tests to make sure the CSS selectors don't break:
135135

136-
```ts
136+
```js
137137
import { Ids } from '../../../src/app/constants';
138138

139139
// Later
140140
cy.get(`#${Ids.username}`)
141141
.type('john')
142142
```
143143

144+
## Tip: Creating Page Objects
145+
Creating objects that provide a convenient handle for all the interactions that various tests need to do with a page is a common testing convention. You can create page objects using TypeScript classes with getters and methods e.g.
146+
147+
```js
148+
import { Ids } from '../../../src/app/constants';
149+
150+
class LoginPage {
151+
visit() {
152+
cy.visit('/login');
153+
}
154+
155+
get username() {
156+
return cy.get(`#${Ids.username}`);
157+
}
158+
}
159+
const page = new LoginPage();
160+
161+
// Later
162+
page.visit();
163+
164+
page.username.type('john');
165+
166+
```
167+
144168
## Tip: Implicit assertion
145169
Whenver a cypress command fails you get a nice error (instead of something like `null` with many other frameworks) so you fail quickly and know exactly when a test fails e.g.
146170

0 commit comments

Comments
 (0)