cypress-cli-select 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/.github/workflows/jest.yml +12 -0
  2. package/CONTRIBUTING.md +39 -0
  3. package/LICENSE.md +21 -0
  4. package/README.md +197 -0
  5. package/assets/choose-spec-pattern-demo.gif +0 -0
  6. package/assets/cypress-cli-select-animated.gif +0 -0
  7. package/assets/print-selected-demo.png +0 -0
  8. package/assets/run-help.gif +0 -0
  9. package/assets/run-spec-tag.gif +0 -0
  10. package/assets/run-spec-title.gif +0 -0
  11. package/cypress/e2e/1-getting-started/todo.cy.js +143 -0
  12. package/cypress/e2e/2-advanced-examples/actions.cy.js +321 -0
  13. package/cypress/e2e/2-advanced-examples/aliasing.cy.js +39 -0
  14. package/cypress/e2e/2-advanced-examples/assertions.cy.js +176 -0
  15. package/cypress/e2e/2-advanced-examples/connectors.cy.js +98 -0
  16. package/cypress/e2e/2-advanced-examples/cookies.cy.js +118 -0
  17. package/cypress/e2e/2-advanced-examples/cypress_api.cy.js +211 -0
  18. package/cypress/e2e/2-advanced-examples/location.cy.js +32 -0
  19. package/cypress/e2e/2-advanced-examples/misc.cy.js +90 -0
  20. package/cypress/e2e/2-advanced-examples/navigation.cy.js +55 -0
  21. package/cypress/e2e/2-advanced-examples/network_requests.cy.js +163 -0
  22. package/cypress/e2e/2-advanced-examples/querying.cy.js +114 -0
  23. package/cypress/e2e/2-advanced-examples/spies_stubs_clocks.cy.js +220 -0
  24. package/cypress/e2e/2-advanced-examples/storage.cy.js +117 -0
  25. package/cypress/e2e/2-advanced-examples/traversal.cy.js +126 -0
  26. package/cypress/e2e/2-advanced-examples/utilities.cy.js +109 -0
  27. package/cypress/e2e/2-advanced-examples/viewport.cy.js +58 -0
  28. package/cypress/e2e/2-advanced-examples/waiting.cy.js +30 -0
  29. package/cypress/e2e/2-advanced-examples/window.cy.js +22 -0
  30. package/cypress/support/commands.js +25 -0
  31. package/cypress/support/component-index.html +14 -0
  32. package/cypress/support/component.js +24 -0
  33. package/cypress/support/e2e.js +19 -0
  34. package/cypress.config.js +25 -0
  35. package/cypress.new.config.js +26 -0
  36. package/index.js +586 -0
  37. package/package.json +46 -0
  38. package/src/components/Clock.cy.js +13 -0
  39. package/src/components/Stepper.cy.js +13 -0
  40. package/src/sortable-list.js +82 -0
  41. package/tapes/run-help.tape +94 -0
  42. package/tapes/run-spec-tag.tape +163 -0
  43. package/tapes/run-spec-title.tape +191 -0
  44. package/tests/cli-component.spec.js +409 -0
  45. package/tests/cli-e2e.spec.js +439 -0
@@ -0,0 +1,118 @@
1
+ /// <reference types="cypress" />
2
+
3
+ context('Cookies', () => {
4
+ beforeEach(() => {
5
+ Cypress.Cookies.debug(true)
6
+
7
+ cy.visit('https://example.cypress.io/commands/cookies')
8
+
9
+ // clear cookies again after visiting to remove
10
+ // any 3rd party cookies picked up such as cloudflare
11
+ cy.clearCookies()
12
+ })
13
+
14
+ it('cy.getCookie() - get a browser cookie', () => {
15
+ // https://on.cypress.io/getcookie
16
+ cy.get('#getCookie .set-a-cookie').click()
17
+
18
+ // cy.getCookie() yields a cookie object
19
+ cy.getCookie('token').should('have.property', 'value', '123ABC')
20
+ })
21
+
22
+ it('cy.getCookies() - get browser cookies for the current domain', () => {
23
+ // https://on.cypress.io/getcookies
24
+ cy.getCookies().should('be.empty')
25
+
26
+ cy.get('#getCookies .set-a-cookie').click()
27
+
28
+ // cy.getCookies() yields an array of cookies
29
+ cy.getCookies().should('have.length', 1).should((cookies) => {
30
+ // each cookie has these properties
31
+ expect(cookies[0]).to.have.property('name', 'token')
32
+ expect(cookies[0]).to.have.property('value', '123ABC')
33
+ expect(cookies[0]).to.have.property('httpOnly', false)
34
+ expect(cookies[0]).to.have.property('secure', false)
35
+ expect(cookies[0]).to.have.property('domain')
36
+ expect(cookies[0]).to.have.property('path')
37
+ })
38
+ })
39
+
40
+ it('cy.getAllCookies() - get all browser cookies', () => {
41
+ // https://on.cypress.io/getallcookies
42
+ cy.getAllCookies().should('be.empty')
43
+
44
+ cy.setCookie('key', 'value')
45
+ cy.setCookie('key', 'value', { domain: '.example.com' })
46
+
47
+ // cy.getAllCookies() yields an array of cookies
48
+ cy.getAllCookies().should('have.length', 2).should((cookies) => {
49
+ // each cookie has these properties
50
+ expect(cookies[0]).to.have.property('name', 'key')
51
+ expect(cookies[0]).to.have.property('value', 'value')
52
+ expect(cookies[0]).to.have.property('httpOnly', false)
53
+ expect(cookies[0]).to.have.property('secure', false)
54
+ expect(cookies[0]).to.have.property('domain')
55
+ expect(cookies[0]).to.have.property('path')
56
+
57
+ expect(cookies[1]).to.have.property('name', 'key')
58
+ expect(cookies[1]).to.have.property('value', 'value')
59
+ expect(cookies[1]).to.have.property('httpOnly', false)
60
+ expect(cookies[1]).to.have.property('secure', false)
61
+ expect(cookies[1]).to.have.property('domain', '.example.com')
62
+ expect(cookies[1]).to.have.property('path')
63
+ })
64
+ })
65
+
66
+ it('cy.setCookie() - set a browser cookie', () => {
67
+ // https://on.cypress.io/setcookie
68
+ cy.getCookies().should('be.empty')
69
+
70
+ cy.setCookie('foo', 'bar')
71
+
72
+ // cy.getCookie() yields a cookie object
73
+ cy.getCookie('foo').should('have.property', 'value', 'bar')
74
+ })
75
+
76
+ it('cy.clearCookie() - clear a browser cookie', () => {
77
+ // https://on.cypress.io/clearcookie
78
+ cy.getCookie('token').should('be.null')
79
+
80
+ cy.get('#clearCookie .set-a-cookie').click()
81
+
82
+ cy.getCookie('token').should('have.property', 'value', '123ABC')
83
+
84
+ // cy.clearCookies() yields null
85
+ cy.clearCookie('token')
86
+
87
+ cy.getCookie('token').should('be.null')
88
+ })
89
+
90
+ it('cy.clearCookies() - clear browser cookies for the current domain', () => {
91
+ // https://on.cypress.io/clearcookies
92
+ cy.getCookies().should('be.empty')
93
+
94
+ cy.get('#clearCookies .set-a-cookie').click()
95
+
96
+ cy.getCookies().should('have.length', 1)
97
+
98
+ // cy.clearCookies() yields null
99
+ cy.clearCookies()
100
+
101
+ cy.getCookies().should('be.empty')
102
+ })
103
+
104
+ it('cy.clearAllCookies() - clear all browser cookies', () => {
105
+ // https://on.cypress.io/clearallcookies
106
+ cy.getAllCookies().should('be.empty')
107
+
108
+ cy.setCookie('key', 'value')
109
+ cy.setCookie('key', 'value', { domain: '.example.com' })
110
+
111
+ cy.getAllCookies().should('have.length', 2)
112
+
113
+ // cy.clearAllCookies() yields null
114
+ cy.clearAllCookies()
115
+
116
+ cy.getAllCookies().should('be.empty')
117
+ })
118
+ })
@@ -0,0 +1,211 @@
1
+ /// <reference types="cypress" />
2
+
3
+ context('Cypress APIs', () => {
4
+ context('Cypress.Commands', () => {
5
+ beforeEach(() => {
6
+ cy.visit('https://example.cypress.io/cypress-api');
7
+ });
8
+
9
+ // https://on.cypress.io/custom-commands
10
+
11
+ it('.add() - create a custom command', () => {
12
+ Cypress.Commands.add(
13
+ 'console',
14
+ {
15
+ prevSubject: true,
16
+ },
17
+ (subject, method) => {
18
+ // the previous subject is automatically received
19
+ // and the commands arguments are shifted
20
+
21
+ // allow us to change the console method used
22
+ method = method || 'log';
23
+
24
+ // log the subject to the console
25
+ console[method]('The subject is', subject);
26
+
27
+ // whatever we return becomes the new subject
28
+ // we don't want to change the subject so
29
+ // we return whatever was passed in
30
+ return subject;
31
+ }
32
+ );
33
+
34
+ cy.get('button')
35
+ .console('info')
36
+ .then(($button) => {
37
+ // subject is still $button
38
+ });
39
+ });
40
+ });
41
+
42
+ context('Cypress.Cookies', () => {
43
+ beforeEach(() => {
44
+ cy.visit('https://example.cypress.io/cypress-api');
45
+ });
46
+
47
+ // https://on.cypress.io/cookies
48
+ it('.debug() - enable or disable debugging', () => {
49
+ Cypress.Cookies.debug(true);
50
+
51
+ // Cypress will now log in the console when
52
+ // cookies are set or cleared
53
+ cy.setCookie('fakeCookie', '123ABC');
54
+ cy.clearCookie('fakeCookie');
55
+ cy.setCookie('fakeCookie', '123ABC');
56
+ cy.clearCookie('fakeCookie');
57
+ cy.setCookie('fakeCookie', '123ABC');
58
+ });
59
+ });
60
+
61
+ context('Cypress.arch', () => {
62
+ beforeEach(() => {
63
+ cy.visit('https://example.cypress.io/cypress-api');
64
+ });
65
+
66
+ it('Get CPU architecture name of underlying OS', () => {
67
+ // https://on.cypress.io/arch
68
+ expect(Cypress.arch).to.exist;
69
+ });
70
+ });
71
+
72
+ context('Cypress.config()', () => {
73
+ beforeEach(() => {
74
+ cy.visit('https://example.cypress.io/cypress-api');
75
+ });
76
+
77
+ it('Get and set configuration options', () => {
78
+ // https://on.cypress.io/config
79
+ let myConfig = Cypress.config();
80
+
81
+ expect(myConfig).to.have.property('animationDistanceThreshold', 5);
82
+ expect(myConfig).to.have.property('baseUrl', null);
83
+ expect(myConfig).to.have.property('defaultCommandTimeout', 4000);
84
+ expect(myConfig).to.have.property('requestTimeout', 5000);
85
+ expect(myConfig).to.have.property('responseTimeout', 30000);
86
+ expect(myConfig).to.have.property('viewportHeight', 660);
87
+ expect(myConfig).to.have.property('viewportWidth', 1000);
88
+ expect(myConfig).to.have.property('pageLoadTimeout', 60000);
89
+ expect(myConfig).to.have.property('waitForAnimations', true);
90
+
91
+ expect(Cypress.config('pageLoadTimeout')).to.eq(60000);
92
+
93
+ // this will change the config for the rest of your tests!
94
+ Cypress.config('pageLoadTimeout', 20000);
95
+
96
+ expect(Cypress.config('pageLoadTimeout')).to.eq(20000);
97
+
98
+ Cypress.config('pageLoadTimeout', 60000);
99
+ });
100
+ });
101
+
102
+ context('Cypress.dom', () => {
103
+ beforeEach(() => {
104
+ cy.visit('https://example.cypress.io/cypress-api');
105
+ });
106
+
107
+ // https://on.cypress.io/dom
108
+ it('.isHidden() - determine if a DOM element is hidden', () => {
109
+ let hiddenP = Cypress.$('.dom-p p.hidden').get(0);
110
+ let visibleP = Cypress.$('.dom-p p.visible').get(0);
111
+
112
+ // our first paragraph has css class 'hidden'
113
+ expect(Cypress.dom.isHidden(hiddenP)).to.be.true;
114
+ expect(Cypress.dom.isHidden(visibleP)).to.be.false;
115
+ });
116
+ });
117
+
118
+ context('Cypress.env()', () => {
119
+ beforeEach(() => {
120
+ cy.visit('https://example.cypress.io/cypress-api');
121
+ });
122
+
123
+ // We can set environment variables for highly dynamic values
124
+
125
+ // https://on.cypress.io/environment-variables
126
+ it('Get environment variables', () => {
127
+ // https://on.cypress.io/env
128
+ // set multiple environment variables
129
+ Cypress.env({
130
+ host: 'veronica.dev.local',
131
+ api_server: 'http://localhost:8888/v1/',
132
+ });
133
+
134
+ // get environment variable
135
+ expect(Cypress.env('host')).to.eq('veronica.dev.local');
136
+
137
+ // set environment variable
138
+ Cypress.env('api_server', 'http://localhost:8888/v2/');
139
+ expect(Cypress.env('api_server')).to.eq('http://localhost:8888/v2/');
140
+
141
+ // get all environment variable
142
+ expect(Cypress.env()).to.have.property('host', 'veronica.dev.local');
143
+ expect(Cypress.env()).to.have.property(
144
+ 'api_server',
145
+ 'http://localhost:8888/v2/'
146
+ );
147
+ });
148
+ });
149
+
150
+ context('Cypress.log', () => {
151
+ beforeEach(() => {
152
+ cy.visit('https://example.cypress.io/cypress-api');
153
+ });
154
+ context('nested 1', () => {
155
+ it('nested 1 - test', () => {
156
+ expect(1).equal(1);
157
+ });
158
+ context('nested 2', () => {
159
+ it('nested 2, a good - test', () => {
160
+ expect(1).equal(1);
161
+ });
162
+ describe('nested 3', () => {
163
+ specify('nested 3 - too bad too sad', () => {
164
+ expect(2).equal(2);
165
+ });
166
+ });
167
+ });
168
+ });
169
+ it('Control what is printed to the Command Log', () => {
170
+ // https://on.cypress.io/cypress-log
171
+ });
172
+ });
173
+
174
+ context('Cypress.platform', () => {
175
+ beforeEach(() => {
176
+ cy.visit('https://example.cypress.io/cypress-api');
177
+ });
178
+
179
+ it('Get underlying OS name', () => {
180
+ // https://on.cypress.io/platform
181
+ expect(Cypress.platform).to.be.exist;
182
+ });
183
+ });
184
+
185
+ context('Cypress.version', () => {
186
+ beforeEach(() => {
187
+ cy.visit('https://example.cypress.io/cypress-api');
188
+ });
189
+
190
+ it('Get current version of Cypress being run', () => {
191
+ // https://on.cypress.io/version
192
+ expect(Cypress.version).to.be.exist;
193
+ });
194
+ });
195
+
196
+ context('Cypress.spec', () => {
197
+ beforeEach(() => {
198
+ cy.visit('https://example.cypress.io/cypress-api');
199
+ });
200
+
201
+ it('Get current spec information', () => {
202
+ // https://on.cypress.io/spec
203
+ // wrap the object so we can inspect it easily by clicking in the command log
204
+ cy.wrap(Cypress.spec).should('include.keys', [
205
+ 'name',
206
+ 'relative',
207
+ 'absolute',
208
+ ]);
209
+ });
210
+ });
211
+ });
@@ -0,0 +1,32 @@
1
+ /// <reference types="cypress" />
2
+
3
+ context('Location', () => {
4
+ beforeEach(() => {
5
+ cy.visit('https://example.cypress.io/commands/location')
6
+ })
7
+
8
+ it('cy.hash() - get the current URL hash', () => {
9
+ // https://on.cypress.io/hash
10
+ cy.hash().should('be.empty')
11
+ })
12
+
13
+ it('cy.location() - get window.location', () => {
14
+ // https://on.cypress.io/location
15
+ cy.location().should((location) => {
16
+ expect(location.hash).to.be.empty
17
+ expect(location.href).to.eq('https://example.cypress.io/commands/location')
18
+ expect(location.host).to.eq('example.cypress.io')
19
+ expect(location.hostname).to.eq('example.cypress.io')
20
+ expect(location.origin).to.eq('https://example.cypress.io')
21
+ expect(location.pathname).to.eq('/commands/location')
22
+ expect(location.port).to.eq('')
23
+ expect(location.protocol).to.eq('https:')
24
+ expect(location.search).to.be.empty
25
+ })
26
+ })
27
+
28
+ it('cy.url() - get the current URL', () => {
29
+ // https://on.cypress.io/url
30
+ cy.url().should('eq', 'https://example.cypress.io/commands/location')
31
+ })
32
+ })
@@ -0,0 +1,90 @@
1
+ /// <reference types="cypress" />
2
+
3
+ context('Misc', () => {
4
+ beforeEach(() => {
5
+ cy.visit('https://example.cypress.io/commands/misc')
6
+ })
7
+
8
+ it('cy.exec() - execute a system command', () => {
9
+ // execute a system command.
10
+ // so you can take actions necessary for
11
+ // your test outside the scope of Cypress.
12
+ // https://on.cypress.io/exec
13
+
14
+ // we can use Cypress.platform string to
15
+ // select appropriate command
16
+ // https://on.cypress/io/platform
17
+ cy.log(`Platform ${Cypress.platform} architecture ${Cypress.arch}`)
18
+
19
+ // on CircleCI Windows build machines we have a failure to run bash shell
20
+ // https://github.com/cypress-io/cypress/issues/5169
21
+ // so skip some of the tests by passing flag "--env circle=true"
22
+ const isCircleOnWindows = Cypress.platform === 'win32' && Cypress.env('circle')
23
+
24
+ if (isCircleOnWindows) {
25
+ cy.log('Skipping test on CircleCI')
26
+
27
+ return
28
+ }
29
+
30
+ // cy.exec problem on Shippable CI
31
+ // https://github.com/cypress-io/cypress/issues/6718
32
+ const isShippable = Cypress.platform === 'linux' && Cypress.env('shippable')
33
+
34
+ if (isShippable) {
35
+ cy.log('Skipping test on ShippableCI')
36
+
37
+ return
38
+ }
39
+
40
+ cy.exec('echo Jane Lane')
41
+ .its('stdout').should('contain', 'Jane Lane')
42
+
43
+ if (Cypress.platform === 'win32') {
44
+ cy.exec(`print ${Cypress.config('configFile')}`)
45
+ .its('stderr').should('be.empty')
46
+ } else {
47
+ cy.exec(`cat ${Cypress.config('configFile')}`)
48
+ .its('stderr').should('be.empty')
49
+
50
+ cy.exec('pwd')
51
+ .its('code').should('eq', 0)
52
+ }
53
+ })
54
+
55
+ it('cy.focused() - get the DOM element that has focus', () => {
56
+ // https://on.cypress.io/focused
57
+ cy.get('.misc-form').find('#name').click()
58
+ cy.focused().should('have.id', 'name')
59
+
60
+ cy.get('.misc-form').find('#description').click()
61
+ cy.focused().should('have.id', 'description')
62
+ })
63
+
64
+ context('Cypress.Screenshot', function () {
65
+ it('cy.screenshot() - take a screenshot', () => {
66
+ // https://on.cypress.io/screenshot
67
+ cy.screenshot('my-image')
68
+ })
69
+
70
+ it('Cypress.Screenshot.defaults() - change default config of screenshots', function () {
71
+ Cypress.Screenshot.defaults({
72
+ blackout: ['.foo'],
73
+ capture: 'viewport',
74
+ clip: { x: 0, y: 0, width: 200, height: 200 },
75
+ scale: false,
76
+ disableTimersAndAnimations: true,
77
+ screenshotOnRunFailure: true,
78
+ onBeforeScreenshot () { },
79
+ onAfterScreenshot () { },
80
+ })
81
+ })
82
+ })
83
+
84
+ it('cy.wrap() - wrap an object', () => {
85
+ // https://on.cypress.io/wrap
86
+ cy.wrap({ foo: 'bar' })
87
+ .should('have.property', 'foo')
88
+ .and('include', 'bar')
89
+ })
90
+ })
@@ -0,0 +1,55 @@
1
+ /// <reference types="cypress" />
2
+
3
+ context('Navigation', () => {
4
+ beforeEach(() => {
5
+ cy.visit('https://example.cypress.io')
6
+ cy.get('.navbar-nav').contains('Commands').click()
7
+ cy.get('.dropdown-menu').contains('Navigation').click()
8
+ })
9
+
10
+ it('cy.go() - go back or forward in the browser\'s history', () => {
11
+ // https://on.cypress.io/go
12
+
13
+ cy.location('pathname').should('include', 'navigation')
14
+
15
+ cy.go('back')
16
+ cy.location('pathname').should('not.include', 'navigation')
17
+
18
+ cy.go('forward')
19
+ cy.location('pathname').should('include', 'navigation')
20
+
21
+ // clicking back
22
+ cy.go(-1)
23
+ cy.location('pathname').should('not.include', 'navigation')
24
+
25
+ // clicking forward
26
+ cy.go(1)
27
+ cy.location('pathname').should('include', 'navigation')
28
+ })
29
+
30
+ it('cy.reload() - reload the page', () => {
31
+ // https://on.cypress.io/reload
32
+ cy.reload()
33
+
34
+ // reload the page without using the cache
35
+ cy.reload(true)
36
+ })
37
+
38
+ it('cy.visit() - visit a remote url', () => {
39
+ // https://on.cypress.io/visit
40
+
41
+ // Visit any sub-domain of your current domain
42
+ // Pass options to the visit
43
+ cy.visit('https://example.cypress.io/commands/navigation', {
44
+ timeout: 50000, // increase total time for the visit to resolve
45
+ onBeforeLoad (contentWindow) {
46
+ // contentWindow is the remote page's window object
47
+ expect(typeof contentWindow === 'object').to.be.true
48
+ },
49
+ onLoad (contentWindow) {
50
+ // contentWindow is the remote page's window object
51
+ expect(typeof contentWindow === 'object').to.be.true
52
+ },
53
+ })
54
+ })
55
+ })
@@ -0,0 +1,163 @@
1
+ /// <reference types="cypress" />
2
+
3
+ context('Network Requests', () => {
4
+ beforeEach(() => {
5
+ cy.visit('https://example.cypress.io/commands/network-requests')
6
+ })
7
+
8
+ // Manage HTTP requests in your app
9
+
10
+ it('cy.request() - make an XHR request', () => {
11
+ // https://on.cypress.io/request
12
+ cy.request('https://jsonplaceholder.cypress.io/comments')
13
+ .should((response) => {
14
+ expect(response.status).to.eq(200)
15
+ // the server sometimes gets an extra comment posted from another machine
16
+ // which gets returned as 1 extra object
17
+ expect(response.body).to.have.property('length').and.be.oneOf([500, 501])
18
+ expect(response).to.have.property('headers')
19
+ expect(response).to.have.property('duration')
20
+ })
21
+ })
22
+
23
+ it('cy.request() - verify response using BDD syntax', () => {
24
+ cy.request('https://jsonplaceholder.cypress.io/comments')
25
+ .then((response) => {
26
+ // https://on.cypress.io/assertions
27
+ expect(response).property('status').to.equal(200)
28
+ expect(response).property('body').to.have.property('length').and.be.oneOf([500, 501])
29
+ expect(response).to.include.keys('headers', 'duration')
30
+ })
31
+ })
32
+
33
+ it('cy.request() with query parameters', () => {
34
+ // will execute request
35
+ // https://jsonplaceholder.cypress.io/comments?postId=1&id=3
36
+ cy.request({
37
+ url: 'https://jsonplaceholder.cypress.io/comments',
38
+ qs: {
39
+ postId: 1,
40
+ id: 3,
41
+ },
42
+ })
43
+ .its('body')
44
+ .should('be.an', 'array')
45
+ .and('have.length', 1)
46
+ .its('0') // yields first element of the array
47
+ .should('contain', {
48
+ postId: 1,
49
+ id: 3,
50
+ })
51
+ })
52
+
53
+ it('cy.request() - pass result to the second request', () => {
54
+ // first, let's find out the userId of the first user we have
55
+ cy.request('https://jsonplaceholder.cypress.io/users?_limit=1')
56
+ .its('body') // yields the response object
57
+ .its('0') // yields the first element of the returned list
58
+ // the above two commands its('body').its('0')
59
+ // can be written as its('body.0')
60
+ // if you do not care about TypeScript checks
61
+ .then((user) => {
62
+ expect(user).property('id').to.be.a('number')
63
+ // make a new post on behalf of the user
64
+ cy.request('POST', 'https://jsonplaceholder.cypress.io/posts', {
65
+ userId: user.id,
66
+ title: 'Cypress Test Runner',
67
+ body: 'Fast, easy and reliable testing for anything that runs in a browser.',
68
+ })
69
+ })
70
+ // note that the value here is the returned value of the 2nd request
71
+ // which is the new post object
72
+ .then((response) => {
73
+ expect(response).property('status').to.equal(201) // new entity created
74
+ expect(response).property('body').to.contain({
75
+ title: 'Cypress Test Runner',
76
+ })
77
+
78
+ // we don't know the exact post id - only that it will be > 100
79
+ // since JSONPlaceholder has built-in 100 posts
80
+ expect(response.body).property('id').to.be.a('number')
81
+ .and.to.be.gt(100)
82
+
83
+ // we don't know the user id here - since it was in above closure
84
+ // so in this test just confirm that the property is there
85
+ expect(response.body).property('userId').to.be.a('number')
86
+ })
87
+ })
88
+
89
+ it('cy.request() - save response in the shared test context', () => {
90
+ // https://on.cypress.io/variables-and-aliases
91
+ cy.request('https://jsonplaceholder.cypress.io/users?_limit=1')
92
+ .its('body').its('0') // yields the first element of the returned list
93
+ .as('user') // saves the object in the test context
94
+ .then(function () {
95
+ // NOTE 👀
96
+ // By the time this callback runs the "as('user')" command
97
+ // has saved the user object in the test context.
98
+ // To access the test context we need to use
99
+ // the "function () { ... }" callback form,
100
+ // otherwise "this" points at a wrong or undefined object!
101
+ cy.request('POST', 'https://jsonplaceholder.cypress.io/posts', {
102
+ userId: this.user.id,
103
+ title: 'Cypress Test Runner',
104
+ body: 'Fast, easy and reliable testing for anything that runs in a browser.',
105
+ })
106
+ .its('body').as('post') // save the new post from the response
107
+ })
108
+ .then(function () {
109
+ // When this callback runs, both "cy.request" API commands have finished
110
+ // and the test context has "user" and "post" objects set.
111
+ // Let's verify them.
112
+ expect(this.post, 'post has the right user id').property('userId').to.equal(this.user.id)
113
+ })
114
+ })
115
+
116
+ it('cy.intercept() - route responses to matching requests', () => {
117
+ // https://on.cypress.io/intercept
118
+
119
+ let message = 'whoa, this comment does not exist'
120
+
121
+ // Listen to GET to comments/1
122
+ cy.intercept('GET', '**/comments/*').as('getComment')
123
+
124
+ // we have code that gets a comment when
125
+ // the button is clicked in scripts.js
126
+ cy.get('.network-btn').click()
127
+
128
+ // https://on.cypress.io/wait
129
+ cy.wait('@getComment').its('response.statusCode').should('be.oneOf', [200, 304])
130
+
131
+ // Listen to POST to comments
132
+ cy.intercept('POST', '**/comments').as('postComment')
133
+
134
+ // we have code that posts a comment when
135
+ // the button is clicked in scripts.js
136
+ cy.get('.network-post').click()
137
+ cy.wait('@postComment').should(({ request, response }) => {
138
+ expect(request.body).to.include('email')
139
+ expect(request.headers).to.have.property('content-type')
140
+ expect(response && response.body).to.have.property('name', 'Using POST in cy.intercept()')
141
+ })
142
+
143
+ // Stub a response to PUT comments/ ****
144
+ cy.intercept({
145
+ method: 'PUT',
146
+ url: '**/comments/*',
147
+ }, {
148
+ statusCode: 404,
149
+ body: { error: message },
150
+ headers: { 'access-control-allow-origin': '*' },
151
+ delayMs: 500,
152
+ }).as('putComment')
153
+
154
+ // we have code that puts a comment when
155
+ // the button is clicked in scripts.js
156
+ cy.get('.network-put').click()
157
+
158
+ cy.wait('@putComment')
159
+
160
+ // our 404 statusCode logic in scripts.js executed
161
+ cy.get('.network-put-comment').should('contain', message)
162
+ })
163
+ })