Commit 90bed012 90bed012a3688c0a5ef5dd16a464a063e988abf1 by Nicolas Perriault

added a 'scope' param for ClientUtils.findOne() and findAll()

1 parent 0e88854b
...@@ -238,16 +238,18 @@ ...@@ -238,16 +238,18 @@
238 /** 238 /**
239 * Finds all DOM elements matching by the provided selector. 239 * Finds all DOM elements matching by the provided selector.
240 * 240 *
241 * @param String selector CSS3 selector 241 * @param String selector CSS3 selector
242 * @param HTMLElement|null scope Element to search child elements within
242 * @return NodeList|undefined 243 * @return NodeList|undefined
243 */ 244 */
244 this.findAll = function findAll(selector) { 245 this.findAll = function findAll(selector, scope) {
246 scope = scope || document;
245 try { 247 try {
246 var pSelector = this.processSelector(selector); 248 var pSelector = this.processSelector(selector);
247 if (pSelector.type === 'xpath') { 249 if (pSelector.type === 'xpath') {
248 return this.getElementsByXPath(pSelector.path); 250 return this.getElementsByXPath(pSelector.path);
249 } else { 251 } else {
250 return document.querySelectorAll(pSelector.path); 252 return scope.querySelectorAll(pSelector.path);
251 } 253 }
252 } catch (e) { 254 } catch (e) {
253 this.log('findAll(): invalid selector provided "' + selector + '":' + e, "error"); 255 this.log('findAll(): invalid selector provided "' + selector + '":' + e, "error");
...@@ -257,16 +259,18 @@ ...@@ -257,16 +259,18 @@
257 /** 259 /**
258 * Finds a DOM element by the provided selector. 260 * Finds a DOM element by the provided selector.
259 * 261 *
260 * @param String selector CSS3 selector 262 * @param String selector CSS3 selector
263 * @param HTMLElement|null scope Element to search child elements within
261 * @return HTMLElement|undefined 264 * @return HTMLElement|undefined
262 */ 265 */
263 this.findOne = function findOne(selector) { 266 this.findOne = function findOne(selector, scope) {
267 scope = scope || document;
264 try { 268 try {
265 var pSelector = this.processSelector(selector); 269 var pSelector = this.processSelector(selector);
266 if (pSelector.type === 'xpath') { 270 if (pSelector.type === 'xpath') {
267 return this.getElementByXPath(pSelector.path); 271 return this.getElementByXPath(pSelector.path);
268 } else { 272 } else {
269 return document.querySelector(pSelector.path); 273 return scope.querySelector(pSelector.path);
270 } 274 }
271 } catch (e) { 275 } catch (e) {
272 this.log('findOne(): invalid selector provided "' + selector + '":' + e, "error"); 276 this.log('findOne(): invalid selector provided "' + selector + '":' + e, "error");
......
1 var fs = require('fs');
2 var clientutils = require('clientutils').create();
3
4 var testCases = {
5 'an empty string': '',
6 'a word': 'plop',
7 'a null char': 'a\u0000',
8 'an utf8 string': 'ÀÁÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ',
9 'song lyrics': ("Voilà l'été, j'aperçois le soleil\n" +
10 "Les nuages filent et le ciel s'éclaircit\n" +
11 "Et dans ma tête qui bourdonnent?\n" +
12 "Les abeilles!"),
13 'a file contents': fs.read(phantom.casperPath + '/tests/site/alert.html')
14 };
15
16 casper.test.comment('ClientUtils.encode()');
17
18 for (var what in testCases) {
19 var source = testCases[what];
20 var encoded = clientutils.encode(source);
21 casper.test.assertEquals(clientutils.decode(encoded), source, 'ClientUtils can encode and decode ' + what);
22 }
23
24 casper.test.done();
1 var fs = require('fs');
2 var x = require('casper').selectXPath;
3
4 function fakeDocument(html) {
5 window.document.body.innerHTML = html;
6 }
7
8 (function(casper) {
9 casper.test.comment('ClientUtils.encode()');
10 var clientutils = require('clientutils').create();
11 var testCases = {
12 'an empty string': '',
13 'a word': 'plop',
14 'a null char': 'a\u0000',
15 'an utf8 string': 'ÀÁÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ',
16 'song lyrics': ("Voilà l'été, j'aperçois le soleil\n" +
17 "Les nuages filent et le ciel s'éclaircit\n" +
18 "Et dans ma tête qui bourdonnent?\n" +
19 "Les abeilles!"),
20 'a file contents': fs.read(phantom.casperPath + '/tests/site/alert.html')
21 };
22 for (var what in testCases) {
23 var source = testCases[what];
24 var encoded = clientutils.encode(source);
25 casper.test.assertEquals(clientutils.decode(encoded), source, 'ClientUtils.encode() encodes and decodes ' + what);
26 }
27 })(casper);
28
29 (function(casper) {
30 casper.test.comment('ClientUtils.exists()');
31 var clientutils = require('clientutils').create();
32 fakeDocument('<ul class="foo"><li>bar</li><li>baz</li></ul>');
33 casper.test.assert(clientutils.exists('ul'), 'ClientUtils.exists() checks that an element exist');
34 casper.test.assertNot(clientutils.exists('ol'), 'ClientUtils.exists() checks that an element exist');
35 casper.test.assert(clientutils.exists('ul.foo li'), 'ClientUtils.exists() checks that an element exist');
36 // xpath
37 casper.test.assert(clientutils.exists(x('//ul')), 'ClientUtils.exists() checks that an element exist using XPath');
38 casper.test.assertNot(clientutils.exists(x('//ol')), 'ClientUtils.exists() checks that an element exist using XPath');
39 fakeDocument(null);
40 })(casper);
41
42 (function(casper) {
43 casper.test.comment('ClientUtils.findAll()');
44 var clientutils = require('clientutils').create();
45 fakeDocument('<ul class="foo"><li>bar</li><li>baz</li></ul>');
46 casper.test.assertType(clientutils.findAll('li'), 'nodelist', 'ClientUtils.findAll() can find matching DOM elements');
47 casper.test.assertEquals(clientutils.findAll('li').length, 2, 'ClientUtils.findAll() can find matching DOM elements');
48 casper.test.assertType(clientutils.findAll('ol'), 'nodelist', 'ClientUtils.findAll() can find matching DOM elements');
49 casper.test.assertEquals(clientutils.findAll('ol').length, 0, 'ClientUtils.findAll() can find matching DOM elements');
50 // scoped
51 var scope = clientutils.findOne('ul');
52 casper.test.assertType(clientutils.findAll('li', scope), 'nodelist', 'ClientUtils.findAll() can find matching DOM elements within a given scope');
53 fakeDocument(null);
54 })(casper);
55
56 (function(casper) {
57 casper.test.comment('ClientUtils.findOne()');
58 var clientutils = require('clientutils').create();
59 fakeDocument('<ul class="foo"><li>bar</li><li>baz</li></ul>');
60 casper.test.assertType(clientutils.findOne('ul'), 'htmlulistelement', 'ClientUtils.findOne() can find a matching DOM element');
61 casper.test.assertNot(clientutils.findOne('ol'), 'ClientUtils.findOne() can find a matching DOM element');
62 // scoped
63 var scope = clientutils.findOne('ul');
64 casper.test.assertType(clientutils.findAll('li', scope), 'nodelist', 'ClientUtils.findAll() can find matching DOM elements within a given scope');
65 casper.test.assertEquals(clientutils.findAll('li', scope).length, 2, 'ClientUtils.findAll() can find matching DOM elements within a given scope');
66 fakeDocument(null);
67 })(casper);
68
69 (function(casper) {
70 casper.test.comment('ClientUtils.processSelector()');
71 var clientutils = require('clientutils').create();
72 // CSS3 selector
73 var cssSelector = clientutils.processSelector('html body > ul.foo li');
74 casper.test.assertType(cssSelector, 'object', 'ClientUtils.processSelector() can process a CSS3 selector');
75 casper.test.assertEquals(cssSelector.type, 'css', 'ClientUtils.processSelector() can process a CSS3 selector');
76 casper.test.assertEquals(cssSelector.path, 'html body > ul.foo li', 'ClientUtils.processSelector() can process a CSS3 selector');
77 // XPath selector
78 var xpathSelector = clientutils.processSelector(x('//li[text()="blah"]'));
79 casper.test.assertType(xpathSelector, 'object', 'ClientUtils.processSelector() can process a XPath selector');
80 casper.test.assertEquals(xpathSelector.type, 'xpath', 'ClientUtils.processSelector() can process a XPath selector');
81 casper.test.assertEquals(xpathSelector.path, '//li[text()="blah"]', 'ClientUtils.processSelector() can process a XPath selector');
82 })(casper);
83
84 casper.test.done();