Skip to content

Commit 2ce848d

Browse files
authored
feat: add cy.request example that passes data to the second request (#275)
* Add cy.request example that pass data around * copy new tests to the HTML
1 parent 6ccd180 commit 2ce848d

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

app/commands/network-requests.html

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,60 @@ <h4><a href="https://on.cypress.io/request">cy.request()</a></h4>
118118
expect(response.body).to.have.length(500)
119119
expect(response).to.have.property('headers')
120120
expect(response).to.have.property('duration')
121+
})</code></pre>
122+
<p>A request can pass the response data to the next request.</p>
123+
<pre><code class="javascript">// first, let's find out the userId of the first user we have
124+
cy.request('https://jsonplaceholder.cypress.io/users?_limit=1')
125+
.its('body.0') // yields the first element of the returned list
126+
.then((user) => {
127+
expect(user).property('id').to.be.a('number')
128+
// make a new post on behalf of the user
129+
cy.request('POST', 'https://jsonplaceholder.cypress.io/posts', {
130+
userId: user.id,
131+
title: 'Cypress Test Runner',
132+
body: 'Fast, easy and reliable testing for anything that runs in a browser.',
133+
})
134+
})
135+
// note that the value here is the returned value of the 2nd request
136+
// which is the new post object
137+
.then((response) => {
138+
expect(response).property('status').to.equal(201) // new entity created
139+
expect(response).property('body').to.contain({
140+
id: 101, // there are already 100 posts, so new entity gets id 101
141+
title: 'Cypress Test Runner',
142+
})
143+
// we don't know the user id here - since it was in above closure
144+
// so in this test just confirm that the property is there
145+
expect(response.body).property('userId').to.be.a('number')
146+
})</code></pre>
147+
148+
<p>A good idea is to save the response data to be used later in the shared test context.</p>
149+
<pre><code class="javascript">cy.request('https://jsonplaceholder.cypress.io/users?_limit=1')
150+
.its('body.0') // yields the first element of the returned list
151+
.as('user') // saves the object in the test context
152+
.then(function () {
153+
// NOTE 👀
154+
// By the time this callback runs the "as('user')" command
155+
// has saved the user object in the test context.
156+
// To access the test context we need to use
157+
// the "function () { ... }" callback form,
158+
// otherwise "this" points at a wrong or undefined object!
159+
cy.request('POST', 'https://jsonplaceholder.cypress.io/posts', {
160+
userId: this.user.id,
161+
title: 'Cypress Test Runner',
162+
body: 'Fast, easy and reliable testing for anything that runs in a browser.',
163+
})
164+
.its('body').as('post') // save the new post from the response
165+
})
166+
.then(function () {
167+
// When this callback runs, both "cy.request" API commands have finished
168+
// and the test context has "user" and "post" objects set.
169+
// Let's verify them.
170+
expect(this.post, 'post has the right user id')
171+
.property('userId').to.equal(this.user.id)
121172
})</code></pre>
122173
</div>
174+
123175
<div class="col-xs-5">
124176
</div>
125177

cypress/integration/examples/network_requests.spec.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,60 @@ context('Network Requests', () => {
8787
})
8888
})
8989

90+
it('cy.request() - pass result to the second request', () => {
91+
// first, let's find out the userId of the first user we have
92+
cy.request('https://jsonplaceholder.cypress.io/users?_limit=1')
93+
.its('body.0') // yields the first element of the returned list
94+
.then((user) => {
95+
expect(user).property('id').to.be.a('number')
96+
// make a new post on behalf of the user
97+
cy.request('POST', 'https://jsonplaceholder.cypress.io/posts', {
98+
userId: user.id,
99+
title: 'Cypress Test Runner',
100+
body: 'Fast, easy and reliable testing for anything that runs in a browser.',
101+
})
102+
})
103+
// note that the value here is the returned value of the 2nd request
104+
// which is the new post object
105+
.then((response) => {
106+
expect(response).property('status').to.equal(201) // new entity created
107+
expect(response).property('body').to.contain({
108+
id: 101, // there are already 100 posts, so new entity gets id 101
109+
title: 'Cypress Test Runner',
110+
})
111+
// we don't know the user id here - since it was in above closure
112+
// so in this test just confirm that the property is there
113+
expect(response.body).property('userId').to.be.a('number')
114+
})
115+
})
116+
117+
it('cy.request() - save response in the shared test context', () => {
118+
// https://on.cypress.io/variables-and-aliases
119+
cy.request('https://jsonplaceholder.cypress.io/users?_limit=1')
120+
.its('body.0') // yields the first element of the returned list
121+
.as('user') // saves the object in the test context
122+
.then(function () {
123+
// NOTE 👀
124+
// By the time this callback runs the "as('user')" command
125+
// has saved the user object in the test context.
126+
// To access the test context we need to use
127+
// the "function () { ... }" callback form,
128+
// otherwise "this" points at a wrong or undefined object!
129+
cy.request('POST', 'https://jsonplaceholder.cypress.io/posts', {
130+
userId: this.user.id,
131+
title: 'Cypress Test Runner',
132+
body: 'Fast, easy and reliable testing for anything that runs in a browser.',
133+
})
134+
.its('body').as('post') // save the new post from the response
135+
})
136+
.then(function () {
137+
// When this callback runs, both "cy.request" API commands have finished
138+
// and the test context has "user" and "post" objects set.
139+
// Let's verify them.
140+
expect(this.post, 'post has the right user id').property('userId').to.equal(this.user.id)
141+
})
142+
})
143+
90144
it('cy.route() - route responses to matching requests', () => {
91145
// https://on.cypress.io/route
92146

0 commit comments

Comments
 (0)