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
+21-27Lines changed: 21 additions & 27 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -720,15 +720,11 @@ First, we test logging in. Then, in their own describe block, we have a bunch of
720
720
As we said above, each test starts from zero! Tests do not start from the state where the previous tests ended.
721
721
722
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
-
--->
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.
728
724
729
725
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.
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).
755
751
The callback function saves the details of a logged-in user to localStorage, and reloads the page.
756
752
Now there is no difference to a user logging in with the login form.
757
753
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).
760
755
761
756
Custom commands are declared in <i>cypress/support/commands.js</i>.
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:
How does the [cy.contains](https://docs.cypress.io/api/commands/contains.html) command actually work?
969
964
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>:
971
966
972
-

967
+

973
968
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>:
976
970
977
971

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).
1137
1131
1138
-
#### 5.18: bloglist end to end testing, step2
1132
+
#### 5.18: Blog List End To End Testing, step 2
1139
1133
1140
1134
Make tests for logging in. Test both successful and unsuccessful login attempts.
1141
1135
Make a new user in the <i>beforeEach</i> block for the tests.
The test has to ensure that a new blog is added to the list of all blogs.
1194
1188
1195
-
#### 5.20: bloglist end to end testing, step4
1189
+
#### 5.20: Blog List End To End Testing, step 4
1196
1190
1197
1191
Make a test that confirms users can like a blog.
1198
1192
1199
-
#### 5.21: bloglist end to end testing, step5
1193
+
#### 5.21: Blog List End To End Testing, step 5
1200
1194
1201
1195
Make a test for ensuring that the user who created a blog can delete it.
1202
1196
1203
-
#### 5.22: bloglist end to end testing, step6
1197
+
#### 5.22: Blog List End To End Testing, step 6
1204
1198
1205
1199
Make a test for ensuring that only the creator can see the delete button of a blog, not anyone else.
1206
1200
1207
-
#### 5.23: bloglist end to end testing, step7
1201
+
#### 5.23: Blog List End To End Testing, step 7
1208
1202
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.
1210
1204
1211
1205
<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
+
1213
1207
```js
1214
1208
cy.get('.blog').eq(0).should('contain', 'The title with the most likes')
1215
1209
cy.get('.blog').eq(1).should('contain', 'The title with the second most likes')
0 commit comments