Commit dd84d1ab dd84d1ab1d826a0ed09f3310d961c70a663dc9c8 by Nicolas Perriault

added BC way to use XPath selectors in clientutils

1 parent c3db0ced
...@@ -47,6 +47,7 @@ ...@@ -47,6 +47,7 @@
47 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 47 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
48 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 48 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
49 ); 49 );
50 var SUPPORTED_SELECTOR_TYPES = ['css', 'xpath'];
50 51
51 /** 52 /**
52 * Clicks on the DOM element behind the provided selector. 53 * Clicks on the DOM element behind the provided selector.
...@@ -241,10 +242,15 @@ ...@@ -241,10 +242,15 @@
241 * @return NodeList|undefined 242 * @return NodeList|undefined
242 */ 243 */
243 this.findAll = function findAll(selector) { 244 this.findAll = function findAll(selector) {
245 var pSelector = this.processSelector(selector);
244 try { 246 try {
245 return document.querySelectorAll(selector); 247 if (pSelector.type === 'xpath') {
248 return this.getElementsByXPath(pSelector.path);
249 } else {
250 return document.querySelectorAll(pSelector.path);
251 }
246 } catch (e) { 252 } catch (e) {
247 this.log('findAll(): invalid selector provided "' + selector + '":' + e, "error"); 253 this.log('findAll(): invalid selector provided "' + pSelector.toString() + '":' + e, "error");
248 } 254 }
249 }; 255 };
250 256
...@@ -255,8 +261,13 @@ ...@@ -255,8 +261,13 @@
255 * @return HTMLElement|undefined 261 * @return HTMLElement|undefined
256 */ 262 */
257 this.findOne = function findOne(selector) { 263 this.findOne = function findOne(selector) {
264 var pSelector = this.processSelector(selector);
258 try { 265 try {
259 return document.querySelector(selector); 266 if (pSelector.type === 'xpath') {
267 return this.getElementByXPath(pSelector.path);
268 } else {
269 return document.querySelector(pSelector.path);
270 }
260 } catch (e) { 271 } catch (e) {
261 this.log('findOne(): invalid selector provided "' + selector + '":' + e, "errors"); 272 this.log('findOne(): invalid selector provided "' + selector + '":' + e, "errors");
262 } 273 }
...@@ -374,6 +385,32 @@ ...@@ -374,6 +385,32 @@
374 console.log("[casper:" + (level || "debug") + "] " + message); 385 console.log("[casper:" + (level || "debug") + "] " + message);
375 }; 386 };
376 387
388 this.processSelector = function processSelector(selector) {
389 var selectorObject = {
390 toString: function toString() {
391 return this.type + ' selector: ' + this.selector;
392 }
393 };
394 if (typeof selector === "string") {
395 // defaults to CSS selector
396 selectorObject.type = "css";
397 selectorObject.path = selector;
398 return selectorObject;
399 } else if (typeof selector === "object") {
400 // validation
401 if (!selector.hasOwnProperty('type') || !selector.hasOwnProperty('path')) {
402 throw new Error("Incomplete selector object");
403 } else if (SUPPORTED_SELECTOR_TYPES.indexOf(selector.type) === -1) {
404 throw new Error("Unsupported selector type: " + selector.type);
405 }
406 if (!selector.hasOwnProperty('toString')) {
407 selector.toString = selectorObject.toString;
408 }
409 return selector;
410 }
411 throw new Error("Unsupported selector type: " + typeof selector);
412 };
413
377 /** 414 /**
378 * Sets a field (or a set of fields) value. Fails silently, but log 415 * Sets a field (or a set of fields) value. Fails silently, but log
379 * error messages. 416 * error messages.
......