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) {
});
```
## Testing
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:
$ phantomjs tests/run.js
## Licensing
`Casper.js` is released under the terms of the [MIT license](http://en.wikipedia.org/wiki/MIT_License).
......
phantom.Casper.extend({
assert: function(condition, message) {
var status = '[PASS]';
if (condition === true) {
testResults.passed++;
} else {
status = '[FAIL]';
testResults.failed++;
}
this.echo([status, message].join(' '));
},
assertEquals: function(testValue, expected, message) {
if (expected === testValue) {
this.echo('[PASS] ' + message);
testResults.passed++;
} else {
this.echo('[FAIL] ' + message);
this.echo(' got: ' + testValue);
this.echo(' expected: ' + expected);
testResults.failed++;
}
},
assertEvalEquals: function(fn, expected, message) {
return this.assertEquals(this.evaluate(fn), expected, message);
}
});
\ No newline at end of file
phantom.injectJs('casper.js');
phantom.injectJs('tests/assert.js');
var casper = new phantom.Casper({
verbose: true,
}), testResults = {
passed: 0,
failed: 0
};
casper.start('tests/site/index.html', function(self) {
self.assertEvalEquals(function() {
return document.title;
}, 'CasperJS test index', 'start() casper can start itself an open an url');
self.click('a:first-child');
});
casper.then(function(self) {
self.assertEvalEquals(function() {
return document.title;
}, 'CasperJS test target', 'click() casper can click on a text link and react when it is loaded');
self.click('a:first-child');
});
casper.then(function(self) {
self.fill('form[action="form.html"]', {
'email': 'chuck@norris.com',
'content': 'Am watching thou',
'check': true,
'choice': 'no'
});
self.assertEvalEquals(function() {
return document.querySelector('input[name="email"]').value;
}, 'chuck@norris.com', 'fill() can fill an input[type=text] form field');
self.assertEvalEquals(function() {
return document.querySelector('textarea[name="content"]').value;
}, 'Am watching thou', 'fill() can fill a textarea form field');
self.assertEvalEquals(function() {
return document.querySelector('input[name="check"]').checked;
}, true, 'fill() can check a form checkbox');
self.assertEvalEquals(function() {
return document.querySelector('input[name="choice"][value="no"]').checked;
}, true, 'fill() can check a form radio button');
});
casper.run(function(self) {
self.echo("==========================================");
var total = testResults.passed + testResults.failed;
self.echo(total + ' tests executed, ' + testResults.passed + ' passed, ' + testResults.failed + ' failed.');
self.exit();
});
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CasperJS test form</title>
</head>
<body>
<form action="form.html">
<input type="text" name="email" placeholder="email" />
<textarea name="content"></textarea>
<select name="topic">
<option>foo</option>
<option value="bar">baz</option>
</select>
<input type="checkbox" name="check" />
<input type="radio" name="choice" value="yes"/>
<input type="radio" name="choice" value="no"/>
<input type="submit"/>
</form>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CasperJS test index</title>
</head>
<body>
<a href="test.html">test</a>
<a href="form.html">form</a>
</body>
</html>
\ No newline at end of file
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CasperJS test target</title>
</head>
<body>
<a href="form.html">test form</a>
</body>
</html>
\ No newline at end of file