Commit 8b140127 8b140127289e8fb76a4e4b98b91c00c52eee16c1 by Nicolas Perriault

migrated clientutils tests to new test format

1 parent 2bcc009f
...@@ -7,132 +7,158 @@ function fakeDocument(html) { ...@@ -7,132 +7,158 @@ function fakeDocument(html) {
7 window.document.body.innerHTML = html; 7 window.document.body.innerHTML = html;
8 } 8 }
9 9
10 (function(casper) { 10 casper.test.begin('ClientUtils.encode() tests', 6, function(test) {
11 casper.test.comment('ClientUtils.encode()'); 11 var clientutils = require('clientutils').create(),
12 var clientutils = require('clientutils').create(); 12 testCases = {
13 var testCases = { 13 'an empty string': '',
14 'an empty string': '', 14 'a word': 'plop',
15 'a word': 'plop', 15 'a null char': 'a\u0000',
16 'a null char': 'a\u0000', 16 'an utf8 string': 'ÀÁÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ',
17 'an utf8 string': 'ÀÁÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ', 17 'song lyrics': ("Voilà l'été, j'aperçois le soleil\n" +
18 'song lyrics': ("Voilà l'été, j'aperçois le soleil\n" + 18 "Les nuages filent et le ciel s'éclaircit\n" +
19 "Les nuages filent et le ciel s'éclaircit\n" + 19 "Et dans ma tête qui bourdonnent?\n" +
20 "Et dans ma tête qui bourdonnent?\n" + 20 "Les abeilles!"),
21 "Les abeilles!"), 21 'a file contents': fs.read(phantom.casperPath + '/tests/site/alert.html')
22 'a file contents': fs.read(phantom.casperPath + '/tests/site/alert.html') 22 };
23 };
24 for (var what in testCases) { 23 for (var what in testCases) {
25 var source = testCases[what]; 24 test.assertEquals(
26 var encoded = clientutils.encode(source); 25 clientutils.decode(clientutils.encode(testCases[what])),
27 casper.test.assertEquals(clientutils.decode(encoded), source, 'ClientUtils.encode() encodes and decodes ' + what); 26 testCases[what],
27 'ClientUtils.encode() encodes and decodes ' + what
28 );
28 } 29 }
29 })(casper); 30 test.done();
31 });
30 32
31 (function(casper) { 33 casper.test.begin('ClientUtils.exists() tests', 5, function(test) {
32 casper.test.comment('ClientUtils.exists()');
33 var clientutils = require('clientutils').create(); 34 var clientutils = require('clientutils').create();
34 fakeDocument('<ul class="foo"><li>bar</li><li>baz</li></ul>'); 35 fakeDocument('<ul class="foo"><li>bar</li><li>baz</li></ul>');
35 casper.test.assert(clientutils.exists('ul'), 'ClientUtils.exists() checks that an element exist'); 36 test.assert(clientutils.exists('ul'),
36 casper.test.assertNot(clientutils.exists('ol'), 'ClientUtils.exists() checks that an element exist'); 37 'ClientUtils.exists() checks that an element exist');
37 casper.test.assert(clientutils.exists('ul.foo li'), 'ClientUtils.exists() checks that an element exist'); 38 test.assertNot(clientutils.exists('ol'),
39 'ClientUtils.exists() checks that an element exist');
40 test.assert(clientutils.exists('ul.foo li'),
41 'ClientUtils.exists() checks that an element exist');
38 // xpath 42 // xpath
39 casper.test.assert(clientutils.exists(x('//ul')), 'ClientUtils.exists() checks that an element exist using XPath'); 43 test.assert(clientutils.exists(x('//ul')),
40 casper.test.assertNot(clientutils.exists(x('//ol')), 'ClientUtils.exists() checks that an element exist using XPath'); 44 'ClientUtils.exists() checks that an element exist using XPath');
45 test.assertNot(clientutils.exists(x('//ol')),
46 'ClientUtils.exists() checks that an element exist using XPath');
41 fakeDocument(null); 47 fakeDocument(null);
42 })(casper); 48 test.done();
49 });
43 50
44 (function(casper) { 51 casper.test.begin('ClientUtils.findAll() tests', 7, function(test) {
45 casper.test.comment('ClientUtils.findAll()');
46 var clientutils = require('clientutils').create(); 52 var clientutils = require('clientutils').create();
47 fakeDocument('<ul class="foo"><li>bar</li><li>baz</li></ul>'); 53 fakeDocument('<ul class="foo"><li>bar</li><li>baz</li></ul>');
48 casper.test.assertType(clientutils.findAll('li'), 'nodelist', 'ClientUtils.findAll() can find matching DOM elements'); 54 test.assertType(clientutils.findAll('li'), 'nodelist',
49 casper.test.assertEquals(clientutils.findAll('li').length, 2, 'ClientUtils.findAll() can find matching DOM elements'); 55 'ClientUtils.findAll() can find matching DOM elements');
50 casper.test.assertType(clientutils.findAll('ol'), 'nodelist', 'ClientUtils.findAll() can find matching DOM elements'); 56 test.assertEquals(clientutils.findAll('li').length, 2,
51 casper.test.assertEquals(clientutils.findAll('ol').length, 0, 'ClientUtils.findAll() can find matching DOM elements'); 57 'ClientUtils.findAll() can find matching DOM elements');
58 test.assertType(clientutils.findAll('ol'), 'nodelist',
59 'ClientUtils.findAll() can find matching DOM elements');
60 test.assertEquals(clientutils.findAll('ol').length, 0,
61 'ClientUtils.findAll() can find matching DOM elements');
52 // scoped 62 // scoped
53 var scope = clientutils.findOne('ul'); 63 var scope = clientutils.findOne('ul');
54 casper.test.assertType(clientutils.findAll('li', scope), 'nodelist', 'ClientUtils.findAll() can find matching DOM elements within a given scope'); 64 test.assertType(clientutils.findAll('li', scope), 'nodelist',
55 casper.test.assertEquals(clientutils.findAll('li', scope).length, 2, 'ClientUtils.findAll() can find matching DOM elements within a given scope'); 65 'ClientUtils.findAll() can find matching DOM elements within a given scope');
56 casper.test.assertType(clientutils.findAll(x('//li'), scope), 'array', 'ClientUtils.findAll() can find matching DOM elements using XPath within a given scope'); 66 test.assertEquals(clientutils.findAll('li', scope).length, 2,
67 'ClientUtils.findAll() can find matching DOM elements within a given scope');
68 test.assertType(clientutils.findAll(x('//li'), scope), 'array',
69 'ClientUtils.findAll() can find matching DOM elements using XPath within a given scope');
57 fakeDocument(null); 70 fakeDocument(null);
58 })(casper); 71 test.done();
72 });
59 73
60 (function(casper) { 74 casper.test.begin('ClientUtils.findOne() tests', 4, function(test) {
61 casper.test.comment('ClientUtils.findOne()');
62 var clientutils = require('clientutils').create(); 75 var clientutils = require('clientutils').create();
63 fakeDocument('<ul class="foo"><li>bar</li><li>baz</li></ul>'); 76 fakeDocument('<ul class="foo"><li>bar</li><li>baz</li></ul>');
64 casper.test.assertType(clientutils.findOne('ul'), 'htmlulistelement', 'ClientUtils.findOne() can find a matching DOM element'); 77 test.assertType(clientutils.findOne('ul'), 'htmlulistelement',
65 casper.test.assertNot(clientutils.findOne('ol'), 'ClientUtils.findOne() can find a matching DOM element'); 78 'ClientUtils.findOne() can find a matching DOM element');
79 test.assertNot(clientutils.findOne('ol'),
80 'ClientUtils.findOne() can find a matching DOM element');
66 // scoped 81 // scoped
67 var scope = clientutils.findOne('ul'); 82 var scope = clientutils.findOne('ul');
68 casper.test.assertType(clientutils.findOne('li', scope), 'htmllielement', 'ClientUtils.findOne() can find a matching DOM element within a given scope'); 83 test.assertType(clientutils.findOne('li', scope), 'htmllielement',
69 casper.test.assertType(clientutils.findOne(x('//li'), scope), 'htmllielement', 'ClientUtils.findOne() can find a matching DOM element using XPath within a given scope'); 84 'ClientUtils.findOne() can find a matching DOM element within a given scope');
85 test.assertType(clientutils.findOne(x('//li'), scope), 'htmllielement',
86 'ClientUtils.findOne() can find a matching DOM element using XPath within a given scope');
70 fakeDocument(null); 87 fakeDocument(null);
71 })(casper); 88 test.done();
89 });
90
72 91
73 (function(casper) { 92 casper.test.begin('ClientUtils.processSelector() tests', 6, function(test) {
74 casper.test.comment('ClientUtils.processSelector()');
75 var clientutils = require('clientutils').create(); 93 var clientutils = require('clientutils').create();
76 // CSS3 selector 94 // CSS3 selector
77 var cssSelector = clientutils.processSelector('html body > ul.foo li'); 95 var cssSelector = clientutils.processSelector('html body > ul.foo li');
78 casper.test.assertType(cssSelector, 'object', 'ClientUtils.processSelector() can process a CSS3 selector'); 96 test.assertType(cssSelector, 'object',
79 casper.test.assertEquals(cssSelector.type, 'css', 'ClientUtils.processSelector() can process a CSS3 selector'); 97 'ClientUtils.processSelector() can process a CSS3 selector');
80 casper.test.assertEquals(cssSelector.path, 'html body > ul.foo li', 'ClientUtils.processSelector() can process a CSS3 selector'); 98 test.assertEquals(cssSelector.type, 'css',
99 'ClientUtils.processSelector() can process a CSS3 selector');
100 test.assertEquals(cssSelector.path, 'html body > ul.foo li',
101 'ClientUtils.processSelector() can process a CSS3 selector');
81 // XPath selector 102 // XPath selector
82 var xpathSelector = clientutils.processSelector(x('//li[text()="blah"]')); 103 var xpathSelector = clientutils.processSelector(x('//li[text()="blah"]'));
83 casper.test.assertType(xpathSelector, 'object', 'ClientUtils.processSelector() can process a XPath selector'); 104 test.assertType(xpathSelector, 'object',
84 casper.test.assertEquals(xpathSelector.type, 'xpath', 'ClientUtils.processSelector() can process a XPath selector'); 105 'ClientUtils.processSelector() can process a XPath selector');
85 casper.test.assertEquals(xpathSelector.path, '//li[text()="blah"]', 'ClientUtils.processSelector() can process a XPath selector'); 106 test.assertEquals(xpathSelector.type, 'xpath',
86 })(casper); 107 'ClientUtils.processSelector() can process a XPath selector');
108 test.assertEquals(xpathSelector.path, '//li[text()="blah"]',
109 'ClientUtils.processSelector() can process a XPath selector');
110 test.done();
111 });
87 112
88 (function(casper) { 113 casper.test.begin('ClientUtils.getElementBounds() tests', 3, function(test) {
89 casper.start(); 114 casper.start().then(function() {
90 // getElementBounds
91 casper.then(function() {
92 this.page.content = '<div id="b1" style="position:fixed;top:10px;left:11px;width:50px;height:60px"></div>'; 115 this.page.content = '<div id="b1" style="position:fixed;top:10px;left:11px;width:50px;height:60px"></div>';
93 this.test.assertEquals(this.getElementBounds('#b1'), 116 test.assertEquals(
117 this.getElementBounds('#b1'),
94 { top: 10, left: 11, width: 50, height: 60 }, 118 { top: 10, left: 11, width: 50, height: 60 },
95 'ClientUtils.getElementBounds() retrieves element boundaries'); 119 'ClientUtils.getElementBounds() retrieves element boundaries'
120 );
96 }); 121 });
97 // getElementsBounds
98 casper.start();
99 casper.then(function() { 122 casper.then(function() {
100 this.test.comment('Casper.getElementsBounds()'); 123 test.comment('Casper.getElementsBounds()');
101 var html = '<div id="boxes">'; 124 var html = '<div id="boxes">';
102 html += ' <div style="position:fixed;top:10px;left:11px;width:50px;height:60px"></div>'; 125 html += ' <div style="position:fixed;top:10px;left:11px;width:50px;height:60px"></div>';
103 html += ' <div style="position:fixed;top:20px;left:21px;width:70px;height:80px"></div>'; 126 html += ' <div style="position:fixed;top:20px;left:21px;width:70px;height:80px"></div>';
104 html += '</div>'; 127 html += '</div>';
105 this.page.content = html; 128 this.page.content = html;
106 var bounds = this.getElementsBounds('#boxes div'); 129 var bounds = this.getElementsBounds('#boxes div');
107 this.test.assertEquals(bounds[0], { top: 10, left: 11, width: 50, height: 60 }, 130 test.assertEquals(
108 'ClientUtils.getElementsBounds() retrieves multiple elements boundaries'); 131 bounds[0],
109 this.test.assertEquals(bounds[1], { top: 20, left: 21, width: 70, height: 80 }, 132 { top: 10, left: 11, width: 50, height: 60 },
110 'ClientUtils.getElementsBounds() retrieves multiple elements boundaries'); 133 'ClientUtils.getElementsBounds() retrieves multiple elements boundaries'
134 );
135 test.assertEquals(
136 bounds[1],
137 { top: 20, left: 21, width: 70, height: 80 },
138 'ClientUtils.getElementsBounds() retrieves multiple elements boundaries'
139 );
140 });
141 casper.run(function() {
142 test.done();
111 }); 143 });
112 })(casper); 144 });
113 145
114 (function(casper) { 146 casper.test.begin('ClientUtils.getElementInfo() tests', 10, function(test) {
115 // element information
116 casper.test.comment('ClientUtils.getElementInfo()');
117 casper.page.content = '<a href="plop" class="plip plup"><i>paf</i></a>'; 147 casper.page.content = '<a href="plop" class="plip plup"><i>paf</i></a>';
118 var info = casper.getElementInfo('a.plip'); 148 var info = casper.getElementInfo('a.plip');
119 casper.test.assertEquals(info.nodeName, 'a', 'ClientUtils.getElementInfo() retrieves element name'); 149 test.assertEquals(info.nodeName, 'a', 'ClientUtils.getElementInfo() retrieves element name');
120 casper.test.assertEquals(info.attributes, { 150 test.assertEquals(info.attributes, {
121 'href': 'plop', 151 'href': 'plop',
122 'class': 'plip plup' 152 'class': 'plip plup'
123 }, 'ClientUtils.getElementInfo() retrieves element attributes'); 153 }, 'ClientUtils.getElementInfo() retrieves element attributes');
124 casper.test.assertEquals(info.html, '<i>paf</i>', 'ClientUtils.getElementInfo() retrieves element html content'); 154 test.assertEquals(info.html, '<i>paf</i>', 'ClientUtils.getElementInfo() retrieves element html content');
125 casper.test.assertEquals(info.text, 'paf', 'ClientUtils.getElementInfo() retrieves element text'); 155 test.assertEquals(info.text, 'paf', 'ClientUtils.getElementInfo() retrieves element text');
126 casper.test.assert(info.x > 0, 'ClientUtils.getElementInfo() retrieves element x pos'); 156 test.assert(info.x > 0, 'ClientUtils.getElementInfo() retrieves element x pos');
127 casper.test.assert(info.y > 0, 'ClientUtils.getElementInfo() retrieves element y pos'); 157 test.assert(info.y > 0, 'ClientUtils.getElementInfo() retrieves element y pos');
128 casper.test.assert(info.width > 0, 'ClientUtils.getElementInfo() retrieves element width'); 158 test.assert(info.width > 0, 'ClientUtils.getElementInfo() retrieves element width');
129 casper.test.assert(info.height > 0, 'ClientUtils.getElementInfo() retrieves element height'); 159 test.assert(info.height > 0, 'ClientUtils.getElementInfo() retrieves element height');
130 casper.test.assert(info.visible, 'ClientUtils.getElementInfo() retrieves element visibility'); 160 test.assert(info.visible, 'ClientUtils.getElementInfo() retrieves element visibility');
131 casper.test.assertEquals(info.tag, '<a href="plop" class="plip plup"><i>paf</i></a>', 161 test.assertEquals(info.tag, '<a href="plop" class="plip plup"><i>paf</i></a>',
132 'ClientUtils.getElementInfo() retrieves element whole tag contents'); 162 'ClientUtils.getElementInfo() retrieves element whole tag contents');
133 163 test.done();
134 })(casper);
135
136 casper.run(function() {
137 this.test.done(40);
138 }); 164 });
......