cypress-xpath
Advanced tools
Comparing version 1.1.0 to 1.2.0
{ | ||
"name": "cypress-xpath", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Adds XPath command to Cypress test runner", | ||
@@ -5,0 +5,0 @@ "main": "src", |
@@ -28,2 +28,4 @@ # cypress-xpath [](https://circleci.com/gh/cypress-io/cypress-xpath) [![renovate-app badge][renovate-badge]][renovate-app] | ||
**note:** you can test XPath expressions from DevTools console using `$x(...)` function, for example `$x('//div')` to find all divs. | ||
See [cypress/integration/spec.js](cypress/integration/spec.js) | ||
@@ -34,3 +36,3 @@ | ||
- [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) | ||
@@ -37,0 +39,0 @@ - [ ] search from the previous subject element [#5](https://github.com/cypress-io/cypress-xpath/issues/5) |
121
src/index.js
@@ -16,3 +16,3 @@ /// <reference types="cypress" /> | ||
*/ | ||
const xpath = (selector) => { | ||
const xpath = (selector, options = {}) => { | ||
/* global XPathResult */ | ||
@@ -28,13 +28,19 @@ const isNumber = (xpathResult) => xpathResult.resultType === XPathResult.NUMBER_TYPE | ||
let nodes = [] | ||
const document = cy.state('window').document | ||
let iterator = document.evaluate(selector, document) | ||
const isPrimitive = (x) => | ||
Cypress._.isNumber(x) || Cypress._.isString(x) || Cypress._.isBoolean(x) | ||
if (isNumber(iterator)) { | ||
const result = numberResult(iterator) | ||
Cypress.log({ | ||
name: 'xpath', | ||
message: selector, | ||
$el: nodes, | ||
consoleProps () { | ||
// options to log later | ||
const log = { | ||
name: 'xpath', | ||
message: selector, | ||
} | ||
const getValue = () => { | ||
let nodes = [] | ||
const document = cy.state('window').document | ||
let iterator = document.evaluate(selector, document) | ||
if (isNumber(iterator)) { | ||
const result = numberResult(iterator) | ||
log.consoleProps = () => { | ||
return { | ||
@@ -45,14 +51,9 @@ 'XPath': selector, | ||
} | ||
}, | ||
}) | ||
return result | ||
} | ||
} | ||
return result | ||
} | ||
if (isString(iterator)) { | ||
const result = stringResult(iterator) | ||
Cypress.log({ | ||
name: 'xpath', | ||
message: selector, | ||
$el: nodes, | ||
consoleProps () { | ||
if (isString(iterator)) { | ||
const result = stringResult(iterator) | ||
log.consoleProps = () => { | ||
return { | ||
@@ -63,14 +64,9 @@ 'XPath': selector, | ||
} | ||
}, | ||
}) | ||
return result | ||
} | ||
} | ||
return result | ||
} | ||
if (isBoolean(iterator)) { | ||
const result = booleanResult(iterator) | ||
Cypress.log({ | ||
name: 'xpath', | ||
message: selector, | ||
$el: nodes, | ||
consoleProps () { | ||
if (isBoolean(iterator)) { | ||
const result = booleanResult(iterator) | ||
log.consoleProps = () => { | ||
return { | ||
@@ -81,35 +77,48 @@ 'XPath': selector, | ||
} | ||
}, | ||
}) | ||
return result | ||
} | ||
} | ||
return result | ||
} | ||
try { | ||
let node = iterator.iterateNext() | ||
try { | ||
let node = iterator.iterateNext() | ||
while (node) { | ||
nodes.push(node) | ||
node = iterator.iterateNext() | ||
while (node) { | ||
nodes.push(node) | ||
node = iterator.iterateNext() | ||
} | ||
log.consoleProps = () => { | ||
return { | ||
'XPath': selector, | ||
} | ||
} | ||
return nodes | ||
} catch (e) { | ||
console.error('Document tree modified during iteration', e) | ||
return null | ||
} | ||
} catch (e) { | ||
console.error('Document tree modified during iteration', e) | ||
} | ||
return null | ||
const resolveValue = () => { | ||
return Cypress.Promise.try(getValue).then(value => { | ||
return cy.verifyUpcomingAssertions(value, options, { | ||
onRetry: resolveValue, | ||
}) | ||
}) | ||
} | ||
// TODO set found elements on the command log? | ||
Cypress.log({ | ||
name: 'xpath', | ||
message: selector, | ||
$el: nodes, | ||
consoleProps () { | ||
return { | ||
'XPath': selector, | ||
} | ||
}, | ||
return resolveValue().then((value) => { | ||
// TODO set found elements on the command log? | ||
Cypress.log(log) | ||
if (isPrimitive(value)) { | ||
return value | ||
} | ||
return Cypress.$(value) | ||
}) | ||
return Cypress.$(nodes) | ||
} | ||
Cypress.Commands.add('xpath', xpath) |
6106
100
45