Skip to content
This repository was archived by the owner on Feb 28, 2023. It is now read-only.

Commit 6fedf91

Browse files
authored
add alias slides (#256)
1 parent 2760d23 commit 6fedf91

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

cypress/integration/11-retry-ability/answer.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,37 @@ describe('Careful with negative assertions', { retries: 2 }, () => {
202202
cy.get('.loading').should('not.be.visible')
203203
})
204204
})
205+
206+
describe('aliases', () => {
207+
context('are reset before each test', () => {
208+
before(() => {
209+
cy.wrap('some value').as('exampleValue')
210+
})
211+
212+
it('works in the first test', () => {
213+
cy.get('@exampleValue').should('equal', 'some value')
214+
})
215+
216+
// NOTE the second test is failing because the alias is reset
217+
it.skip('does not exist in the second test', () => {
218+
// there is not alias because it is created once before
219+
// the first test, and is reset before the second test
220+
cy.get('@exampleValue').should('equal', 'some value')
221+
})
222+
})
223+
224+
context('should be created before each test', () => {
225+
beforeEach(() => {
226+
// we will create a new alias before each test
227+
cy.wrap('some value').as('exampleValue')
228+
})
229+
230+
it('works in the first test', () => {
231+
cy.get('@exampleValue').should('equal', 'some value')
232+
})
233+
234+
it('works in the second test', () => {
235+
cy.get('@exampleValue').should('equal', 'some value')
236+
})
237+
})
238+
})

slides/11-retry-ability/PITCHME.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- deep dive into assertions
66
- built-in command waits
77
- retry-ability 🔑
8+
- aliases
89

910
+++
1011

@@ -471,7 +472,56 @@ cy.get('@some-method')
471472
.should('have.been.calledOnce)
472473
```
473474
475+
---
476+
## Aliases
477+
478+
Values and DOM elements can be saved under an alias using [.as](https://on.cypress.io/as) command.
479+
480+
Read the guide at [https://on.cypress.io/variables-and-aliases](https://on.cypress.io/variables-and-aliases)
481+
482+
+++
483+
484+
```js
485+
before(() => {
486+
cy.wrap('some value').as('exampleValue')
487+
})
488+
489+
it('works in the first test', () => {
490+
cy.get('@exampleValue').should('equal', 'some value')
491+
})
492+
493+
// NOTE the second test is failing because the alias is reset
494+
it('does not exist in the second test', () => {
495+
cy.get('@exampleValue').should('equal', 'some value')
496+
})
497+
```
498+
499+
**Note** aliases are reset before each test
500+
474501
+++
502+
503+
![Failing second test due to an alias defined in before hook](./img/alias-does-not-exist.png)
504+
505+
+++
506+
507+
**Solution:** create aliases using `beforeEach` hook
508+
509+
```js
510+
beforeEach(() => {
511+
// we will create a new alias before each test
512+
cy.wrap('some value').as('exampleValue')
513+
})
514+
515+
it('works in the first test', () => {
516+
cy.get('@exampleValue').should('equal', 'some value')
517+
})
518+
519+
it('works in the second test', () => {
520+
cy.get('@exampleValue').should('equal', 'some value')
521+
})
522+
```
523+
524+
---
475525
## 📝 Take away
476526
477527
Most commands have built-in sensible waits:
108 KB
Loading

slides/intro/PITCHME.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
- [github.com/cypress-io/testing-workshop-cypress](https://github.com/cypress-io/testing-workshop-cypress)
44

5+
Jump to: [00-start](?p=00-start), [01-basic](?p=01-basic), [02-adding-items](?p=02-adding-items), [03-selector-playground](?p=03-selector-playground), [04-reset-state](?p=04-reset-state), [05-xhr](?p=05-xhr), [06-app-data-store](?p=06-app-data-store), [07-ci](?p=07-ci), [08-dashboard](?p=08-dashboard), [09-reporters](?p=09-reporters), [10-configuration](?p=10-configuration), [11-retry-ability](?p=11-retry-ability)
6+
57
+++
68
## Gleb Bahmutov, PhD
79

0 commit comments

Comments
 (0)