Skip to content

Commit

Permalink
feat: add waiting page
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Apr 23, 2020
1 parent fdf3e51 commit 9002ed5
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 8 deletions.
4 changes: 4 additions & 0 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ module.exports = {
text: 'Connectors',
link: '/commands/connectors/',
},
{
text: 'Waiting',
link: '/commands/waiting/',
},
{
text: 'Spies, Stubs & Clocks',
link: '/commands/spies_stubs_clocks/',
Expand Down
78 changes: 78 additions & 0 deletions docs/commands/waiting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Waiting

Examples of waiting for an amount of time or resource to resolve in Cypress, for a full reference of commands, go to [docs.cypress.io](https://on.cypress.io/api)

## [cy.wait()](https://on.cypress.io/wait)

To wait for a specific amount of time or resource to resolve, use the `cy.wait()` command.

<!-- fiddle cy.wait() / wait for a specific amount of time-->

```html
<form>
<div class="form-group">
<input type="text" class="form-control wait-input1" />
</div>
<div class="form-group">
<input type="text" class="form-control wait-input2" />
</div>
<div class="form-group">
<input type="text" class="form-control wait-input3" />
</div>
</form>
```

```js
cy.get('.wait-input1').type('Wait 1000ms after typing')
cy.wait(1000)
cy.get('.wait-input2').type('Wait 1000ms after typing')
cy.wait(1000)
cy.get('.wait-input3').type('Wait 1000ms after typing')
cy.wait(1000)
```

<!-- fiddle-end -->

You can wait for a specific route

<!-- fiddle cy.wait() / waiting for specific route -->

```html
<button class="network-btn btn btn-primary">Get Comment</button>
<div class="network-comment"></div>
<script>
// we fetch all data from this REST json backend
const root = 'https://jsonplaceholder.cypress.io'
function getComment() {
$.ajax({
url: `${root}/comments/1`,
method: 'GET',
}).then(function (data) {
$('.network-comment').text(data.body)
})
}
$('.network-btn').on('click', function (e) {
e.preventDefault()
getComment(e)
})
</script>
```

```js
cy.server()

// Listen to GET to comments/1
cy.route('GET', 'comments/*').as('getComment')

// we have code that gets a comment when
// the button is clicked in scripts.js
cy.get('.network-btn').click()

// wait for GET comments/1
cy.wait('@getComment').its('status').should('eq', 200)
```

<!-- fiddle-end -->

**Tip:** be careful of adding unnecessary wait times, see our [Best Practices: Unnecessary Waiting](https://on.cypress.io/best-practices#Unnecessary-Waiting) guide.
6 changes: 6 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ Commands drive your tests in the browser like a real user would. They let you pe
- [Actions](./commands/actions.md)
- [type](./commands/actions.md#type)
- [focus](./commands/actions.md#focus)
- [Window](./commands/window.md)
- [window](./commands/window.md#cy-window)
- [document](./commands/document.md#cy-document)
- [title](./commands/title.md#cy-title)
- [Navigation](./commands/navigation.md)
- [go](./commands/navigation.md#cy-go)
- [reload](./commands/navigation.md#cy-reload)
- [visit](./commands/navigation.md#cy-visit)
- [Waiting](./commands/waiting.md)
- [wait](./commands/waiting.md#cy-wait)
- [Connectors](./commands/connectors.md)
- [then](./commands/connectors.md#then)
- [Spies, Stubs & Clocks](./commands/spies_stubs_clocks.md)
Expand Down
9 changes: 5 additions & 4 deletions md-to-js.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ then
fi

echo "Each spec file will visit $BASE_URL before tests in each spec"
npx export-fiddle docs/commands/querying.md --before $BASE_URL/commands/querying
npx export-fiddle docs/commands/traversal.md --before $BASE_URL/commands/traversal
npx export-fiddle docs/commands/actions.md --before $BASE_URL/commands/actions
npx export-fiddle docs/commands/window.md --before $BASE_URL/commands/window
npx export-fiddle docs/commands/navigation.md --before $BASE_URL
npx export-fiddle docs/commands/connectors.md --before $BASE_URL/commands/connectors
npx export-fiddle docs/commands/navigation.md --before $BASE_URL
npx export-fiddle docs/commands/querying.md --before $BASE_URL/commands/querying
npx export-fiddle docs/commands/spies_stubs_clocks.md --before $BASE_URL/commands/spies_stubs_clocks
npx export-fiddle docs/commands/traversal.md --before $BASE_URL/commands/traversal
npx export-fiddle docs/commands/waiting.md --before $BASE_URL/commands/waiting
npx export-fiddle docs/commands/window.md --before $BASE_URL/commands/window
npx export-fiddle docs/cypress-api/index.md --before $BASE_URL/cypress-api
npx export-fiddle docs/utilities/index.md --before $BASE_URL/utilities

Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
},
"homepage": "https://github.com/bahmutov/cypress-examples#readme",
"devDependencies": {
"@cypress/fiddle": "1.17.0",
"@cypress/fiddle": "1.18.0",
"cypress": "4.4.0",
"husky": "4.2.5",
"lint-staged": "10.1.3",
Expand Down

0 comments on commit 9002ed5

Please sign in to comment.