Skip to content

Commit 2f994d3

Browse files
committed
Improve consistency in part5e
1 parent 6289cf1 commit 2f994d3

File tree

8 files changed

+81
-66
lines changed

8 files changed

+81
-66
lines changed

src/content/5/en/part5e.md

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -503,20 +503,18 @@ describe('Note app', function() {
503503

504504
it('it can be made not important', function () {
505505
cy.contains('another note cypress')
506-
.parent()
507506
.contains('button', 'make not important')
508507
.click()
509508

510509
cy.contains('another note cypress')
511-
.parent()
512510
.contains('button', 'make important')
513511
})
514512
})
515513
})
516514
})
517515
```
518516

519-
The first command does several things. First, it searches for an element containing the text <i>another note cypress</i>. Then, using the _parent()_ method, it finds the parent element, and from within that, it searches for the <i>make not important</i> button and clicks it.
517+
The first command does several things. First, it searches for an element containing the text <i>another note cypress</i>. Then, within the found element, it searches for the <i>make not important</i> button and clicks it.
520518

521519
The second command checks that the text on the button has changed to <i>make important</i>.
522520

@@ -887,54 +885,61 @@ cy.visit('')
887885
### Changing the importance of a note
888886
889887
Lastly, let's take a look at the test we did for changing the importance of a note.
890-
First, we'll change the beforeEach block so that it creates three notes instead of one:
888+
First, we'll change the beforeEach block so that it creates three notes instead of one. The tests change as follows:
891889
892890
```js
893-
describe('when logged in', function() {
894-
describe('and several notes exist', function () {
891+
describe('when logged in', function () {
892+
beforeEach(function () {
893+
cy.login({ username: 'mluukkai', password: 'salainen' })
894+
})
895+
896+
it('a new note can be created', function () {
897+
cy.contains('new note').click()
898+
cy.get('input').type('a note created by cypress')
899+
cy.contains('save').click()
900+
cy.contains('a note created by cypress')
901+
})
902+
describe('and several notes exist', function () { // highlight-line
895903
beforeEach(function () {
896-
// highlight-start
897-
cy.login({ username: 'mluukkai', password: 'salainen' })
898-
cy.createNote({ content: 'first note', important: false })
899-
cy.createNote({ content: 'second note', important: false })
900-
cy.createNote({ content: 'third note', important: false })
901-
// highlight-end
904+
cy.createNote({ content: 'first note', important: true }) // highlight-line
905+
cy.createNote({ content: 'second note', important: true }) // highlight-line
906+
cy.createNote({ content: 'third note', important: true }) // highlight-line
902907
})
903908

904-
it('one of those can be made important', function () {
905-
cy.contains('second note')
906-
.contains('make important')
909+
it('one of those can be made non important', function () { // highlight-line
910+
cy.contains('second note') // highlight-line
911+
.contains('button', 'make not important')
907912
.click()
908913

909-
cy.contains('second note')
910-
.contains('make not important')
914+
cy.contains('second note') // highlight-line
915+
.contains('button', 'make important')
911916
})
912917
})
913918
})
914919
```
915920
916921
How does the [cy.contains](https://docs.cypress.io/api/commands/contains.html) command actually work?
917922
918-
When we click the _cy.contains('second note')_ command in Cypress [Test Runner](https://docs.cypress.io/guides/core-concepts/cypress-app#Test-Runner), we see that the command searches for the element containing the text <i>second note</i>:
923+
When we click the _-contains 'second note'_ command in Cypress [Test Runner](https://docs.cypress.io/guides/core-concepts/cypress-app#Test-Runner), we see that the command searches for the element containing the text <i>second note</i>:
919924
920-
![cypress test runner clicking second note](../../images/5/34new.png)
925+
![cypress test runner clicking second note](../../images/5/34eb.png)
921926
922-
By clicking the next line _.contains('make important')_ we see that the test uses the 'make important' button corresponding to the <i>second note</i>:
927+
By clicking the next line _-contains 'button, make not important'_ we see that the test uses the <i>make not important</i> button corresponding to the <i>second note</i>:
923928
924-
![cypress test runner clicking make important](../../images/5/35new.png)
929+
![cypress test runner clicking make important](../../images/5/35a.png)
925930
926931
When chained, the second <i>contains</i> command <i>continues</i> the search from within the component found by the first command.
927932
928933
If we had not chained the commands, and instead write:
929934
930935
```js
931936
cy.contains('second note')
932-
cy.contains('make important').click()
937+
cy.contains('button', 'make not important').click()
933938
```
934939
935940
the result would have been entirely different. The second line of the test would click the button of a wrong note:
936941
937-
![cypress showing error and incorrectly trying to click first button](../../images/5/36new.png)
942+
![cypress showing error and incorrectly trying to click first button](../../images/5/36.png)
938943
939944
When coding tests, you should check in the test runner that the tests use the right components!
940945
@@ -956,15 +961,17 @@ const Note = ({ note, toggleImportance }) => {
956961
957962
Our tests break! As the test runner reveals, _cy.contains('second note')_ now returns the component containing the text, and the button is not in it.
958963
959-
![cypress showing test is broken trying to click make important](../../images/5/37new.png)
964+
![cypress showing test is broken trying to click make important](../../images/5/37.png)
960965
961966
One way to fix this is the following:
962967
963968
```js
964-
it('one of those can be made important', function () {
969+
it('one of those can be made non important', function () {
965970
cy.contains('second note').parent().find('button').click()
966-
cy.contains('second note').parent().find('button')
967-
.should('contain', 'make not important')
971+
cy.contains('second note')
972+
.parent()
973+
.find('button')
974+
.should('contain', 'make important')
968975
})
969976
```
970977
@@ -978,14 +985,14 @@ Unfortunately, we have some copy-paste in the tests now, because the code for se
978985
In these kinds of situations, it is possible to use the [as](https://docs.cypress.io/api/commands/as.html) command:
979986
980987
```js
981-
it('one of those can be made important', function () {
988+
it('one of those can be made non important', function () {
982989
cy.contains('second note').parent().find('button').as('theButton')
983990
cy.get('@theButton').click()
984-
cy.get('@theButton').should('contain', 'make not important')
991+
cy.get('@theButton').should('contain', 'make important')
985992
})
986993
```
987994
988-
Now the first line finds the right button and uses <i>as</i> to save it as <i>theButton</i>. The following lines can use the named element with <i>cy.get('@theButton')</i>.
995+
Now the first line finds the right button and uses <i>as</i> to save it as <i>theButton</i>. The following lines can use the named element with _cy.get('@theButton')_.
989996
990997
### Running and debugging the tests
991998
@@ -1022,7 +1029,7 @@ Stopping the test execution with the debugger is [possible](https://docs.cypress
10221029
The developer console is all sorts of useful when debugging your tests.
10231030
You can see the HTTP requests done by the tests on the Network tab, and the console tab will show you information about your tests:
10241031
1025-
![developer console while running cypress](../../images/5/38new.png)
1032+
![developer console while running cypress](../../images/5/38.png)
10261033
10271034
So far we have run our Cypress tests using the graphical test runner. It is also possible to run them [from the command line](https://docs.cypress.io/guides/guides/command-line.html). We just have to add an npm script for it:
10281035
@@ -1035,9 +1042,9 @@ So far we have run our Cypress tests using the graphical test runner. It is also
10351042
10361043
Now we can run our tests from the command line with the command <i>npm run test:e2e</i>
10371044
1038-
![terminal output of running npm e2e tests showing passed](../../images/5/39new.png)
1045+
![terminal output of running npm e2e tests showing passed](../../images/5/39.png)
10391046
1040-
Note that videos of the test execution will be saved to <i>cypress/videos/</i>, so you should probably git ignore this directory. It is also possible to [turn off](https://docs.cypress.io/guides/guides/screenshots-and-videos#Videos) the making of videos.
1047+
It is also possible to record a video of the test execution in Cypress. Recording a video can be especially useful, for example, when debugging or in a CI/CD pipeline, as the video allows you to easily review what happened in the browser before an error occurred. The feature is disabled by default; instructions for enabling it can be found in the Cypress [documentation](https://docs.cypress.io/guides/guides/screenshots-and-videos#Videos).
10411048
10421049
Tests are found in [GitHub](https://github.com/fullstack-hy2020/notes-e2e-cypress/).
10431050

src/content/5/fi/osa5e.md

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -485,20 +485,18 @@ describe('Note app', function() {
485485

486486
it('it can be made not important', function () {
487487
cy.contains('another note cypress')
488-
.parent()
489488
.contains('button', 'make not important')
490489
.click()
491490

492491
cy.contains('another note cypress')
493-
.parent()
494492
.contains('button', 'make important')
495493
})
496494
})
497495
})
498496
})
499497
```
500498

501-
Ensimmäinen komento tekee monta eri asiaa. Ensin etsitään elementti, missä on teksti <i>another note cypress</i>. Sen jälkeen metodilla _parent()_ haetaan elementin vanhempi, ja sen sisältä etsitään painike <i>make not important</i> ja klikataan sitä.
499+
Ensimmäinen komento tekee monta eri asiaa. Ensin etsitään elementti, missä on teksti <i>another note cypress</i>. Sitten löydetyn elementin sisältä etsitään painike <i>make not important</i> ja klikataan sitä.
502500

503501
Toinen komento varmistaa, että saman napin teksti on vaihtunut muotoon <i>make important</i>.
504502

@@ -851,54 +849,62 @@ cy.visit('')
851849
852850
### Muistiinpanon tärkeyden muutos
853851
854-
Tarkastellaan vielä aiemmin tekemäämme testiä, joka varmistaa että muistiinpanon tärkeyttä on mahdollista muuttaa. Muutetaan testin alustuslohkoa siten, että se luo yhden sijaan kolme muistiinpanoa:
852+
Tarkastellaan vielä aiemmin tekemäämme testiä, joka varmistaa että muistiinpanon tärkeyttä on mahdollista muuttaa. Muutetaan testin alustuslohkoa siten, että se luo yhden sijaan kolme muistiinpanoa. Testit muuttuvat seuraavasti:
855853
856854
```js
857-
describe('when logged in', function() {
858-
describe('and several notes exist', function () {
855+
describe('when logged in', function () {
856+
beforeEach(function () {
857+
cy.login({ username: 'mluukkai', password: 'salainen' })
858+
})
859+
860+
it('a new note can be created', function () {
861+
cy.contains('new note').click()
862+
cy.get('input').type('a note created by cypress')
863+
cy.contains('save').click()
864+
cy.contains('a note created by cypress')
865+
})
866+
describe('and several notes exist', function () { // highlight-line
859867
beforeEach(function () {
860-
// highlight-start
861-
cy.createNote({ content: 'first note', important: false })
862-
cy.createNote({ content: 'second note', important: false })
863-
cy.createNote({ content: 'third note', important: false })
864-
// highlight-end
868+
cy.createNote({ content: 'first note', important: true }) // highlight-line
869+
cy.createNote({ content: 'second note', important: true }) // highlight-line
870+
cy.createNote({ content: 'third note', important: true }) // highlight-line
865871
})
866872

867-
it('one of those can be made important', function () {
868-
cy.contains('second note')
869-
.contains('make important')
873+
it('one of those can be made non important', function () { // highlight-line
874+
cy.contains('second note') // highlight-line
875+
.contains('button', 'make not important')
870876
.click()
871877

872-
cy.contains('second note')
873-
.contains('make not important')
878+
cy.contains('second note') // highlight-line
879+
.contains('button', 'make important')
874880
})
875881
})
876882
})
877883
```
878884
879885
Miten komento [cy.contains](https://docs.cypress.io/api/commands/contains.html) tarkalleen ottaen toimii?
880886
881-
Kun klikkaamme komentoa _cy.contains('second note')_ Cypressin [test runnerista](https://docs.cypress.io/guides/core-concepts/test-runner.html) nähdään, että komento löytää elementin, jonka sisällä on teksti <i>second note</i>:
887+
Kun klikkaamme komentoa _-contains 'second note'_, Cypressin [test runnerista](https://docs.cypress.io/guides/core-concepts/test-runner.html) nähdään, että komento löytää elementin, jonka sisällä on teksti <i>second note</i>:
882888
883-
![Klikatessa vasemmalla olevasta testisteppien listasta komentoa, renderöityy oikealle sovelluksen sen hetkinen tila, missä löydetty elementti on merkattuna korostettuna.](../../images/5/34new.png)
889+
![Klikatessa vasemmalla olevasta testisteppien listasta komentoa, renderöityy oikealle sovelluksen sen hetkinen tila, missä löydetty elementti on merkattuna korostettuna.](../../images/5/34eb.png)
884890
885-
Klikkaamalla seuraavaa riviä _.contains('make important')_, nähdään että löydetään nimenomaan
891+
Klikkaamalla seuraavaa riviä _-contains 'button, make not important'_, nähdään että löydetään nimenomaan
886892
<i>second note</i>:a vastaava tärkeyden muutoksen tekevä nappi:
887893
888-
![Klikatessa vasemmalla olevasta testisteppien listasta komentoa, korostuu oikealle valintaa vastaava nappi](../../images/5/35new.png)
894+
![Klikatessa vasemmalla olevasta testisteppien listasta komentoa, korostuu oikealle valintaa vastaava nappi](../../images/5/35a.png)
889895
890896
Peräkkäin ketjutettuna toisena oleva <i>contains</i>-komento siis <i>jatkaa</i> hakua ensimmäisen komennon löytämän komponentin sisältä.
891897
892898
Jos emme ketjuttaisi komentoja, eli olisimme kirjoittaneet
893899
894900
```js
895901
cy.contains('second note')
896-
cy.contains('make important').click()
902+
cy.contains('button', 'make not important').click()
897903
```
898904
899905
tulos olisi ollut aivan erilainen. Toinen rivi painaisi tässä tapauksessa väärän muistiinpanon nappia:
900906
901-
![Renderöityy virhe AssertionError: Timed out retrying after 4000ms: Expected to find content 'make not important'.](../../images/5/36new.png)
907+
![Renderöityy virhe AssertionError: Timed out retrying after 4000ms: Expected to find content 'make not important'.](../../images/5/36.png)
902908
903909
Testejä tehdessä kannattaa siis ehdottomasti varmistaa test runnerista, että testit etsivät niitä elementtejä, joita niiden on tarkoitus tutkia!
904910
@@ -920,34 +926,36 @@ const Note = ({ note, toggleImportance }) => {
920926
921927
Testit hajoavat! Kuten test runner paljastaa, komento _cy.contains('second note')_ palauttaakin nyt ainoastaan tekstin sisältävän komponentin, ja nappi on sen ulkopuolella:
922928
923-
![Oikealle puolelle havainnollistuu, että fokus osuu napin sijaan pelkkään tekstiin](../../images/5/37new.png)
929+
![Oikealle puolelle havainnollistuu, että fokus osuu napin sijaan pelkkään tekstiin](../../images/5/37.png)
924930
925931
Eräs tapa korjata ongelma on seuraavassa:
926932
927933
```js
928-
it('other of those can be made important', function () {
934+
it('one of those can be made non important', function () {
929935
cy.contains('second note').parent().find('button').click()
930-
cy.contains('second note').parent().find('button')
931-
.should('contain', 'make not important')
936+
cy.contains('second note')
937+
.parent()
938+
.find('button')
939+
.should('contain', 'make important')
932940
})
933941
```
934942
935-
Ensimmäisellä rivillä etsitään komennon [parent](https://docs.cypress.io/api/commands/parent.htm) tekstin <i>second note</i> sisältävän elementin vanhemman alla oleva nappi ja painetaan sitä. Toinen rivi varmistaa, että napin teksti muuttuu.
943+
Ensimmäisellä rivillä etsitään komennon [parent](https://docs.cypress.io/api/commands/parent.htm) avulla tekstin <i>second note</i> sisältävän elementin vanhemman alla oleva nappi ja painetaan sitä. Toinen rivi varmistaa, että napin teksti muuttuu.
936944
937945
Huomaa, että napin etsimiseen käytetään komentoa [find](https://docs.cypress.io/api/commands/find.html#Syntax). Komento [cy.get](https://docs.cypress.io/api/commands/get.html) ei sovellu tähän tilanteeseen, sillä se etsii elementtejä aina <i>koko</i> sivulta ja palauttaisi nyt kaikki sovelluksen viisi nappia.
938946
939947
Testissä on ikävästi copypastea, sillä rivien alku eli napin etsivä koodi on sama.
940948
Tälläisissä tilanteissa on mahdollista hyödyntää komentoa [as](https://docs.cypress.io/api/commands/as.html):
941949
942950
```js
943-
it('other of those can be made important', function () {
951+
it('one of those can be made non important', function () {
944952
cy.contains('second note').parent().find('button').as('theButton')
945953
cy.get('@theButton').click()
946-
cy.get('@theButton').should('contain', 'make not important')
954+
cy.get('@theButton').should('contain', 'make important')
947955
})
948956
```
949957
950-
Nyt ensimmäinen rivi etsii oikean napin, ja tallentaa sen komennon <i>as</i> avulla nimellä <i>theButton</i>. Seuraavat rivit pääsevät nimettyyn elementtiin käsiksi komennolla <i>cy.get('@theButton')</i>.
958+
Nyt ensimmäinen rivi etsii oikean napin, ja tallentaa sen komennon <i>as</i> avulla nimellä <i>theButton</i>. Seuraavat rivit pääsevät nimettyyn elementtiin käsiksi komennolla _cy.get('@theButton')_.
951959
952960
### Testien suoritus ja debuggaaminen
953961
@@ -981,7 +989,7 @@ Myös testien suorituksen pysäyttäminen debuggeriin on [mahdollista](https://d
981989
982990
Developer-konsoli on monin tavoin hyödyllinen testejä debugatessa. Network-tabilla näkyvät testattavan sovelluksen tekemät HTTP-pyynnöt, ja console-välilehti kertoo testin komentoihin liittyviä tietoja:
983991
984-
![Console-välilehti havainnollistaa testien löytämiä elementtejä.](../../images/5/38new.png)
992+
![Console-välilehti havainnollistaa testien löytämiä elementtejä.](../../images/5/38.png)
985993
986994
Olemme toistaiseksi suorittaneet Cypress-testejä ainoastaan graafisen test runnerin kautta. Testit on luonnollisesti mahdollista suorittaa myös [komentoriviltä](https://docs.cypress.io/guides/guides/command-line.html). Lisätään vielä projektiin npm-skripti tätä tarkoitusta varten
987995
@@ -994,9 +1002,9 @@ Olemme toistaiseksi suorittaneet Cypress-testejä ainoastaan graafisen test runn
9941002
9951003
Nyt siis voimme suorittaa Cypress-testit komentoriviltä komennolla <i>npm run test:e2e</i>
9961004
997-
![Komennon suoritus tulostaa konsoliin tekstuaalisen raportin joka kertoo 5 läpimenneestä testistä.](../../images/5/39new.png)
1005+
![Komennon suoritus tulostaa konsoliin tekstuaalisen raportin joka kertoo 5 läpimenneestä testistä.](../../images/5/39.png)
9981006
999-
Huomaa, että testien suorituksesta tallentuu video hakemistoon <i>cypress/videos/</i>. Tämä hakemisto lienee syytä gitignoroida. Videoiden teko on myös mahdollista ottaa [pois päältä](https://docs.cypress.io/guides/guides/screenshots-and-videos#Videos).
1007+
Cypressissä on mahdollista tallentaa myös video testien suorituksesta. Videon tallentaminen voi olla erityisen hyödyllistä esimerkiksi debugatessa tai CI/CD-putkessa, sillä videosta voi jälkeenpäin tarkastaa helposti, mitä selaimessa tapahtui ennen virhettä. Ominaisuus on oletuksena pois päältä, ohjeet sen käyttöönottoon löytyvät Cypressin [dokumentaatiosta](https://docs.cypress.io/guides/guides/screenshots-and-videos#Videos).
10001008
10011009
Testien koodin lopullinen versio on kokonaisuudessaan [GitHubissa](https://github.com/fullstack-hy2020/notes-e2e-cypress/).
10021010

src/content/images/5/34eb.png

65.5 KB
Loading

src/content/images/5/35a.png

68.1 KB
Loading

src/content/images/5/36.png

86.7 KB
Loading

src/content/images/5/37.png

73.7 KB
Loading

src/content/images/5/38.png

120 KB
Loading

src/content/images/5/39.png

52.8 KB
Loading

0 commit comments

Comments
 (0)