@testim/testim-cli
Version:
Command line interface for running Testing on you CI
55 lines (41 loc) • 1.75 kB
JavaScript
/**
*
* Send a sequence of key strokes to an element.
*
* @param {String} ID ID of a WebElement JSON object to route the command to
* @param {String|String[]} value The sequence of keys to type. An array must be provided. The server should flatten the array items to a single string to be typed.
*
* @see https://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/element/:id/value
* @type protocol
*
*/
var unicodeChars = require('../utils/unicodeChars'),
ErrorHandler = require('../utils/ErrorHandler.js');
module.exports = function elementIdValue (id, value) {
if(typeof id !== 'string' && typeof id !== 'number') {
throw new ErrorHandler.ProtocolError('number or type of arguments don\'t agree with elementIdValue protocol command');
}
var requestPath = '/session/:sessionId/element/:id/value',
key = [];
requestPath = requestPath.replace(/:id/gi, id);
if(typeof value === 'string') {
// replace key with corresponding unicode character
key = checkUnicode(value);
} else if(value instanceof Array) {
value.forEach(function(charSet) {
key = key.concat(checkUnicode(charSet));
});
} else {
throw new ErrorHandler.ProtocolError('number or type of arguments don\'t agree with elementIdValue protocol command');
}
var data = {'value': key};
return this.requestHandler.create(requestPath, data);
};
/*!
* check for unicode character or split string into literals
* @param {String} value text
* @return {Array} set of characters or unicode symbols
*/
function checkUnicode(value) {
return unicodeChars.hasOwnProperty(value) ? [unicodeChars[value]] : value.split('');
}