Skip to content
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

Update auth examples for v12 on custom commands page #4894

Merged
merged 1 commit into from
Dec 5, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions content/api/cypress-api/custom-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,21 +173,33 @@ cy.getSessionStorage('token').should('eq', 'abc123')
#### Log in command using UI

```js
Cypress.Commands.add('typeLogin', (user) => {
cy.get('input[name=email]').type(user.email)

cy.get('input[name=password]').type(user.password)
Cypress.Commands.add('loginViaUi', (user) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Cypress.Commands.add('loginViaUi', (user) => {
Cypress.Commands.add('loginViaUI', (user) => {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cy.session(
user,
() => {
cy.visit('/login')
cy.get('input[name=email]').type(user.email)
cy.get('input[name=password]').type(user.password)
cy.click('button#login')
cy.get('h1').contains(`Welcome back ${user.name}!`)
},
{
validate: () => {
cy.getCookie('auth_key').should('exist')
},
}
)
})
```

```js
cy.typeLogin({ email: '[email protected]', password: 'Secret1' })
cy.loginViaUi({ email: '[email protected]', password: '$ecret1', name: 'johndoe' })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we also have a loginViaCyOrigin type example here as well that looks similar to the loginViaUI to help give users a pattern to follow?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would probably be a light version of what we have with the auth0/okta/cognito guides to show a general cy.origin pattern with cy.session

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cy.loginViaUi({ email: '[email protected]', password: '$ecret1', name: 'johndoe' })
cy.loginViaUI({ email: '[email protected]', password: '$ecret1', name: 'johndoe' })

```

#### Log in command using request

```javascript
Cypress.Commands.add('login', (userType, options = {}) => {
Cypress.Commands.add('loginViaApi', (userType, options = {}) => {
// this is an example of skipping your UI and logging in programmatically

// setup some basic types
Expand Down Expand Up @@ -232,10 +244,10 @@ Cypress.Commands.add('login', (userType, options = {}) => {

```javascript
// can start a chain off of cy
cy.login('admin')
cy.loginViaApi('admin')

// can be chained but will not receive the previous subject
cy.get('button').login('user')
cy.get('button').loginViaApi('user')
```

#### Log out command using UI
Expand All @@ -245,6 +257,8 @@ Cypress.Commands.add('logout', () => {
cy.contains('Login').should('not.exist')
cy.get('.avatar').click()
cy.contains('Logout').click()
cy.get('h1').contains('Login')
cy.getCookie('auth_key').should('not.exist')
})
```

Expand Down