You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
520
518
521
519
The second command checks that the text on the button has changed to <i>make important</i>.
522
520
@@ -887,54 +885,61 @@ cy.visit('')
887
885
### Changing the importance of a note
888
886
889
887
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:
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')
907
912
.click()
908
913
909
-
cy.contains('second note')
910
-
.contains('make not important')
914
+
cy.contains('second note')// highlight-line
915
+
.contains('button', 'make important')
911
916
})
912
917
})
913
918
})
914
919
```
915
920
916
921
How does the [cy.contains](https://docs.cypress.io/api/commands/contains.html) command actually work?
917
922
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>:
919
924
920
-

925
+

921
926
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>:
923
928
924
-

929
+

925
930
926
931
When chained, the second <i>contains</i> command <i>continues</i> the search from within the component found by the first command.
927
932
928
933
If we had not chained the commands, and instead write:
929
934
930
935
```js
931
936
cy.contains('second note')
932
-
cy.contains('make important').click()
937
+
cy.contains('button', 'make not important').click()
933
938
```
934
939
935
940
the result would have been entirely different. The second line of the test would click the button of a wrong note:
936
941
937
-

942
+

938
943
939
944
When coding tests, you should check in the test runner that the tests use the right components!
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')_.
989
996
990
997
### Running and debugging the tests
991
998
@@ -1022,7 +1029,7 @@ Stopping the test execution with the debugger is [possible](https://docs.cypress
1022
1029
The developer console is all sorts of useful when debugging your tests.
1023
1030
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:
1024
1031
1025
-

1032
+

1026
1033
1027
1034
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:
1028
1035
@@ -1035,9 +1042,9 @@ So far we have run our Cypress tests using the graphical test runner. It is also
1035
1042
1036
1043
Now we can run our tests from the command line with the command <i>npm run test:e2e</i>
1037
1044
1038
-

1045
+

1039
1046
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).
1041
1048
1042
1049
Tests are found in [GitHub](https://github.com/fullstack-hy2020/notes-e2e-cypress/).
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ä.
502
500
503
501
Toinen komento varmistaa, että saman napin teksti on vaihtunut muotoon <i>make important</i>.
504
502
@@ -851,54 +849,62 @@ cy.visit('')
851
849
852
850
### Muistiinpanon tärkeyden muutos
853
851
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:
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')
870
876
.click()
871
877
872
-
cy.contains('second note')
873
-
.contains('make not important')
878
+
cy.contains('second note')// highlight-line
879
+
.contains('button', 'make important')
874
880
})
875
881
})
876
882
})
877
883
```
878
884
879
885
Miten komento [cy.contains](https://docs.cypress.io/api/commands/contains.html) tarkalleen ottaen toimii?
880
886
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>:
882
888
883
-

889
+

884
890
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
886
892
<i>second note</i>:a vastaava tärkeyden muutoksen tekevä nappi:
887
893
888
-

894
+

889
895
890
896
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ä.
891
897
892
898
Jos emme ketjuttaisi komentoja, eli olisimme kirjoittaneet
893
899
894
900
```js
895
901
cy.contains('second note')
896
-
cy.contains('make important').click()
902
+
cy.contains('button', 'make not important').click()
897
903
```
898
904
899
905
tulos olisi ollut aivan erilainen. Toinen rivi painaisi tässä tapauksessa väärän muistiinpanon nappia:
900
906
901
-

907
+

902
908
903
909
Testejä tehdessä kannattaa siis ehdottomasti varmistaa test runnerista, että testit etsivät niitä elementtejä, joita niiden on tarkoitus tutkia!
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:
922
928
923
-

929
+

924
930
925
931
Eräs tapa korjata ongelma on seuraavassa:
926
932
927
933
```js
928
-
it('other of those can be made important', function () {
934
+
it('one of those can be made non important', function () {
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.
936
944
937
945
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.
938
946
939
947
Testissä on ikävästi copypastea, sillä rivien alku eli napin etsivä koodi on sama.
940
948
Tälläisissä tilanteissa on mahdollista hyödyntää komentoa [as](https://docs.cypress.io/api/commands/as.html):
941
949
942
950
```js
943
-
it('other of those can be made important', function () {
951
+
it('one of those can be made non important', function () {
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')_.
951
959
952
960
### Testien suoritus ja debuggaaminen
953
961
@@ -981,7 +989,7 @@ Myös testien suorituksen pysäyttäminen debuggeriin on [mahdollista](https://d
981
989
982
990
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:
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
987
995
@@ -994,9 +1002,9 @@ Olemme toistaiseksi suorittaneet Cypress-testejä ainoastaan graafisen test runn
994
1002
995
1003
Nyt siis voimme suorittaa Cypress-testit komentoriviltä komennolla <i>npm run test:e2e</i>
996
1004
997
-

1005
+

998
1006
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).
1000
1008
1001
1009
Testien koodin lopullinen versio on kokonaisuudessaan [GitHubissa](https://github.com/fullstack-hy2020/notes-e2e-cypress/).
0 commit comments