@@ -138,7 +138,7 @@ module.exports = {
138
138
// wait for page to load
139
139
.waitForElementVisible (' .navbar' , 1000 )
140
140
// click on the login link
141
- .click (' a[href="# /login"]' )
141
+ .click (' a[href="/login"]' )
142
142
143
143
browser .assert .urlContains (' login' );
144
144
},
@@ -196,32 +196,36 @@ Writing this up in code is straight-forward too. Just like we did previously, le
196
196
197
197
``` javascript
198
198
module .exports = {
199
- ' get to login page ' : ( browser ) => {
199
+ " get to login page " : browser => {
200
200
browser
201
+ // Load the page at the launch URL
201
202
.url (browser .launchUrl )
202
- .waitForElementVisible (' .navbar' , 1000 )
203
- .click (' a[href="#/login"]' )
203
+ // wait for page to load
204
+ .waitForElementVisible (" .navbar" , 1000 )
205
+ // click on the login link
206
+ .click (' a[href="/login"]' );
204
207
205
- browser .assert .urlContains (' login' );
208
+ browser .assert .urlContains (" login" );
206
209
},
207
- ' logging in ' : ( browser ) => {
210
+ " logging in " : browser => {
208
211
browser
209
- // set the input email to a valid email
210
- .setValue (' input[type=email]' , ' ari@fullstack.io' )
212
+ // set the input email to a valid username / password
213
+ .setValue (" input[type=text]" , " admin" )
214
+ .setValue (" input[type=password]" , " secret" )
211
215
// submit the form
212
- .click (' input[type=submit]' )
216
+ .click (" input[type=submit]" )
213
217
// wait for the page to load
214
- .waitForElementVisible (' .navbar' , 1000 )
218
+ .waitForElementVisible (" .navbar" , 1000 )
215
219
// Get the text of the h1 tag
216
- .getText (' .content h1' , function (comp ) {
217
- this .assert .equal (comp .value , ' Welcome home!' )
218
- })
220
+ .getText (" .home h1" , function (comp ) {
221
+ this .assert .equal (comp .value , " Welcome home!" );
222
+ });
219
223
220
- browser .assert .urlContains (browser .launchUrl )
224
+ browser .assert .urlContains (browser .launchUrl );
221
225
},
222
- ' logging out ' : ( browser ) => {},
223
- ' close ' : ( browser ) => {},
224
- }
226
+ " logging out " : browser => {},
227
+ close : browser => {}
228
+ };
225
229
```
226
230
227
231
Running these tests again (in the third terminal window):
@@ -235,55 +239,57 @@ nightwatch
235
239
We can do a similar thing with the ` logging out ` step from our browser. To get a user to log out, we will:
236
240
237
241
1 . ` Find and click ` on the logout link
238
- 2 . ` Wait ` for the content to load for the next page (which contains an "are you sure?"-style button).
239
- 3 . We'll ` click ` on the "I'm sure" button to log out
240
- 4 . We'll want to `wait for the content to load again
241
- 5 . We'll ` assert ` that t`he h1 tag contains the value we expect it to have
242
- 6 . And we'll make sure the page shows the Login button
242
+ 2 . We'll want to `wait for the content to load again
243
+ 3 . We'll ` assert ` that t`he h1 tag contains the value we expect it to have
244
+ 4 . And we'll make sure the page shows the Login button
243
245
244
246
Let's implement this with comments inline:
245
247
246
248
247
249
``` javascript
248
250
module .exports = {
249
- ' get to login page ' : ( browser ) => {
251
+ " get to login page " : browser => {
250
252
browser
253
+ // Load the page at the launch URL
251
254
.url (browser .launchUrl )
252
- .waitForElementVisible (' .navbar' , 1000 )
253
- .click (' a[href="#/login"]' )
255
+ // wait for page to load
256
+ .waitForElementVisible (" .navbar" , 1000 )
257
+ // click on the login link
258
+ .click (' a[href="/login"]' );
254
259
255
- browser .assert .urlContains (' login' );
260
+ browser .assert .urlContains (" login" );
256
261
},
257
- ' logging in ' : ( browser ) => {
262
+ " logging in " : browser => {
258
263
browser
259
- .setValue (' input[type=email]' , ' ari@fullstack.io' )
260
- .click (' input[type=submit]' )
261
- .waitForElementVisible (' .navbar' , 1000 )
262
- .getText (' .content h1' , function (comp ) {
263
- this .assert .equal (comp .value , ' Welcome home!' )
264
- })
264
+ // set the input email to a valid username / password
265
+ .setValue (" input[type=text]" , " admin" )
266
+ .setValue (" input[type=password]" , " secret" )
267
+ // submit the form
268
+ .click (" input[type=submit]" )
269
+ // wait for the page to load
270
+ .waitForElementVisible (" .navbar" , 1000 )
271
+ // Get the text of the h1 tag
272
+ .getText (" .home h1" , function (comp ) {
273
+ this .assert .equal (comp .value , " Welcome home!" );
274
+ });
265
275
266
- browser .assert .urlContains (browser .launchUrl )
276
+ browser .assert .urlContains (browser .launchUrl );
267
277
},
268
- ' logging out ' : ( browser ) => {
278
+ " logging out " : browser => {
269
279
browser
270
280
// Find and click on the logout link
271
- .click (' a[href="#/logout"]' )
272
- // Wait for the content to load
273
- .waitForElementVisible (' .content button' , 1000 )
274
- // Click on the button to logout
275
- .click (' button' )
281
+ .click (" .logout" )
276
282
// We'll wait for the next content to load
277
- .waitForElementVisible (' h1 ' , 1000 )
283
+ .waitForElementVisible (" h1 " , 1000 )
278
284
// Get the text of the h1 tag
279
- .getText (' h1 ' , function (res ) {
280
- this .assert .equal (res .value , ' Welcome home! ' )
285
+ .getText (" h1 " , function (res ) {
286
+ this .assert .equal (res .value , " You need to know the secret " );
281
287
})
282
288
// Make sure the Login button shows now
283
- .waitForElementVisible (' a[href="# /login"]' , 1000 );
289
+ .waitForElementVisible (' a[href="/login"]' , 1000 );
284
290
},
285
- ' close ' : ( browser ) => {},
286
- }
291
+ close : browser => {}
292
+ };
287
293
```
288
294
289
295
As of now, you may have noticed that your chrome browsers haven't been closing when the tests have completed. This is because we haven't told selenium that we want the session to be complete. We can use the ` end() ` command on the ` browser ` object to close the connection. This is why we have the last and final step called ` close ` .
0 commit comments