Skip to content
This repository has been archived by the owner on Oct 13, 2022. It is now read-only.

Commit

Permalink
feat: add retries, close #3
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Dec 6, 2018
1 parent 52b0470 commit e45ad8d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ See [cypress/integration/spec.js](cypress/integration/spec.js)
## Roadmap

- [x] wrap returned DOM nodes in jQuery [#2](https://github.com/cypress-io/cypress-xpath/issues/2)
- [ ] retry the assertion that follows [#3](https://github.com/cypress-io/cypress-xpath/issues/3)
- [x] retry the assertion that follows [#3](https://github.com/cypress-io/cypress-xpath/issues/3)
- [ ] add TypeScript definitions [#4](https://github.com/cypress-io/cypress-xpath/issues/4)
- [ ] search from the previous subject element [#5](https://github.com/cypress-io/cypress-xpath/issues/5)

Expand Down
2 changes: 1 addition & 1 deletion cypress/integration/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('cypress-xpath', () => {
cy.xpath('//h1/text()').its('0.textContent').should('equal', 'cypress-xpath')
})

it.skip('retries until element is inserted', () => {
it('retries until element is inserted', () => {
// the element will be inserted after 1 second
cy.xpath('string(//*[@id="inserted"])').should('equal', 'inserted text')
})
Expand Down
25 changes: 17 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
})
```
*/
const xpath = (selector) => {
const xpath = (selector, options = {}) => {
/* global XPathResult */
const isNumber = (xpathResult) => xpathResult.resultType === XPathResult.NUMBER_TYPE
const numberResult = (xpathResult) => xpathResult.numberValue
Expand Down Expand Up @@ -96,15 +96,24 @@ const xpath = (selector) => {
}
}

const nodes = getValue()
const resolveValue = () => {
return Cypress.Promise.try(getValue).then(value => {
return cy.verifyUpcomingAssertions(value, options, {
onRetry: resolveValue,
})
})
}

return resolveValue().then((value) => {
// TODO set found elements on the command log?
Cypress.log(log)
if (isPrimitive(value)) {
return value
}
return Cypress.$(value)
})

// TODO set found elements on the command log?
Cypress.log(log)

if (isPrimitive(nodes)) {
return nodes
}
return Cypress.$(nodes)
}

Cypress.Commands.add('xpath', xpath)

0 comments on commit e45ad8d

Please sign in to comment.