Skip to content

Commit ca2fd6a

Browse files
committed
Use labels to find username and password input fields
1 parent c19271c commit ca2fd6a

File tree

2 files changed

+30
-30
lines changed

2 files changed

+30
-30
lines changed

src/content/5/en/part5e.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ describe('Note app', function() {
303303
describe('when logged in', function() {
304304
beforeEach(function() {
305305
cy.contains('button', 'login').click()
306-
cy.get('input:first').type('mluukkai')
307-
cy.get('input:last').type('salainen')
306+
cy.contains('label', 'username').type('mluukkai')
307+
cy.contains('label', 'password').type('salainen')
308308
cy.get('#login-button').click()
309309
})
310310
// highlight-end
@@ -335,7 +335,7 @@ If the page contained more inputs, the test would break
335335

336336
![cypress error - cy.type can only be called on a single element](../../images/5/31x.png)
337337

338-
Due to this problem, it would again be better to give the input an <i>ID</i> and search for the element by its ID.
338+
Due to this problem, it would again be better to give the input an <i>ID</i> and search for the element by its ID. Let's stick with the simplest solution for now.
339339

340340
The structure of the tests looks like so:
341341

@@ -345,8 +345,8 @@ describe('Note app', function() {
345345

346346
it('user can login', function() {
347347
cy.contains('button', 'login').click()
348-
cy.get('#username').type('mluukkai')
349-
cy.get('#password').type('salainen')
348+
cy.contains('label', 'username').type('mluukkai')
349+
cy.contains('label', 'password').type('salainen')
350350
cy.get('#login-button').click()
351351

352352
cy.contains('Matti Luukkainen logged in')
@@ -355,8 +355,8 @@ describe('Note app', function() {
355355
describe('when logged in', function() {
356356
beforeEach(function() {
357357
cy.contains('button', 'login').click()
358-
cy.get('input:first').type('mluukkai')
359-
cy.get('input:last').type('salainen')
358+
cy.contains('label', 'username').type('mluukkai')
359+
cy.contains('label', 'password').type('salainen')
360360
cy.get('#login-button').click()
361361
})
362362

@@ -534,8 +534,8 @@ describe('Note app', function() {
534534

535535
it.only('login fails with wrong password', function() {
536536
cy.contains('button', 'login').click()
537-
cy.get('#username').type('mluukkai')
538-
cy.get('#password').type('wrong')
537+
cy.contains('label', 'username').type('mluukkai')
538+
cy.contains('label', 'password').type('wrong')
539539
cy.get('#login-button').click()
540540

541541
cy.contains('wrong credentials')
@@ -622,8 +622,8 @@ Let's finish the test so that it also checks that the application does not rende
622622
```js
623623
it('login fails with wrong password', function() {
624624
cy.contains('button', 'login').click()
625-
cy.get('#username').type('mluukkai')
626-
cy.get('#password').type('wrong')
625+
cy.contains('label', 'username').type('mluukkai')
626+
cy.contains('label', 'password').type('wrong')
627627
cy.get('#login-button').click()
628628

629629
cy.get('.error')
@@ -659,8 +659,8 @@ Currently, we have the following tests:
659659
describe('Note app', function() {
660660
it('user can login', function() {
661661
cy.contains('button', 'login').click()
662-
cy.get('#username').type('mluukkai')
663-
cy.get('#password').type('salainen')
662+
cy.contains('label', 'username').type('mluukkai')
663+
cy.contains('label', 'password').type('salainen')
664664
cy.get('#login-button').click()
665665

666666
cy.contains('Matti Luukkainen logged in')
@@ -673,8 +673,8 @@ describe('Note app', function() {
673673
describe('when logged in', function() {
674674
beforeEach(function() {
675675
cy.contains('button', 'login').click()
676-
cy.get('input:first').type('mluukkai')
677-
cy.get('input:last').type('salainen')
676+
cy.contains('label', 'username').type('mluukkai')
677+
cy.contains('label', 'password').type('salainen')
678678
cy.get('#login-button').click()
679679
})
680680

src/content/5/fi/osa5e.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -297,8 +297,8 @@ describe('Note app', function() {
297297
describe('when logged in', function() {
298298
beforeEach(function() {
299299
cy.contains('button', 'login').click()
300-
cy.get('#username').type('mluukkai')
301-
cy.get('#password').type('salainen')
300+
cy.contains('label', 'username').type('mluukkai')
301+
cy.contains('label', 'password').type('salainen')
302302
cy.get('#login-button').click()
303303
})
304304
// highlight-end
@@ -328,7 +328,7 @@ Jos kenttiä olisi useampia, testi hajoaisi
328328

329329
![Aiheutuu virhe cy.type() cam only be called on a single element. Your subject contained 2 elements.](../../images/5/31x.png)
330330

331-
Tämän takia olisi jälleen parempi lisätä lomakkeen kentälle <i>id</i> ja hakea kenttä testissä id:n perusteella.
331+
Tämän takia olisi jälleen parempi lisätä lomakkeen kentälle <i>id</i> ja hakea kenttä testissä id:n perusteella. Pitäydytään nyt kuitenkin yksinkertaisimmassa ratkaisussa.
332332

333333
Testien rakenne näyttää seuraavalta:
334334

@@ -338,8 +338,8 @@ describe('Note app', function() {
338338

339339
it('user can login', function() {
340340
cy.contains('button', 'login').click()
341-
cy.get('#username').type('mluukkai')
342-
cy.get('#password').type('salainen')
341+
cy.contains('label', 'username').type('mluukkai')
342+
cy.contains('label', 'password').type('salainen')
343343
cy.get('#login-button').click()
344344

345345
cy.contains('Matti Luukkainen logged in')
@@ -348,8 +348,8 @@ describe('Note app', function() {
348348
describe('when logged in', function() {
349349
beforeEach(function() {
350350
cy.contains('button', 'login').click()
351-
cy.get('#username').type('mluukkai')
352-
cy.get('#password').type('salainen')
351+
cy.contains('label', 'username').type('mluukkai')
352+
cy.contains('label', 'password').type('salainen')
353353
cy.get('#login-button').click()
354354
})
355355

@@ -514,8 +514,8 @@ describe('Note app', function() {
514514

515515
it.only('login fails with wrong password', function() {
516516
cy.contains('button', 'login').click()
517-
cy.get('#username').type('mluukkai')
518-
cy.get('#password').type('wrong')
517+
cy.contains('label', 'username').type('mluukkai')
518+
cy.contains('label', 'password').type('wrong')
519519
cy.get('#login-button').click()
520520

521521
cy.contains('wrong credentials')
@@ -601,8 +601,8 @@ Viimeistellään testi vielä siten, että se varmistaa myös, että sovellus ei
601601
```js
602602
it('login fails with wrong password', function() {
603603
cy.contains('button', 'login').click()
604-
cy.get('#username').type('mluukkai')
605-
cy.get('#password').type('wrong')
604+
cy.contains('label', 'username').type('mluukkai')
605+
cy.contains('label', 'password').type('wrong')
606606
cy.get('#login-button').click()
607607

608608
cy.get('.error')
@@ -630,8 +630,8 @@ Sovelluksemme testit näyttävät tällä hetkellä seuraavalta:
630630
describe('Note app', function() {
631631
it('user can login', function() {
632632
cy.contains('button', 'login').click()
633-
cy.get('#username').type('mluukkai')
634-
cy.get('#password').type('salainen')
633+
cy.contains('label', 'username').type('mluukkai')
634+
cy.contains('label', 'password').type('salainen')
635635
cy.get('#login-button').click()
636636

637637
cy.contains('Matti Luukkainen logged in')
@@ -644,8 +644,8 @@ describe('Note app', function() {
644644
describe('when logged in', function() {
645645
beforeEach(function() {
646646
cy.contains('button', 'login').click()
647-
cy.get('#username').type('mluukkai')
648-
cy.get('#password').type('salainen')
647+
cy.contains('label', 'username').type('mluukkai')
648+
cy.contains('label', 'password').type('salainen')
649649
cy.get('#login-button').click()
650650
})
651651

0 commit comments

Comments
 (0)