fail sooner if user passes in a bad locator strategy (fix #1539)

This commit is contained in:
Jonathan Lipps
2013-12-03 16:47:47 -08:00
parent 1181cbaa49
commit f4477adfd1
4 changed files with 41 additions and 5 deletions

View File

@@ -13,6 +13,7 @@ var status = require('./status.js')
, respondError = responses.respondError
, respondSuccess = responses.respondSuccess
, checkMissingParams = responses.checkMissingParams
, checkValidLocStrat = responses.checkValidLocStrat
, notYetImplemented = responses.notYetImplemented
, _ = require('underscore');
@@ -211,7 +212,9 @@ exports.findElements = function(req, res) {
, selector = req.body.value;
if (checkMissingParams(res, {strategy: strategy, selector: selector}, true)) {
req.device.findElements(strategy, selector, getResponseHandler(req, res));
if (checkValidLocStrat(req, res, strategy)) {
req.device.findElements(strategy, selector, getResponseHandler(req, res));
}
}
};
@@ -220,7 +223,9 @@ exports.findElement = function(req, res) {
, selector = req.body.value;
if (checkMissingParams(res, {strategy: strategy, selector: selector}, true)) {
req.device.findElement(strategy, selector, getResponseHandler(req, res));
if (checkValidLocStrat(req, res, strategy)) {
req.device.findElement(strategy, selector, getResponseHandler(req, res));
}
}
};
@@ -229,7 +234,9 @@ exports.findElementFromElement = function(req, res) {
, strategy = req.body.using
, selector = req.body.value;
req.device.findElementFromElement(element, strategy, selector, getResponseHandler(req, res));
if (checkValidLocStrat(req, res, strategy)) {
req.device.findElementFromElement(element, strategy, selector, getResponseHandler(req, res));
}
};
exports.findElementsFromElement = function(req, res) {
@@ -237,7 +244,9 @@ exports.findElementsFromElement = function(req, res) {
, strategy = req.body.using
, selector = req.body.value;
req.device.findElementsFromElement(element, strategy, selector, getResponseHandler(req, res));
if (checkValidLocStrat(req, res, strategy)) {
req.device.findElementsFromElement(element, strategy, selector, getResponseHandler(req, res));
}
};
exports.setValue = function(req, res) {

View File

@@ -135,6 +135,23 @@ exports.checkMissingParams = function(res, params, strict) {
}
};
exports.checkValidLocStrat = function(req, res, strat) {
var validStrats = [
'tag name',
'xpath',
'id',
'name'
];
if (!_.contains(validStrats, strat)) {
logger.info("Invalid locator strategy: " + strat);
respondError(req, res, status.codes.UnknownCommand.code, {message: "Invalid locator strategy: " + strat});
return false;
} else {
return true;
}
};
var notYetImplemented = exports.notYetImplemented = function(req, res) {
logger.info("Responding to client that a method is not implemented");
res.send(501, {

View File

@@ -249,6 +249,15 @@ describeWd('xpath', function(h) {
});
});
});
it('should get an error when strategy doesnt exist', function(done) {
h.driver.elementByCss('button', function(err, el) {
should.exist(err);
should.not.exist(el);
err.status.should.eql(9);
err.cause.value.message.should.equal("Invalid locator strategy: css selector");
done();
});
});
});
describeWd('unallowed tag names', function(h) {

View File

@@ -49,7 +49,8 @@ describeWd('elementByTagName', function(h) {
h.driver.elementByCss('button', function(err, el) {
should.exist(err);
should.not.exist(el);
err.status.should.eql(13);
err.status.should.eql(9);
err.cause.value.message.should.equal("Invalid locator strategy: css selector");
done();
});
});