@@ -268,9 +268,10 @@ command instead.
268
268
269
269
#### ` .invoke() `
270
270
271
- [ ` .invoke() ` ] ( /api/commands/invoke ) now throws an error if the function returns
272
- a promise. If you wish to call a method that returns a promise and wait for it
273
- to resolve, use [ ` .then() ` ] ( /api/commands/then ) instead of ` .invoke() ` .
271
+ The [ ` .invoke() ` ] ( /api/commands/invoke ) command now throws an error if the
272
+ function returns a promise. If you wish to call a method that returns a promise
273
+ and wait for it to resolve, use [ ` .then() ` ] ( /api/commands/then ) instead of
274
+ ` .invoke() ` .
274
275
275
276
``` diff
276
277
cy.wrap(myAPI)
@@ -295,7 +296,72 @@ assertions to their own chain. For example, rewrite
295
296
296
297
#### ` .should() `
297
298
298
- [ ` .should() ` ] ( /api/commands/should ) now throws an error if Cypress commands are
299
+ The [ ` .should() ` ] ( /api/commands/should ) assertion now throws an error if Cypress
300
+ commands are invoked from inside a ` .should() ` callback. This previously
301
+ resulted in unusual and undefined behavior. If you wish to execute a series of
302
+ commands on the yielded value, use` .then() ` instead.
303
+
304
+ ``` diff
305
+ cy.get('button')
306
+ - .should(($button) => {
307
+
308
+ })
309
+ + .then(api => api.makeARequest('http://example.com'))
310
+ .then(res => { ...handle response... })
311
+ ```
312
+
313
+ #### ` .within() `
314
+
315
+ The [ ` .within() ` ] ( /api/commands/within ) command now throws an error if it is
316
+ passed multiple elements as the subject. This previously resulted in
317
+ inconsistent behavior, where some commands would use all passed in elements,
318
+ some would use only the first and ignore the rest, and
319
+ [ ` .screenshot() ` ] ( /api/commands/screenshot ) would throw an error if used inside
320
+ a ` .within() ` block with multiple elements.
321
+
322
+ If you were relying on the old behavior, you have several options depending on
323
+ the desired result.
324
+
325
+ The simplest option is to reduce the subject to a single element.
326
+
327
+ ``` diff
328
+ cy.get('tr')
329
+ + .first() // Limit the subject to a single element before calling .within()
330
+ .within(() => {
331
+ cy.contains('Edit').click()
332
+ })
333
+ ```
334
+
335
+ If you have multiple subjects and wish to run commands over the collection as a
336
+ whole, you can alias the subject rather than use ` .within() ` .
337
+
338
+ ``` diff
339
+ cy.get('tr')
340
+ - .within(() => {
341
+ - cy.get('td').should('have.class', 'foo')
342
+ - cy.get('td').should('have.class', 'bar')
343
+ - })
344
+ + .as('rows') // Store multiple elements as an alias
345
+
346
+ + cy.get('@rows').find('td').should('have.class', 'foo')
347
+ + cy.get('@rows').find('td').should('have.class', 'bar')
348
+ ```
349
+
350
+ Or if you have a collection and want to run commands over every element, use
351
+ ` .each() ` in conjunction with ` .within() ` .
352
+
353
+ ``` diff
354
+ cy.get('tr')
355
+ - .within(() => {
356
+ - cy.contains('Edit').should('have.attr', 'disabled')
357
+ - })
358
+ + .each($tr => {
359
+ + cy.wrap($tr).within(() => {
360
+ + cy.contains('Edit').should('have.attr', 'disabled')
361
+ + })
362
+ + })
363
+ ```
364
+
299
365
invoked from inside a ` .should() ` callback. This previously resulted in unusual
300
366
and undefined behavior. If you wish to execute a series of commands on the
301
367
yielded value, use` .then() ` instead.
0 commit comments