From f4477adfd13b39ef10ce780687c2ad96e6514601 Mon Sep 17 00:00:00 2001 From: Jonathan Lipps Date: Tue, 3 Dec 2013 16:47:47 -0800 Subject: [PATCH] fail sooner if user passes in a bad locator strategy (fix #1539) --- lib/server/controller.js | 17 +++++++++++++---- lib/server/responses.js | 17 +++++++++++++++++ test/functional/apidemos/findElement.js | 9 +++++++++ test/functional/testapp/findElement.js | 3 ++- 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/lib/server/controller.js b/lib/server/controller.js index 92f93024d..1ea1b7222 100644 --- a/lib/server/controller.js +++ b/lib/server/controller.js @@ -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) { diff --git a/lib/server/responses.js b/lib/server/responses.js index 278e96b6a..5252191ab 100644 --- a/lib/server/responses.js +++ b/lib/server/responses.js @@ -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, { diff --git a/test/functional/apidemos/findElement.js b/test/functional/apidemos/findElement.js index 785bad9c1..bb8cde41f 100644 --- a/test/functional/apidemos/findElement.js +++ b/test/functional/apidemos/findElement.js @@ -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) { diff --git a/test/functional/testapp/findElement.js b/test/functional/testapp/findElement.js index 1f282365d..5452abae7 100644 --- a/test/functional/testapp/findElement.js +++ b/test/functional/testapp/findElement.js @@ -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(); }); });