-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs for experimental session API #3427
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
--- | ||
title: defineSession | ||
containerClass: experimental | ||
--- | ||
|
||
{% note warning %} | ||
{% fa fa-warning orange %} **This is an experimental feature. In order to use it, you must set the {% url "`experimentalSessionSupport`" experiments %} configuration option to `true`.** | ||
{% endnote %} | ||
|
||
Defines a `Session`. A `Session` a collection of `cookies` and `localStorage` across all domains that may be restored to at any point during a test with the accompanying {% url `cy.useSession()` useSession %} command. | ||
|
||
## Usage | ||
```ts | ||
cy.defineSession(name, steps, options?) | ||
cy.defineSession(options) | ||
``` | ||
|
||
## Arguments | ||
|
||
**{% fa fa-angle-right %} name** ***(String)*** | ||
The name of the `Session` to be referenced later within a {% url `cy.useSession()` useSession %} command. | ||
|
||
**{% fa fa-angle-right %} steps** ***(Function)*** | ||
A function containing the Cypress commands needed to set `Session` data. For example, logging into your application. | ||
|
||
**{% fa fa-angle-right %} options** ***(Object)*** | ||
|
||
Option | Default | Description | ||
--- | --- | --- | ||
`name` | `null` | The name of the session | ||
`steps` | `null` | A function containing the Cypress commands for intializing `Session` state | ||
`after` | `null` | A function that always runs **after** {% url `cy.useSession()` useSession %} for both new and saved `Sessions`. | ||
`before` | `null` | A function that always runs **before** {% url `cy.useSession()` useSession %} for both new and saved `Sessions`. | ||
`verify` | `null` | A function that, when returns false, invalidates and recreates the session during {% url `cy.useSession()` useSession %} | ||
|
||
## Yields {% helper_icon yields %} | ||
|
||
Unlike most Cypress commands, `cy.defineSession()` is *synchronous* and returns the `Session` reference instead of a Promise-like chain-able object. This allows you to assign the `Session` object and pass the reference directly to {% url `cy.useSession()` useSession %}. You may also place `cy.defineSession()` calls in a `cypress/support` and export the session objects for use in multiple spec files. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This maybe would be ok in code ticks since it is referring to an actual code reference 'Session' object. |
||
|
||
**Note:** You may also ignore the return value and reference the `Session` by name in {% url `cy.useSession()` useSession %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
--- | ||
title: useSession | ||
containerClass: experimental | ||
--- | ||
|
||
{% note warning %} | ||
{% fa fa-warning orange %} **This is an experimental feature. In order to use it, you must set the {% url "`experimentalSessionSupport`" experiments %} configuration option to `true`.** | ||
{% endnote %} | ||
|
||
|
||
Use `cy.useSession()` to set client-stored persisted data in your application. A `Session` consists of `cookies` and `localStorage` and is created with the accompanying {% url `cy.defineSession()` defineSession %} command. | ||
|
||
With `cy.useSession()`, you can: | ||
* set `cookies` and `localStorage` without use of `cy.request` or `cy.visit` before every test. | ||
* switch between multiple `Sessions` during a single test | ||
* inspect `Session` data that applied to test in the Command Log | ||
* quickly iterate as an authenticated user in `Interactive mode` without replaying login steps | ||
* keep the convenience and test coverage of UI based authentication while gaining the speed of programmatic login | ||
* gain massive reductions in spec run time. Since `Session` data is saved after the first time it's used,future tests that share a call to `useSession()` will use near-instant `Session` restoration without needing to perform slow UI actions. | ||
|
||
|
||
# Usage | ||
|
||
```ts | ||
cy.useSession(sessionReference) | ||
``` | ||
|
||
|
||
**Note:** when using `Sessions`, all cookies and localStorage will be cleared before every test *for all domains*. | ||
|
||
## Arguments | ||
|
||
### **{% fa fa-angle-right %} sessionReference** **_(`string | object`)_** | ||
|
||
Specifies the `Session` reference, as previously defined in {% url `cy.defineSession()` defineSession %}, to apply. This is either the name (`string`) of a `Session` or the return value of the {% url `cy.defineSession()` defineSession %} command. | ||
|
||
```ts | ||
cy.useSession('mySession') | ||
|
||
// OR | ||
|
||
const mySession = cy.defineSession({ | ||
name: 'mySession' | ||
steps () {...} | ||
}) | ||
|
||
cy.useSession(mySession) | ||
``` | ||
|
||
# Examples | ||
|
||
### `Session` applied in a `beforeEach` hook: | ||
```ts | ||
// can be placed inside spec or in a cypress/support file | ||
cy.defineSession({ | ||
name: 'myUser', | ||
steps () { | ||
cy.visit('/login') | ||
cy.get('.username').type('user') | ||
cy.get('.password').type('pass') | ||
cy.get('.login').click() | ||
} | ||
}) | ||
|
||
describe('suite', ()=>{ | ||
beforeEach(()=>{ | ||
cy.useSession('myUser') | ||
// useSession() always navigates you to a blank page | ||
cy.visit('...') | ||
}) | ||
|
||
it('test one', ()=>{ | ||
// beforeEach executed session steps | ||
// and saved the 'myUser' Session | ||
}) | ||
|
||
it('test two', ()=>{ | ||
// beforeEach did NOT execute session steps | ||
// and instantaneously restored saved 'myUser' session data | ||
}) | ||
}) | ||
``` | ||
|
||
### Switch users within a single test | ||
```ts | ||
it('admin can ban user', ()=> { | ||
cy.useSession('admin') | ||
cy.visit('/user/one') | ||
cy.get('.ban-user-button').click() | ||
// wait for the network request to be completed | ||
cy.get('.toast').contains('successfully banned user') | ||
|
||
cy.useSession('basicUser') | ||
cy.visit('/home') | ||
cy.contains('sorry, you can no longer access this site') | ||
}) | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I probably would not put
Session
in code ticks. It's just going to be complicated to keep up with and doesn't seem like it's strictly 'code' per se, more of a concept.