From 4a2e660f888c7bc8224f586b19014e393d5da9f9 Mon Sep 17 00:00:00 2001 From: christian-bromann Date: Sun, 10 Aug 2014 23:59:51 -0700 Subject: [PATCH] added isEnabled and elementIdEnabled commands --- lib/commands/isEnabled.js | 95 ++++++++++++++++++++++++++++++++ lib/protocol/elementIdEnabled.js | 31 +++++++++++ 2 files changed, 126 insertions(+) create mode 100644 lib/commands/isEnabled.js create mode 100644 lib/protocol/elementIdEnabled.js diff --git a/lib/commands/isEnabled.js b/lib/commands/isEnabled.js new file mode 100644 index 00000000000..326cb91408b --- /dev/null +++ b/lib/commands/isEnabled.js @@ -0,0 +1,95 @@ +/** + * + * Return true or false if the selected DOM-element found by given selector is enabled. + * + * + :index.html + + + + + :isVisible.js + client + .isEnabled('.input1', function(err, isEnabled) { + console.log(isEnabled); // outputs: true + }) + .isEnabled('#input2', function(err, isEnabled) { + console.log(isEnabled); // outputs: false + }) + .isEnabled('#input3', function(err, isEnabled) { + console.log(isEnabled); // outputs: false + }) + * + * + * @param {String} selector DOM-element + * @returns {Boolean|Boolean[]} true if element(s)* (is|are) visible + * + * @uses protocol/elements, protocol/elementIdEnabled + * @type state + * + */ + +var async = require('async'), + ErrorHandler = require('../utils/ErrorHandler.js'); + +module.exports = function isEnabled (selector) { + + /*! + * make sure that callback contains chainit callback + */ + var callback = arguments[arguments.length - 1]; + + /*! + * parameter check + */ + if(typeof selector !== 'string') { + return callback(new ErrorHandler.CommandError('number or type of arguments don\'t agree with isEnabled command')); + } + + var self = this, + response = {}; + + async.waterfall([ + function(cb) { + self.elements(selector, cb); + }, + function(res, cb) { + response.elements = res; + response.elementIdEnabled = []; + + if(res.value.length === 0) { + // throw NoSuchElement error if no element was found + return callback(new ErrorHandler(7)); + } + + async.eachSeries(res.value, function(val, seriesCallback) { + self.elementIdEnabled(val.ELEMENT, function(err,res) { + if(res) { + response.elementIdEnabled.push(res); + } + + seriesCallback(err); + }); + }, cb); + } + ], function(err) { + + var value = null; + + if(response.elementIdEnabled && response.elementIdEnabled.length === 1) { + + value = response.elementIdEnabled[0].value; + + } else if(response.elementIdEnabled && response.elementIdEnabled.length > 1) { + + value = response.elementIdEnabled.map(function(res) { + return res.value; + }); + + } + + callback(err, value || false, response); + + }); + +}; \ No newline at end of file diff --git a/lib/protocol/elementIdEnabled.js b/lib/protocol/elementIdEnabled.js new file mode 100644 index 00000000000..9d2e033f416 --- /dev/null +++ b/lib/protocol/elementIdEnabled.js @@ -0,0 +1,31 @@ +/** + * + * Determine if an element is currently enabled. + * + * @param {String} ID ID of a WebElement JSON object to route the command to + * @returns {Boolean} true if the element is enabled + * + * @see https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/element/:id/enabled + * @type protocol + * + */ + +var ErrorHandler = require('../utils/ErrorHandler.js'); + +module.exports = function elementIdEnabled (id) { + + /*! + * make sure that callback contains chainit callback + */ + var callback = arguments[arguments.length - 1]; + + if(typeof id !== 'string' && typeof id !== 'number') { + return callback(new ErrorHandler.ProtocolError('number or type of arguments don\'t agree with elementIdEnabled protocol command')); + } + + this.requestHandler.create( + '/session/:sessionId/element/:id/enabled'.replace(/:id/gi, id), + callback + ); + +};