Skip to content

Commit 4aacfbe

Browse files
committed
save progress
1 parent 342a2e8 commit 4aacfbe

File tree

2 files changed

+176
-172
lines changed

2 files changed

+176
-172
lines changed

src/content/5/en/part5d.md

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Some tests might pass one time and fail another, even if the code does not chang
2424

2525
### Cypress
2626

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.
2828
Its operating principle is radically different than most E2E testing libraries because Cypress tests are run completely within the browser.
2929
Other libraries run the tests in a Node process, which is connected to the browser through an API.
3030

@@ -58,7 +58,7 @@ We also made a small change to the script that starts the application, without t
5858

5959
Unlike the frontend's unit tests, Cypress tests can be in the frontend or the backend repository, or even in their separate repository.
6060

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.
6262

6363
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>.
6464

@@ -80,7 +80,7 @@ Let's add an npm script to <i>the backend</i> which starts it in test mode, or s
8080
}
8181
```
8282

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).
8484

8585
When both the backend and frontend are running, we can start Cypress with the command
8686

@@ -118,16 +118,16 @@ describe('Note app', function() {
118118
})
119119
```
120120

121-
The test is run by clicking the test in the Cypress:
121+
The test is run by clicking on the test in Cypress:
122122

123123
Running the test shows how the application behaves as the test is run:
124124

125125
![cypress showing automation of note test](../../images/5/56new.png)
126126

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.
128128

129129
[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.
131131

132132
We could have declared the test using an arrow function
133133

@@ -205,7 +205,7 @@ module.exports = {
205205

206206
### Writing to a form
207207

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.
209209
We assume our backend contains a user with the username <i>mluukkai</i> and password <i>salainen</i>.
210210

211211
The test begins by opening the login form.
@@ -223,8 +223,7 @@ describe('Note app', function() {
223223

224224
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).
225225

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:
228227

229228
```js
230229
describe('Note app', function() {
@@ -261,7 +260,7 @@ it('user can login', function () {
261260

262261
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.
263262

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.
265264
We change our login form like so:
266265

267266
```js
@@ -296,7 +295,7 @@ const LoginForm = ({ ... }) => {
296295
}
297296
```
298297

299-
We also added an id to our submit button so we can access it in our tests.
298+
We also added an ID to our submit button so we can access it in our tests.
300299

301300
The test becomes:
302301

@@ -316,13 +315,13 @@ describe('Note app', function() {
316315

317316
The last row ensures that the login was successful.
318317

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>.
320319

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!
322321

323322
### Testing new note form
324323

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:
326325

327326
```js
328327
describe('Note app', function() {
@@ -363,7 +362,7 @@ If the page contained more inputs, the test would break
363362

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

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.
367366

368367
The structure of the tests looks like so:
369368

@@ -494,7 +493,7 @@ Unlike earlier, now the testing starts with the backend in the same state every
494493

495494
Let's add one more test for checking that we can change the importance of notes.
496495

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>:
498497

499498
```js
500499
const NoteForm = ({ createNote }) => {
@@ -603,8 +602,8 @@ it('login fails with wrong password', function() {
603602
})
604603
```
605604
606-
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>.
608607
609608
We could do the same using the [should](https://docs.cypress.io/api/commands/should.html) syntax:
610609
@@ -720,7 +719,11 @@ First, we test logging in. Then, in their own describe block, we have a bunch of
720719
721720
As we said above, each test starts from zero! Tests do not start from the state where the previous tests ended.
722721
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+
--->
724727
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.
725728
726729
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

Comments
 (0)