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 searches for a component containing the text <i>another note cypress</i>, and then for a <i>make not important</i> button within it. It then clicks the button.
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.
518
520
519
521
The second command checks that the text on the button has changed to <i>make important</i>.
There is one more annoying feature in our tests. The application address <i>http:localhost:5173</i> is hardcoded in many places.
843
+
There is one more annoying feature in our tests. The frontend address <i>http://localhost:5173</i> and the backend address <i>http://localhost:3001</i> are hardcoded for tests. Of these, the address of the backend is actually useless, because a proxy has been defined in the Vite configuration of the frontend, which forwards all requests made by the frontend to the address <i>http:localhost:5173/api</i> to the backend:
844
+
845
+
```js
846
+
exportdefaultdefineConfig({
847
+
server: {
848
+
proxy: {
849
+
'/api': {
850
+
target:'http://localhost:3001',
851
+
changeOrigin:true,
852
+
},
853
+
}
854
+
},
855
+
// ...
856
+
})
857
+
```
858
+
859
+
So we can replace all the addresses in the tests from _http://localhost:3001/api/..._ to _http://localhost:5173/api/..._
838
860
839
861
Let's define the <i>baseUrl</i> for the application in the Cypress pre-generated [configuration file](https://docs.cypress.io/guides/references/configuration) <i>cypress.config.js</i>:
All the commands in the tests use the address of the application
875
+
All commands in the tests and in the <i>command.js</i> file that use the application's address
854
876
855
877
```js
856
-
cy.visit('http://localhost:5173')
878
+
cy.visit('http://localhost:5173')
857
879
```
858
880
859
881
can be transformed into
@@ -862,44 +884,6 @@ can be transformed into
862
884
cy.visit('')
863
885
```
864
886
865
-
The backend's hardcoded address <i>http://localhost:3001</i> is still in the tests. Cypress [documentation](https://docs.cypress.io/guides/guides/environment-variables) recommends defining other addresses used by the tests as environment variables.
866
-
867
-
Let's expand the configuration file <i>cypress.config.js</i> as follows:
On useita eri tapoja testata asia. Seuraavassa etsitään ensin muistiinpano ja klikataan sen nappia <i>make important</i>. Tämän jälkeen tarkistetaan että muistiinpano sisältää napin <i>make not important</i>.
470
+
On useita eri tapoja testata asia. Seuraavassa etsitään ensin muistiinpano ja klikataan sen nappia <i>make not important</i>. Tämän jälkeen tarkistetaan että muistiinpano sisältää napin <i>make important</i>.
Ensimmäinen komento etsii ensin komponentin, missä on teksti <i>another note cypress</i>ja sen sisältä painikkeen <i>make not important</i> ja klikkaa sitä.
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ä.
500
502
501
503
Toinen komento varmistaa, että saman napin teksti on vaihtunut muotoon <i>make important</i>.
Testeissämme on vielä eräs ikävä piirre. Sovelluksen osoite <i>http:localhost:5173</i> on kovakoodattuna moneen kohtaan.
808
+
Testeissämme on vielä eräs ikävä piirre. Sovelluksen frontendin osoite <i>http://localhost:5173</i> sekä backendin osoite <i>http://localhost:3001</i> on kovakoodattuna testeihin. Näistä oikeastaan backendin osoite on turha, sillä frontendin Vite-konfiguraatioon on määritelty proxy, joka forwardoi kaikki osoitteeseen <i>http://localhost:5173/api</i> menevät frontendin tekemät pyynnöt backendiin:
809
+
810
+
```js
811
+
exportdefaultdefineConfig({
812
+
server: {
813
+
proxy: {
814
+
'/api': {
815
+
target:'http://localhost:3001',
816
+
changeOrigin:true,
817
+
},
818
+
}
819
+
},
820
+
// ...
821
+
})
822
+
```
823
+
824
+
Voimme siis korvata testeissä kaikki osoitteet _http://localhost:3001/api/..._ osoitteella _http://localhost:5173/api/..._
803
825
804
826
Määritellään sovellukselle <i>baseUrl</i> Cypressin valmiiksi generoimaan [konfiguraatiotiedostoon](https://docs.cypress.io/guides/references/configuration) <i>cypress.config.js</i>:
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:
0 commit comments