-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update and expand the custom command recipe (#540)
- Loading branch information
Showing
18 changed files
with
190 additions
and
28 deletions.
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
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,13 @@ | ||
# add custom command in TypeScript | ||
> Write your own Cypress commands using TypeScript | ||
In this recipe we do not use `tsconfig.json` file, thus we need to specify additional command types using `/// <reference ...>` commands, like this: | ||
|
||
```js | ||
// cypress/integration/spec.ts | ||
/// <reference path="../support/index.d.ts" /> | ||
``` | ||
|
||
 | ||
|
||
See [cypress/support/index.ts](cypress/support/index.ts) and [cypress/support/index.d.ts](cypress/support/index.d.ts). |
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,6 @@ | ||
{ | ||
"viewportHeight": 200, | ||
"viewportWidth": 300, | ||
"pluginsFile": false, | ||
"fixturesFolder": false | ||
} |
22 changes: 22 additions & 0 deletions
22
examples/fundamentals__add-custom-command-ts/cypress/integration/async-command.ts
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,22 @@ | ||
// by loading custom commands from the support file, | ||
// we also load the global Cypress type definition | ||
/// <reference path="../support/index.d.ts" /> | ||
|
||
Cypress.Commands.add('asyncAdd', (a, b) => { | ||
cy.log(`${a} + ${b}`) | ||
// our application in "index.html" has placed a promise-returning | ||
// method "asyncAdd" onto the "window" object. | ||
// from the tests's custom command we can invoke that method | ||
// Cypress automatically waits for the promises to resolve | ||
// before yielding their value to the next command in the test | ||
// https://on.cypress.io/invoke | ||
cy.window().invoke('asyncAdd', a, b) | ||
}) | ||
|
||
describe('example', () => { | ||
it('adds numbers using custom command', () => { | ||
cy.visit('index.html') | ||
// the custom command will yield resolved value | ||
cy.asyncAdd(2, 3).should('equal', 5) | ||
}) | ||
}) |
34 changes: 34 additions & 0 deletions
34
examples/fundamentals__add-custom-command-ts/cypress/integration/spec.ts
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,34 @@ | ||
// load the type definition for new command we are adding "cy.dataCy" | ||
// note: this definition also will load regular Cypress definition | ||
// because index.d.ts references global "cypress" types | ||
/// <reference path="../support/index.d.ts" /> | ||
// after that the custom commands like "cy.dataCy" | ||
// should be recognized | ||
|
||
describe('finds', () => { | ||
it('element using data-cy custom command', () => { | ||
cy.visit('index.html') | ||
// use custom command we have defined above | ||
cy.dataCy('greeting').should('be.visible') | ||
}) | ||
|
||
it('element using h1', () => { | ||
cy.visit('index.html') | ||
// sanity check that h1 element has "data-cy" attribute | ||
// with expected value | ||
cy.get('h1') | ||
.should('be.visible') | ||
.and('have.attr', 'data-cy', 'greeting') | ||
}) | ||
|
||
it('dynamically added element', () => { | ||
cy.visit('index.html') | ||
// another custom command, this one comes from external module | ||
// load https://github.com/NoriSte/cypress-wait-until | ||
cy.waitUntil(() => { | ||
return cy | ||
.window() | ||
.then((win) => Boolean(win.document.querySelector('[data-cy=dynamic]'))) | ||
}) | ||
}) | ||
}) |
21 changes: 21 additions & 0 deletions
21
examples/fundamentals__add-custom-command-ts/cypress/support/index.d.ts
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,21 @@ | ||
// describe custom Cypress commands in this file | ||
|
||
// load the global Cypress types | ||
/// <reference types="cypress" /> | ||
// load the 3rd party command definition for cy.waitUntil() | ||
/// <reference types="cypress-wait-until" /> | ||
|
||
declare namespace Cypress { | ||
interface Chainable { | ||
/** | ||
* Custom command to select DOM element by data-cy attribute. | ||
* @example cy.dataCy('greeting') | ||
*/ | ||
dataCy(value: string): Chainable<Element> | ||
|
||
/** | ||
* Custom command that adds two given numbers | ||
*/ | ||
asyncAdd(a: number, b: number): Chainable<number> | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
examples/fundamentals__add-custom-command-ts/cypress/support/index.ts
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,19 @@ | ||
// load the global Cypress types | ||
/// <reference types="cypress" /> | ||
// load the 3rd party command definition for cy.waitUntil() | ||
/// <reference types="cypress-wait-until" /> | ||
|
||
// load https://github.com/NoriSte/cypress-wait-until | ||
// which adds "cy.waitUntil" command | ||
// note that this 3rd party module includes TypeScript "types" | ||
// file that correctly adds "waitUntil" to the Cypress Chainer namespace | ||
require('cypress-wait-until') | ||
|
||
/** | ||
* Adds custom command "cy.dataCy" to the global "cy" object | ||
* | ||
* @example cy.dataCy('greeting') | ||
*/ | ||
Cypress.Commands.add('dataCy', (value) => cy.get(`[data-cy=${value}]`)) | ||
|
||
// the types for cy.dataCy will be defined in "index.d.ts" file |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,26 @@ | ||
<body> | ||
<main> | ||
<h1 data-cy="greeting">Page title</h1> | ||
<p>This is a simple page with a <pre>data-cy="greeting"</pre> element.</p> | ||
</main> | ||
<script> | ||
// adds another data-cy element dynamically | ||
setTimeout(() => { | ||
let el = document.createElement('div') | ||
el.setAttribute('data-cy', 'dynamic') | ||
el.innerText = 'This element was added dynamically' | ||
document.body.appendChild(el) | ||
}, 1000) | ||
</script> | ||
|
||
<script> | ||
// method is added to the "window" object dynamically | ||
setTimeout(() => { | ||
window.asyncAdd = (a, b) => { | ||
return new Promise((resolve) => { | ||
setTimeout(() => resolve(a + b), 1000) | ||
}) | ||
} | ||
}, 1000) | ||
</script> | ||
</body> |
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,11 @@ | ||
{ | ||
"name": "add-custom-command-ts", | ||
"version": "1.0.0", | ||
"description": "Write your own Cypress commands in TypeScript", | ||
"scripts": { | ||
"cypress:open": "../../node_modules/.bin/cypress open", | ||
"cypress:run": "../../node_modules/.bin/cypress run", | ||
"test:ci": "../../node_modules/.bin/cypress run", | ||
"test:ci:record": "../../node_modules/.bin/cypress run --record" | ||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
{ | ||
"viewportHeight": 200, | ||
"viewportWidth": 300, | ||
"pluginsFile": false | ||
} | ||
"pluginsFile": false, | ||
"fixturesFolder": false | ||
} |
22 changes: 0 additions & 22 deletions
22
examples/fundamentals__add-custom-command/cypress/README.md
This file was deleted.
Oops, something went wrong.
5 changes: 4 additions & 1 deletion
5
examples/fundamentals__add-custom-command/cypress/integration/async-command.js
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.