Skip to content

Commit 87af8a9

Browse files
committed
part 5 d
1 parent 4aacfbe commit 87af8a9

File tree

2 files changed

+132
-138
lines changed

2 files changed

+132
-138
lines changed

src/content/5/en/part5d.md

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -720,15 +720,11 @@ First, we test logging in. Then, in their own describe block, we have a bunch of
720720
As we said above, each test starts from zero! Tests do not start from the state where the previous tests ended.
721721
722722
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-
--->
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.
723+
So instead of logging in a user using the form in the <i>beforeEach</i> block, we are going to bypass the UI and do a HTTP request to the backend to log in. The reason for this is that logging in with a HTTP request is much faster than filling out a form.
728724
729725
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.
730726
However, Cypress can handle that as well.
731-
The code is the following
727+
The code is the following:
732728
733729
```js
734730
describe('when logged in', function() {
@@ -751,12 +747,11 @@ describe('when logged in', function() {
751747
})
752748
```
753749
754-
We can access the response to a [cy.request](https://docs.cypress.io/api/commands/request.html) with the _then_ method. Under the hood <i>cy.request</i>, like all Cypress commands, are [promises](https://docs.cypress.io/guides/core-concepts/introduction-to-cypress.html#Commands-Are-Promises).
750+
We can access to the response of a [cy.request](https://docs.cypress.io/api/commands/request.html) with the [_then_](https://docs.cypress.io/guides/core-concepts/introduction-to-cypress#The-Cypress-Command-Queue) method. Under the hood <i>cy.request</i>, like all Cypress commands, are [asynchronous](https://docs.cypress.io/guides/core-concepts/introduction-to-cypress#Commands-Are-Asynchronous).
755751
The callback function saves the details of a logged-in user to localStorage, and reloads the page.
756752
Now there is no difference to a user logging in with the login form.
757753
758-
If and when we write new tests to our application, we have to use the login code in multiple places.
759-
We should make it a [custom command](https://docs.cypress.io/api/cypress-api/custom-commands.html).
754+
If and when we write new tests to our application, we have to use the login code in multiple places, we should make it a [custom command](https://docs.cypress.io/api/cypress-api/custom-commands.html).
760755
761756
Custom commands are declared in <i>cypress/support/commands.js</i>.
762757
The code for logging in is as follows:
@@ -790,7 +785,7 @@ describe('when logged in', function() {
790785
})
791786
```
792787
793-
The same applies to creating a new note now that we think about it. We have a test, which makes a new note using the form. We also make a new note in the <i>beforeEach</i> block of the test testing changing the importance of a note:
788+
The same applies to creating a new note now that we think about it. We have a test, which makes a new note using the form. We also make a new note in the <i>beforeEach</i> block of the test that changes the importance of a note:
794789
795790
```js
796791
describe('Note app', function() {
@@ -839,7 +834,7 @@ Cypress.Commands.add('createNote', ({ content, important }) => {
839834
840835
The command expects the user to be logged in and the user's details to be saved to localStorage.
841836
842-
Now the formatting block becomes:
837+
Now the note beforeEach block becomes:
843838
844839
```js
845840
describe('Note app', function() {
@@ -939,7 +934,7 @@ The tests and the frontend code can be found on the [GitHub](https://github.com/
939934
### Changing the importance of a note
940935
941936
Lastly, let's take a look at the test we did for changing the importance of a note.
942-
First, we'll change the formatting block so that it creates three notes instead of one:
937+
First, we'll change the beforeEach block so that it creates three notes instead of one:
943938
944939
```js
945940
describe('when logged in', function() {
@@ -967,12 +962,11 @@ describe('when logged in', function() {
967962
968963
How does the [cy.contains](https://docs.cypress.io/api/commands/contains.html) command actually work?
969964
970-
When we click the _cy.contains('second note')_ command in Cypress [Test Runner](https://docs.cypress.io/guides/core-concepts/test-runner.html), we see that the command searches for the element containing the text <i>second note</i>:
965+
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>:
971966
972-
![cypress test runner clicking testbody and second note](../../images/5/34new.png)
967+
![cypress test runner clicking second note](../../images/5/34new.png)
973968
974-
By clicking the next line _.contains('make important')_ we see that the test uses
975-
the 'make important' button corresponding to the <i>second note</i>:
969+
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>:
976970
977971
![cypress test runner clicking make important](../../images/5/35new.png)
978972
@@ -1007,7 +1001,7 @@ const Note = ({ note, toggleImportance }) => {
10071001
}
10081002
```
10091003
1010-
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.
1004+
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.
10111005
10121006
![cypress showing test is broken trying to click make important](../../images/5/37new.png)
10131007
@@ -1044,7 +1038,7 @@ Now the first line finds the right button and uses <i>as</i> to save it as <i>th
10441038
10451039
Finally, some notes on how Cypress works and debugging your tests.
10461040
1047-
The form of the Cypress tests gives the impression that the tests are normal JavaScript code, and we could for example try this:
1041+
Because of the form of the Cypress tests, it gives the impression that they are normal JavaScript code, and we could for example try this:
10481042
10491043
```js
10501044
const button = cy.contains('log in')
@@ -1114,7 +1108,7 @@ I especially recommend reading [Introduction to Cypress](https://docs.cypress.io
11141108
11151109
> <i>This is the single most important guide for understanding how to test with Cypress. Read it. Understand it.</i>
11161110
1117-
#### 5.17: bloglist end to end testing, step1
1111+
#### 5.17: Blog List End To End Testing, step 1
11181112
11191113
Configure Cypress for your project. Make a test for checking that the application displays the login form by default.
11201114
@@ -1135,7 +1129,7 @@ describe('Blog app', function() {
11351129
11361130
The <i>beforeEach</i> formatting blog must empty the database using for example the method we used in the [material](/en/part5/end_to_end_testing#controlling-the-state-of-the-database).
11371131
1138-
#### 5.18: bloglist end to end testing, step2
1132+
#### 5.18: Blog List End To End Testing, step 2
11391133
11401134
Make tests for logging in. Test both successful and unsuccessful login attempts.
11411135
Make a new user in the <i>beforeEach</i> block for the tests.
@@ -1168,7 +1162,7 @@ describe('Blog app', function() {
11681162
11691163
<i>Optional bonus exercise</i>: Check that the notification shown with unsuccessful login is displayed red.
11701164
1171-
#### 5.19: bloglist end to end testing, step3
1165+
#### 5.19: Blog List End To End Testing, step 3
11721166
11731167
Make a test that verifies a logged-in user can create a new blog.
11741168
The structure of the test could be as follows:
@@ -1192,24 +1186,24 @@ describe('Blog app', function() {
11921186
11931187
The test has to ensure that a new blog is added to the list of all blogs.
11941188
1195-
#### 5.20: bloglist end to end testing, step4
1189+
#### 5.20: Blog List End To End Testing, step 4
11961190
11971191
Make a test that confirms users can like a blog.
11981192
1199-
#### 5.21: bloglist end to end testing, step5
1193+
#### 5.21: Blog List End To End Testing, step 5
12001194
12011195
Make a test for ensuring that the user who created a blog can delete it.
12021196
1203-
#### 5.22: bloglist end to end testing, step6
1197+
#### 5.22: Blog List End To End Testing, step 6
12041198
12051199
Make a test for ensuring that only the creator can see the delete button of a blog, not anyone else.
12061200
1207-
#### 5.23: bloglist end to end testing, step7
1201+
#### 5.23: Blog List End To End Testing, step 7
12081202
1209-
Make a test that checks that the blogs are ordered according to likes with the blog with the most likes being first.
1203+
Make a test that checks that the blogs are ordered by likes, with the most liked blog being first.
12101204
12111205
<i>This exercise is quite a bit trickier than the previous ones.</i> One solution is to add a certain class for the element which wraps the blog's content and use the [eq](https://docs.cypress.io/api/commands/eq#Syntax) method to get the blog element in a specific index:
1212-
1206+
12131207
```js
12141208
cy.get('.blog').eq(0).should('contain', 'The title with the most likes')
12151209
cy.get('.blog').eq(1).should('contain', 'The title with the second most likes')

0 commit comments

Comments
 (0)