Skip to content

Commit

Permalink
added isEnabled and elementIdEnabled commands
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann committed Aug 11, 2014
1 parent 3b93c76 commit 4a2e660
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
95 changes: 95 additions & 0 deletions lib/commands/isEnabled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
*
* Return true or false if the selected DOM-element found by given selector is enabled.
*
* <example>
:index.html
<input type="text" name="inputField" class="input1">
<input type="text" name="inputField" class="input2" disabled>
<input type="text" name="inputField" class="input3" disabled="disabled">
: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
})
* </example>
*
* @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);

});

};
31 changes: 31 additions & 0 deletions lib/protocol/elementIdEnabled.js
Original file line number Diff line number Diff line change
@@ -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
);

};

0 comments on commit 4a2e660

Please sign in to comment.