forked from nightwatchjs/nightwatch
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added some method for better selector strategy
- Loading branch information
1 parent
cefae71
commit 068a1ba
Showing
8 changed files
with
335 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* Returns input element related to label . The element will be returned as web element JSON object (with some convenience method). | ||
* | ||
* | ||
* @example | ||
* module.exports = { | ||
* 'demo Test': function(browser) { | ||
* const resultElement = await browser.findElementByLabel('username'); | ||
* | ||
* console.log('element Id:', resultElement.getId()); | ||
* }, | ||
* | ||
* @syntax browser.findElementByLabel(label_text, callback) | ||
* @syntax browser.findElementByLabel(label_text) | ||
* @param {string} label_text The label_text used to locate the input element. Can be a string | ||
* @param {object} {exact: true} Used to exact match of label_text | ||
* @param {function} callback Callback function which is called with the result value. | ||
* @method findElementByLabel | ||
* @since 2.0.0 | ||
* @api protocol.elements | ||
*/ | ||
|
||
const LocateStrategy = require('../../element/strategy.js'); | ||
const FindElement = require('./findElement.js'); | ||
|
||
class FindElementByLabel extends FindElement { | ||
get selector() { | ||
if (this.args[1] && this.args[1].exact === true) { | ||
return `//${this.__selector}[text()='${this.args[0]}']//following::input[1]`; | ||
} | ||
|
||
return `//${this.__selector}[contains(text(), '${this.args[0]}')]//following::input[1]`; | ||
} | ||
|
||
setStrategy() { | ||
this.__strategy = LocateStrategy.XPATH; | ||
|
||
return this; | ||
} | ||
|
||
setStrategyFromArgs() {} | ||
} | ||
|
||
module.exports = FindElementByLabel; |
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,43 @@ | ||
const LocateStrategy = require('../../element/strategy.js'); | ||
const FindElement = require('./findElement.js'); | ||
/** | ||
* Returns an element's first child. The child element will be returned as web element JSON object (with an added .getId() convenience method). | ||
* | ||
* | ||
* @example | ||
* module.exports = { | ||
* 'demo Test': function(browser) { | ||
* const resultElement = await browser.getFirstElementChild('.features-container'); | ||
* | ||
* console.log('last child element Id:', resultElement.getId()); | ||
* }, | ||
* | ||
* @syntax browser.getFirstElementChild(selector, callback) | ||
* @syntax browser.getFirstElementChild(selector) | ||
* @param {string} [using] The locator strategy to use. See [W3C Webdriver - locator strategies](https://www.w3.org/TR/webdriver/#locator-strategies) | ||
* @param {string|object} selector The selector (CSS/Xpath) used to locate the element. Can either be a string or an object which specifies [element properties](https://nightwatchjs.org/guide#element-properties). | ||
* @param {function} callback Callback function which is called with the result value. | ||
* @method findElementByLabel | ||
* @since 2.0.0 | ||
* @moreinfo developer.mozilla.org/en-US/docs/Web/API/Element/firstElementChild | ||
* @api protocol.elements | ||
*/ | ||
class FindElementByPlaceholderText extends FindElement { | ||
get selector() { | ||
if (this.args[1] && this.args[1].exact === true) { | ||
return `//input[@placeholder='${this.args[0]}']`; | ||
} | ||
|
||
return `//input[contains(@placeholder, '${this.args[0]}')]`; | ||
} | ||
|
||
setStrategy() { | ||
this.__strategy = LocateStrategy.XPATH; | ||
|
||
return this; | ||
} | ||
|
||
setStrategyFromArgs() {} | ||
} | ||
|
||
module.exports = FindElementByPlaceholderText; |
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,43 @@ | ||
const LocateStrategy = require('../../element/strategy.js'); | ||
const FindElement = require('./findElement.js'); | ||
/** | ||
* Returns an element's first child. The child element will be returned as web element JSON object (with an added .getId() convenience method). | ||
* | ||
* | ||
* @example | ||
* module.exports = { | ||
* 'demo Test': function(browser) { | ||
* const resultElement = await browser.getFirstElementChild('.features-container'); | ||
* | ||
* console.log('last child element Id:', resultElement.getId()); | ||
* }, | ||
* | ||
* @syntax browser.getFirstElementChild(selector, callback) | ||
* @syntax browser.getFirstElementChild(selector) | ||
* @param {string} [using] The locator strategy to use. See [W3C Webdriver - locator strategies](https://www.w3.org/TR/webdriver/#locator-strategies) | ||
* @param {string|object} selector The selector (CSS/Xpath) used to locate the element. Can either be a string or an object which specifies [element properties](https://nightwatchjs.org/guide#element-properties). | ||
* @param {function} callback Callback function which is called with the result value. | ||
* @method getFirstElementChild | ||
* @since 2.0.0 | ||
* @moreinfo developer.mozilla.org/en-US/docs/Web/API/Element/firstElementChild | ||
* @api protocol.elements | ||
*/ | ||
class FindElementByText extends FindElement { | ||
get selector() { | ||
if (this.args[1] && this.args[1].exact === true) { | ||
return `//${this.__selector}[text()='${this.args[0]}']`; | ||
} | ||
|
||
return `//${this.__selector}[contains(text(), '${this.args[0]}')]`; | ||
} | ||
|
||
setStrategy() { | ||
this.__strategy = LocateStrategy.XPATH; | ||
|
||
return this; | ||
} | ||
|
||
setStrategyFromArgs() {} | ||
} | ||
|
||
module.exports = FindElementByText; |
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,66 @@ | ||
/** | ||
* Search for an elements on the page, starting from the document root. The located element will be returned as web element JSON object (with an added .getId() convenience method). | ||
* First argument is the element selector, either specified as a string or as an object (with 'selector' and 'locateStrategy' properties). | ||
* | ||
* @example | ||
* module.exports = { | ||
* 'demo Test': function(browser) { | ||
* const resultElement = await browser.findElement('.features-container li:first-child'); | ||
* | ||
* console.log('Element Id:', resultElement.getId()); | ||
* }, | ||
* | ||
* | ||
* @link /#find-element | ||
* @syntax browser.findElement(selector, callback) | ||
* @syntax await browser.findElement(selector); | ||
* @param {string} selector The search target. | ||
* @param {function} [callback] Callback function to be invoked with the result when the command finishes. | ||
* @since 1.7.0 | ||
* @api protocol.elements | ||
*/ | ||
const Utils = require('../../utils/index.js'); | ||
const ProtocolAction = require('./_base-action.js'); | ||
|
||
|
||
module.exports = class FindElementMultipleORCriteria extends ProtocolAction { | ||
async command(...args) { | ||
let cssSelectors , xpathSelector, callback = function() {}; | ||
|
||
args.forEach((selector) => { | ||
if (Utils.isString(selector)) { | ||
if (Utils.isValidXpath(selector)){ | ||
xpathSelector = xpathSelector ? xpathSelector + " | " + selector : selector; | ||
} else { | ||
cssSelectors = cssSelectors ? cssSelectors + ", " + selector : selector; | ||
} | ||
} | ||
}); | ||
|
||
if (Utils.isFunction(args.at(-1))) { | ||
callback = args.at(-1); | ||
} | ||
|
||
let result = []; | ||
|
||
if (cssSelectors) { | ||
const cssRes = await this.api.findElements(cssSelectors, callback); | ||
|
||
if (!cssRes.error) { | ||
result = result.concat(cssRes); | ||
result = Object.assign(cssRes, result); | ||
} | ||
} | ||
|
||
if (xpathSelector) { | ||
const xpathRes = await this.api.findElements('xpath', xpathSelector, callback) | ||
|
||
if (!xpathRes.error) { | ||
result = result.concat(xpathRes); | ||
result = Object.assign(xpathRes, result); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
}; |
Oops, something went wrong.