UNPKG

@testim/testim-cli

Version:

Command line interface for running Testing on you CI

73 lines (60 loc) 2.77 kB
/** * Works just like execute, only you can use selectors to pass html elements to * the function you wish to execute in the browser. * * The function fn will receive every resolved selector as an array of html elements, * even if there is only one result, or no result. * These arrays are the first arguments the function fn receives. * If you pass an array of selectors, the resulting html element arrays are returned in the same order. * * All arguments you append after function fn are added as the arguments after the html arrays. * You can use any JSON value or a function as such an argument. * * <example> :selectorExecute.js client.selectorExecute("//div", function(divs, message) { return divs.length + message; }, " divs on the page").then(function(res) { console.log(res); // returns, for example, "68 divs on the page" }); client.selectorExecute(["//div", "=Read Post"], function(divs, links) { var message = 'There are '; message += divs.length + ' divs on the page'; message += ' and '; message += links.length + ' links with an link text "' + links[0].text + '"'; return message; }).then(function(res) { console.log(res); // returns, for example, "There are 68 divs on the page and 42 links with an link text 'Read Post'" }); * </example> * * @param {String|Array.<String>} selectors single selector or array of selectors * @param {Function} script function to get executed in the browser * @param {...*} [argument1,...,argumentN] arguments added to fn. Can be any JSON value or function * * @uses protocol/execute * @type action */ var ensureClientS = require('../helpers/ensureClientSideSelectorSupport'), createSelectorScript = require('../helpers/_createSelectorScript'), ErrorHandler = require('../utils/ErrorHandler'); module.exports = function selectorExecute(selector, script) { var args = Array.prototype.slice.call(arguments, 2); if(typeof selector === 'string') { selector = [selector]; } if (!Array.isArray(selector)) { throw new ErrorHandler.CommandError('Argument \'selector\' must be string or array of strings.'); } if (!/string|function/.test(typeof script)) { throw new ErrorHandler.CommandError('Argument \'script\' must be a function or string.'); } var fullScript = createSelectorScript.call(this, script, selector, args); return ensureClientS.call(this).execute(fullScript).then(function(res) { var result = res && res.value; if(result && result.message === 'NoSuchElement') { throw new ErrorHandler(7); } return result; }); };