Commit 86bf9f38 86bf9f38310cee44884637472120bafdd9847dc5 by Nicolas Perriault

added basic test suite

1 parent dd18d334
...@@ -607,6 +607,12 @@ new phantom.Casper().start('http://www.liberation.fr/', function(self) { ...@@ -607,6 +607,12 @@ new phantom.Casper().start('http://www.liberation.fr/', function(self) {
607 }); 607 });
608 ``` 608 ```
609 609
610 ## Testing
611
612 CasperJS has some unit and functional tests, located in the `tests` subfolder. More tests will be added in the future. To run the test suite, from the root of a checkout of the casperjs repository:
613
614 $ phantomjs tests/run.js
615
610 ## Licensing 616 ## Licensing
611 617
612 `Casper.js` is released under the terms of the [MIT license](http://en.wikipedia.org/wiki/MIT_License). 618 `Casper.js` is released under the terms of the [MIT license](http://en.wikipedia.org/wiki/MIT_License).
......
1 phantom.Casper.extend({
2 assert: function(condition, message) {
3 var status = '[PASS]';
4 if (condition === true) {
5 testResults.passed++;
6 } else {
7 status = '[FAIL]';
8 testResults.failed++;
9 }
10 this.echo([status, message].join(' '));
11 },
12
13 assertEquals: function(testValue, expected, message) {
14 if (expected === testValue) {
15 this.echo('[PASS] ' + message);
16 testResults.passed++;
17 } else {
18 this.echo('[FAIL] ' + message);
19 this.echo(' got: ' + testValue);
20 this.echo(' expected: ' + expected);
21 testResults.failed++;
22 }
23 },
24
25 assertEvalEquals: function(fn, expected, message) {
26 return this.assertEquals(this.evaluate(fn), expected, message);
27 }
28 });
...\ No newline at end of file ...\ No newline at end of file
1 phantom.injectJs('casper.js');
2 phantom.injectJs('tests/assert.js');
3
4 var casper = new phantom.Casper({
5 verbose: true,
6 }), testResults = {
7 passed: 0,
8 failed: 0
9 };
10
11 casper.start('tests/site/index.html', function(self) {
12 self.assertEvalEquals(function() {
13 return document.title;
14 }, 'CasperJS test index', 'start() casper can start itself an open an url');
15 self.click('a:first-child');
16 });
17
18 casper.then(function(self) {
19 self.assertEvalEquals(function() {
20 return document.title;
21 }, 'CasperJS test target', 'click() casper can click on a text link and react when it is loaded');
22 self.click('a:first-child');
23 });
24
25 casper.then(function(self) {
26 self.fill('form[action="form.html"]', {
27 'email': 'chuck@norris.com',
28 'content': 'Am watching thou',
29 'check': true,
30 'choice': 'no'
31 });
32 self.assertEvalEquals(function() {
33 return document.querySelector('input[name="email"]').value;
34 }, 'chuck@norris.com', 'fill() can fill an input[type=text] form field');
35 self.assertEvalEquals(function() {
36 return document.querySelector('textarea[name="content"]').value;
37 }, 'Am watching thou', 'fill() can fill a textarea form field');
38 self.assertEvalEquals(function() {
39 return document.querySelector('input[name="check"]').checked;
40 }, true, 'fill() can check a form checkbox');
41 self.assertEvalEquals(function() {
42 return document.querySelector('input[name="choice"][value="no"]').checked;
43 }, true, 'fill() can check a form radio button');
44 });
45
46 casper.run(function(self) {
47 self.echo("==========================================");
48 var total = testResults.passed + testResults.failed;
49 self.echo(total + ' tests executed, ' + testResults.passed + ' passed, ' + testResults.failed + ' failed.');
50 self.exit();
51 });
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>CasperJS test form</title>
6 </head>
7 <body>
8 <form action="form.html">
9 <input type="text" name="email" placeholder="email" />
10 <textarea name="content"></textarea>
11 <select name="topic">
12 <option>foo</option>
13 <option value="bar">baz</option>
14 </select>
15 <input type="checkbox" name="check" />
16 <input type="radio" name="choice" value="yes"/>
17 <input type="radio" name="choice" value="no"/>
18 <input type="submit"/>
19 </form>
20 </body>
21 </html>
...\ No newline at end of file ...\ No newline at end of file
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>CasperJS test index</title>
6 </head>
7 <body>
8 <a href="test.html">test</a>
9 <a href="form.html">form</a>
10 </body>
11 </html>
...\ No newline at end of file ...\ No newline at end of file
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>CasperJS test target</title>
6 </head>
7 <body>
8 <a href="form.html">test form</a>
9 </body>
10 </html>
...\ No newline at end of file ...\ No newline at end of file