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
Copy file name to clipboardExpand all lines: src/content/5/en/part5d.md
+22-19Lines changed: 22 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,7 @@ Some tests might pass one time and fail another, even if the code does not chang
24
24
25
25
### Cypress
26
26
27
-
E2E library [Cypress](https://www.cypress.io/) has become popular within the last year. Cypress is exceptionally easy to use, and when compared to Selenium, for example, it requires a lot less hassle and headache.
27
+
E2E library [Cypress](https://www.cypress.io/) has become popular within the last years. Cypress is exceptionally easy to use, and when compared to Selenium, for example, it requires a lot less hassle and headache.
28
28
Its operating principle is radically different than most E2E testing libraries because Cypress tests are run completely within the browser.
29
29
Other libraries run the tests in a Node process, which is connected to the browser through an API.
30
30
@@ -58,7 +58,7 @@ We also made a small change to the script that starts the application, without t
58
58
59
59
Unlike the frontend's unit tests, Cypress tests can be in the frontend or the backend repository, or even in their separate repository.
60
60
61
-
The tests require the tested system to be running. Unlike our backend integration tests, Cypress tests <i>do not start</i> the system when they are run.
61
+
The tests require that the system being tested is running. Unlike our backend integration tests, Cypress tests <i>do not start</i> the system when they are run.
62
62
63
63
Let's add an npm script to <i>the backend</i> which starts it in test mode, or so that <i>NODE\_ENV</i> is <i>test</i>.
64
64
@@ -80,7 +80,7 @@ Let's add an npm script to <i>the backend</i> which starts it in test mode, or s
80
80
}
81
81
```
82
82
83
-
**NB** To get Cypress working with WSL2 one might need to do some additional configuring first. These two [links](https://docs.cypress.io/guides/getting-started/installing-cypress#Windows-Subsystem-for-Linux) are great places to [start](https://nickymeuleman.netlify.app/blog/gui-on-wsl2-cypress).
83
+
**NB** To get Cypress working with WSL2 one might need to do some additional configuring first. These two [links](https://docs.cypress.io/guides/references/advanced-installation#Windows-Subsystem-for-Linux) are great places to [start](https://nickymeuleman.netlify.app/blog/gui-on-wsl2-cypress).
84
84
85
85
When both the backend and frontend are running, we can start Cypress with the command
The test is run by clicking the test in the Cypress:
121
+
The test is run by clicking on the test in Cypress:
122
122
123
123
Running the test shows how the application behaves as the test is run:
124
124
125
125

126
126
127
-
The structure of the test should look familiar. They use <i>describe</i> blocks to group different test cases, just like Jest. The test cases have been defined with the <i>it</i> method. Cypress borrowed these parts from the [Mocha](https://mochajs.org/) testing library it uses under the hood.
127
+
The structure of the test should look familiar. They use <i>describe</i> blocks to group different test cases, just like Jest. The test cases have been defined with the <i>it</i> method. Cypress borrowed these parts from the [Mocha](https://mochajs.org/) testing library that it uses under the hood.
128
128
129
129
[cy.visit](https://docs.cypress.io/api/commands/visit.html) and [cy.contains](https://docs.cypress.io/api/commands/contains.html) are Cypress commands, and their purpose is quite obvious.
130
-
[cy.visit](https://docs.cypress.io/api/commands/visit.html) opens the web address given to it as a parameter in the browser used by the test. [cy.contains](https://docs.cypress.io/api/commands/contains.html) searches for the string it received as a parameter from the page.
130
+
[cy.visit](https://docs.cypress.io/api/commands/visit.html) opens the web address given to it as a parameter in the browser used by the test. [cy.contains](https://docs.cypress.io/api/commands/contains.html) searches for the string it received as a parameter in the page.
131
131
132
132
We could have declared the test using an arrow function
133
133
@@ -205,7 +205,7 @@ module.exports = {
205
205
206
206
### Writing to a form
207
207
208
-
Let's extend our tests so that the test tries to log in to our application.
208
+
Let's extend our tests so that our new test tries to log in to our application.
209
209
We assume our backend contains a user with the username <i>mluukkai</i> and password <i>salainen</i>.
The test first searches for the login button by its text and clicks the button with the command [cy.click](https://docs.cypress.io/api/commands/click.html#Syntax).
225
225
226
-
Both of our tests begin the same way, by opening the page <i><http://localhost:5173></i>, so we should
227
-
separate the shared part into a <i>beforeEach</i> block run before each test:
226
+
Both of our tests begin the same way, by opening the page <i><http://localhost:5173></i>, so we should extract the shared code into a <i>beforeEach</i> block run before each test:
228
227
229
228
```js
230
229
describe('Note app', function() {
@@ -261,7 +260,7 @@ it('user can login', function () {
261
260
262
261
The test works. The problem is if we later add more input fields, the test will break because it expects the fields it needs to be the first and the last on the page.
263
262
264
-
It would be better to give our inputs unique <i>ids</i> and use those to find them.
263
+
It would be better to give our inputs unique <i>IDs</i> and use those to find them.
The last row ensures that the login was successful.
318
317
319
-
Note that the CSS[id-selector](https://developer.mozilla.org/en-US/docs/Web/CSS/ID_selectors) is #, so if we want to search for an element with the id <i>username</i> the CSS selector is <i>#username</i>.
318
+
Note that the CSS's [ID selector](https://developer.mozilla.org/en-US/docs/Web/CSS/ID_selectors) is #, so if we want to search for an element with the ID <i>username</i> the CSS selector is <i>#username</i>.
320
319
321
-
Please note that passing the test at this stage requires that there is a user in the test database of the backend environment whose username is <i>mluukkai</i> and the password is <i>salainen</i>. Create a user if needed!
320
+
Please note that passing the test at this stage requires that there is a user in the test database of the backend test environment, whose username is <i>mluukkai</i> and the password is <i>salainen</i>. Create a user if needed!
322
321
323
322
### Testing new note form
324
323
325
-
Let's next add test methods to test the "new note" functionality:
324
+
Next, let's add tests to test the "new note" functionality:
326
325
327
326
```js
328
327
describe('Note app', function() {
@@ -363,7 +362,7 @@ If the page contained more inputs, the test would break
363
362
364
363

365
364
366
-
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.
365
+
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.
367
366
368
367
The structure of the tests looks like so:
369
368
@@ -494,7 +493,7 @@ Unlike earlier, now the testing starts with the backend in the same state every
494
493
495
494
Let's add one more test for checking that we can change the importance of notes.
496
495
497
-
A while ago we changed the frontend so that a new note is important by default, or the <i>important</i> field is <i>true</i>:
496
+
A while ago we changed the frontend so that a new note is important by default, so the <i>important</i> field is <i>true</i>:
First, we use [cy.get](https://docs.cypress.io/api/commands/get.html#Syntax) to search for a component with the CSS class <i>error</i>. Then we check that the error message can be found from this component.
607
-
Note that the [CSS class selector](https://developer.mozilla.org/en-US/docs/Web/CSS/Class_selectors) starts with a full stop, so the selector for the class <i>error</i> is <i>.error</i>.
605
+
First, we use [cy.get](https://docs.cypress.io/api/commands/get.html#Syntax) to search for a component with the CSS class <i>error</i>. Then we check that the error message can be found in this component.
606
+
Note that the [CSS class selectors](https://developer.mozilla.org/en-US/docs/Web/CSS/Class_selectors) start with a full stop, so the selector for the class <i>error</i> is <i>.error</i>.
608
607
609
608
We could do the same using the [should](https://docs.cypress.io/api/commands/should.html) syntax:
610
609
@@ -720,7 +719,11 @@ First, we test logging in. Then, in their own describe block, we have a bunch of
720
719
721
720
As we said above, each test starts from zero! Tests do not start from the state where the previous tests ended.
722
721
723
-
The Cypress documentation gives us the following advice: [Fully test the login flow – but only once](https://docs.cypress.io/guides/end-to-end-testing/testing-your-app#Fully-test-the-login-flow-but-only-once).
722
+
The Cypress documentation gives us the following advice: [Fully test the login flow – but only once](https://docs.cypress.io/guides/end-to-end-testing/testing-your-app#Fully-test-the-login-flow----but-only-once).
723
+
<!---
724
+
!!! TODO NEXT: Remove mention to cypress recommendation or update content to use cy.session (would also need to update example repo)
725
+
!!!!
726
+
--->
724
727
So instead of logging in a user using the form in the <i>beforeEach</i> block, Cypress recommends that we [bypass the UI](https://docs.cypress.io/guides/getting-started/testing-your-app.html#Bypassing-your-UI) and do an HTTP request to the backend to log in. The reason for this is that logging in with an HTTP request is much faster than filling out a form.
725
728
726
729
Our situation is a bit more complicated than in the example in the Cypress documentation because when a user logs in, our application saves their details to the localStorage.
0 commit comments