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

feat: cy.else can take a message to log #77

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ cy.get('#enrolled')
.check()
```

You can print a message if the `ELSE` branch is taken

```js
cy.get('...').if('...').else().log('a message')
// same as
cy.get('...').if('...').else('a message')
```

## Multiple commands

Sometimes it makes sense to place the "if" or "else" commands into `.then()` block
Expand Down
6 changes: 6 additions & 0 deletions cypress/e2e/else.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@ import '../../src'
describe('else branch', () => {
it('takes the if branch', () => {
cy.wrap(42).if('equal', 42).log('if branch').else().log('else branch')
cy.log('**built-in log**')
cy.wrap(42).if('equal', 42).log('if branch').else('else branch')
})

it('takes the else branch', () => {
cy.wrap(42).if('equal', 1).log('if branch').else().log('else branch')
cy.log('**built-in log**')
cy.wrap(42).if('equal', 1).log('if branch').else('else branch')
cy.log('**prints numbers**')
cy.wrap(42).if('equal', 1).log('if branch').else(42)
})

it('can have multiple if-else', () => {
Expand Down
5 changes: 4 additions & 1 deletion src/index-v11.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,15 @@ Cypress.Commands.add(
},
)

Cypress.Commands.add('else', { prevSubject: true }, (subject) => {
Cypress.Commands.add('else', { prevSubject: true }, (subject, text) => {
debug('else command, subject', subject)
if (typeof subject === 'undefined') {
// find the subject from the "if()" before
subject = findMyIfSubject(cy.state('current').attributes)
}
if (typeof text !== undefined) {
cy.log(text)
}
if (subject) {
cy.wrap(subject, { log: false })
}
Expand Down
9 changes: 8 additions & 1 deletion src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,19 @@ declare namespace Cypress {
* execute if the previous `.if()` command skipped
* the "IF" branch. Note: `.if()` passes its subject
* to the `.else()`
* You can also print a message if the ELSE branch
* is taken
* @param message Message to print to the console. Optional.
* @example
* cy.get('checkox#agree')
* .if('checked').log('Already agreed')
* .else().check()
* @example
* cy.get('...')
* .if('not.visible').log('Not visible')
* .else('visible')
*/
else(): Chainable<any>
else(message?: any): Chainable<any>

/**
* Finishes if/else commands and continues
Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,15 @@ if (major < 12) {
},
)

Cypress.Commands.add('else', { prevSubject: true }, (subject) => {
Cypress.Commands.add('else', { prevSubject: true }, (subject, text) => {
debug('else command, subject', subject)
if (typeof subject === 'undefined') {
// find the subject from the "if()" before
subject = findMyIfSubject(cy.state('current').attributes)
}
if (typeof text !== undefined) {
cy.log(text)
}
if (subject) {
cy.wrap(subject, { log: false })
}
Expand Down