Commit c61f0f18 c61f0f1839c4aa73800b50871f52d9e97159d7b1 by Nicolas Perriault

migrated remaining tests to the new format

1 parent 8e107626
Subproject commit 058396e39822a5fa2d57e1a771ad2340ca0f358b
Subproject commit 18b8cbb5335073eaf0d1cd9a7c1f1634e394aa4b
......
......@@ -16,7 +16,7 @@
var li = document.createElement('li')
li.appendChild(document.createTextNode('four'));
document.querySelector('ul').appendChild(li);
}, 2000);
}, 500);
</script>
</body>
</html>
\ No newline at end of file
</html>
......
/*global casper*/
/*jshint strict:false*/
casper.start('tests/site/index.html', function() {
this.test.assertEquals(this.getHTML('ul li'), 'one', 'Casper.getHTML() retrieves inner HTML by default');
this.test.assertEquals(this.getHTML('ul li', true), '<li>one</li>', 'Casper.getHTML() can retrieve outer HTML');
});
casper.run(function() {
casper.test.done(2);
casper.test.begin('getHTML() tests', 2, function(test) {
casper.start('tests/site/index.html', function() {
test.assertEquals(this.getHTML('ul li'), 'one',
'Casper.getHTML() retrieves inner HTML by default');
test.assertEquals(this.getHTML('ul li', true), '<li>one</li>',
'Casper.getHTML() can retrieve outer HTML');
}).run(function() {
test.done();
});
});
......
/*global casper*/
/*jshint strict:false*/
casper.start('tests/site/elementattribute.html', function() {
this.test.comment('Casper.getElementAttribute()');
this.test.assertEquals(this.getElementAttribute('.testo','data-stuff'), 'beautiful string', 'Casper.getElementAttribute() works as intended');
});
casper.run(function() {
this.test.done(1);
casper.test.begin('getElementAttribute() tests', 1, function(test) {
casper.start('tests/site/elementattribute.html', function() {
test.assertEquals(this.getElementAttribute('.testo','data-stuff'), 'beautiful string',
'Casper.getElementAttribute() works as intended');
}).run(function() {
test.done();
});
});
......
......@@ -2,23 +2,18 @@
/*jshint strict:false*/
var fs = require('fs');
// FIXME: we're using local url scheme until https://github.com/ariya/phantomjs/pull/288 is
// possibly merged
casper.start('file://' + phantom.casperPath + '/tests/site/index.html', function() {
var imageUrl = 'file://' + phantom.casperPath + '/tests/site/images/phantom.png';
var image = this.base64encode(imageUrl);
this.test.comment('Casper.base64encode()');
this.test.assertEquals(image.length, 6160, 'Casper.base64encode() can retrieve base64 contents');
this.test.comment('Casper.download()');
this.download(imageUrl, '__test_logo.png');
this.test.assert(fs.exists('__test_logo.png'), 'Casper.download() downloads a file');
if (fs.exists('__test_logo.png')) {
fs.remove('__test_logo.png');
}
});
casper.run(function() {
this.test.done(2);
casper.test.begin('base64encode() and download() tests', 2, function(test) {
// FIXME: https://github.com/ariya/phantomjs/pull/364 has been merged, update scheme
casper.start('file://' + phantom.casperPath + '/tests/site/index.html', function() {
var imageUrl = 'file://' + phantom.casperPath + '/tests/site/images/phantom.png',
image = this.base64encode(imageUrl);
test.assertEquals(image.length, 6160, 'Casper.base64encode() can retrieve base64 contents');
this.download(imageUrl, '__test_logo.png');
test.assert(fs.exists('__test_logo.png'), 'Casper.download() downloads a file');
if (fs.exists('__test_logo.png')) {
fs.remove('__test_logo.png');
}
}).run(function() {
test.done();
});
});
......
/*global casper*/
/*jshint strict:false maxparams:99*/
casper.test.comment('Casper.evaluate()');
casper.start();
var context = {
"_boolean_true": true,
"_boolean_false": false,
"_int_number": 42,
"_float_number": 1337.42,
"_string": "plop! \"Ÿ£$\" 'no'",
"_array": [1, 2, 3],
"_object": {a: 1, b: 2},
"_function": function(){console.log('ok');}
};
var result = casper.evaluate(function(_boolean_true,
_boolean_false,
_int_number,
_float_number,
_string,
_array,
_object,
_function) {
return [].map.call(arguments, function(arg) {
return typeof(arg);
});
}, context);
casper.test.assertEquals(result.toString(),
['boolean', 'boolean', 'number', 'number', 'string', 'object', 'object', 'function'].toString(),
'Casper.evaluate() handles passed argument context correcly');
// no context
casper.test.assertEquals(casper.evaluate(function() {
return 42;
}), 42, 'Casper.evaluate() handles evaluation with no context passed');
// object context (previous casperjs versions compatibility mode)
casper.test.assertEquals(casper.evaluate(function(a) {
return [a];
}, {a: "foo"}), ["foo"], 'Casper.evaluate() accepts an object as arguments context');
casper.test.assertEquals(casper.evaluate(function(a, b) {
return [a, b];
}, {a: "foo", b: "bar"}), ["foo", "bar"], 'Casper.evaluate() accepts an object as arguments context');
casper.test.assertEquals(casper.evaluate(function(a, b, c) {
return [a, b, c];
}, {a: "foo", b: "bar", c: "baz"}), ["foo", "bar", "baz"], 'Casper.evaluate() accepts an object as arguments context');
casper.test.begin('mapping argument context', 1, function(test) {
casper.start();
var context = {
"_boolean_true": true,
"_boolean_false": false,
"_int_number": 42,
"_float_number": 1337.42,
"_string": "plop! \"Ÿ£$\" 'no'",
"_array": [1, 2, 3],
"_object": {a: 1, b: 2},
"_function": function(){console.log('ok');}
};
var result = casper.evaluate(function(_boolean_true, _boolean_false, _int_number,
_float_number, _string, _array, _object, _function) {
return [].map.call(arguments, function(arg) {
return typeof(arg);
});
}, context);
test.assertEquals(
result.toString(),
['boolean', 'boolean', 'number', 'number', 'string', 'object', 'object', 'function'].toString(),
'Casper.evaluate() handles passed argument context correcly'
);
test.done();
});
// array context
casper.test.assertEquals(casper.evaluate(function(a) {
return [a];
}, ["foo"]), ["foo"], 'Casper.evaluate() accepts an array as arguments context');
casper.test.assertEquals(casper.evaluate(function(a, b) {
return [a, b];
}, ["foo", "bar"]), ["foo", "bar"], 'Casper.evaluate() accepts an array as arguments context');
casper.test.assertEquals(casper.evaluate(function(a, b, c) {
return [a, b, c];
}, ["foo", "bar", "baz"]), ["foo", "bar", "baz"], 'Casper.evaluate() accepts an array as arguments context');
casper.test.begin('handling no argument context', 1, function(test) {
casper.start();
test.assertEquals(casper.evaluate(function() {
return 42;
}), 42, 'Casper.evaluate() handles evaluation with no context passed');
test.done();
});
// natural arguments context (phantomjs equivalent)
casper.test.assertEquals(casper.evaluate(function(a) {
return [a];
}, "foo"), ["foo"], 'Casper.evaluate() accepts natural arguments context');
casper.test.assertEquals(casper.evaluate(function(a, b) {
return [a, b];
}, "foo", "bar"), ["foo", "bar"], 'Casper.evaluate() accepts natural arguments context');
casper.test.assertEquals(casper.evaluate(function(a, b, c) {
return [a, b, c];
}, "foo", "bar", "baz"), ["foo", "bar", "baz"], 'Casper.evaluate() accepts natural arguments context');
casper.test.begin('handling of object context (BC mode)', 3, function(test) {
casper.start();
test.assertEquals(casper.evaluate(function(a) {
return [a];
}, {a: "foo"}), ["foo"], 'Casper.evaluate() accepts an object as arguments context');
test.assertEquals(casper.evaluate(function(a, b) {
return [a, b];
}, {a: "foo", b: "bar"}), ["foo", "bar"], 'Casper.evaluate() accepts an object as arguments context');
test.assertEquals(casper.evaluate(function(a, b, c) {
return [a, b, c];
}, {a: "foo", b: "bar", c: "baz"}), ["foo", "bar", "baz"], 'Casper.evaluate() accepts an object as arguments context');
test.done();
});
casper.start().thenEvaluate(function(a, b) {
window.a = a
window.b = b;
}, "foo", "bar");
casper.test.begin('handling of array context', 3, function(test) {
casper.start();
test.assertEquals(casper.evaluate(function(a) {
return [a];
}, ["foo"]), ["foo"], 'Casper.evaluate() accepts an array as arguments context');
test.assertEquals(casper.evaluate(function(a, b) {
return [a, b];
}, ["foo", "bar"]), ["foo", "bar"], 'Casper.evaluate() accepts an array as arguments context');
test.assertEquals(casper.evaluate(function(a, b, c) {
return [a, b, c];
}, ["foo", "bar", "baz"]), ["foo", "bar", "baz"], 'Casper.evaluate() accepts an array as arguments context');
test.done();
});
casper.then(function() {
this.test.comment('Casper.thenEvaluate()');
this.test.assertEquals(this.getGlobal('a'), "foo", "Casper.thenEvaluate() sets args");
this.test.assertEquals(this.getGlobal('b'), "bar",
"Casper.thenEvaluate() sets args the same way evaluate() does");
casper.test.begin('natural arguments context (phantomjs equivalent)', 3, function(test) {
casper.start();
test.assertEquals(casper.evaluate(function(a) {
return [a];
}, "foo"), ["foo"], 'Casper.evaluate() accepts natural arguments context');
test.assertEquals(casper.evaluate(function(a, b) {
return [a, b];
}, "foo", "bar"), ["foo", "bar"], 'Casper.evaluate() accepts natural arguments context');
test.assertEquals(casper.evaluate(function(a, b, c) {
return [a, b, c];
}, "foo", "bar", "baz"), ["foo", "bar", "baz"], 'Casper.evaluate() accepts natural arguments context');
test.done();
});
casper.run(function() {
this.test.done(13);
casper.test.begin('thenEvaluate() tests', 2, function(test) {
casper.start().thenEvaluate(function(a, b) {
window.a = a
window.b = b;
}, "foo", "bar");
casper.then(function() {
test.assertEquals(this.getGlobal('a'), "foo", "Casper.thenEvaluate() sets args");
test.assertEquals(this.getGlobal('b'), "bar",
"Casper.thenEvaluate() sets args the same way evaluate() does");
});
casper.run(function() {
test.done();
});
});
......
/*global casper*/
/*jshint strict:false*/
// events
casper.test.comment("events");
casper.plopped = false;
casper.on("plop", function() {
this.plopped = true;
casper.test.begin('events', 2, function(test) {
casper.plopped = false;
casper.on("plop", function() {
this.plopped = true;
});
test.assert(Object.keys(casper._events).some(function(i) {
return i === "plop";
}), "on() has set an event handler");
casper.emit("plop");
test.assert(casper.plopped, "emit() emits an event");
test.done();
});
casper.test.assert(Object.keys(casper._events).some(function(i) {
return i === "plop";
}), "on() has set an event handler");
casper.emit("plop");
casper.test.assert(casper.plopped, "emit() emits an event");
// filters
casper.test.comment("filters");
casper.foo = 0;
casper.setFilter("test", function(a) {
this.foo = 42;
return a + 1;
casper.test.begin('filters', 3, function(test) {
casper.foo = 0;
casper.setFilter("test", function(a) {
this.foo = 42;
return a + 1;
});
test.assert(Object.keys(casper._filters).some(function(i) {
return i === "test";
}), "setFilter() has set a filter");
test.assertEquals(casper.filter("test", 1), 2, "filter() filters a value");
test.assertEquals(casper.foo, 42, "filter() applies the correct context");
delete casper.foo;
test.done();
});
casper.test.assert(Object.keys(casper._filters).some(function(i) {
return i === "test";
}), "setFilter() has set a filter");
casper.test.assertEquals(casper.filter("test", 1), 2, "filter() filters a value");
casper.test.assertEquals(casper.foo, 42, "filter() applies the correct context");
delete casper.foo;
casper.test.done(5);
......
/*global casper*/
/*jshint strict:false*/
casper.test.comment('Casper.exists()');
casper.start('tests/site/index.html', function() {
this.test.assert(this.exists('a') && !this.exists('chucknorriz'), 'Casper.exists() can check if an element exists');
});
casper.run(function() {
this.test.done(1);
casper.test.begin('exists() tests', 2, function(test) {
casper.start('tests/site/index.html', function() {
test.assert(this.exists('a'), 'Casper.exists() can check if an element exists');
test.assertNot(this.exists('chucknorriz'), 'Casper.exists() can check than an element does not exist')
}).run(function() {
test.done();
});
});
......
/*global casper*/
/*jshint strict:false*/
casper.test.comment('Casper.fetchText()');
casper.start('tests/site/index.html', function() {
this.test.assertEquals(this.fetchText('ul li'), 'onetwothree', 'Casper.fetchText() can retrieve text contents');
});
casper.run(function() {
this.test.done(1);
casper.test.begin('fetchText() tests', 1, function(test) {
casper.start('tests/site/index.html', function() {
test.assertEquals(this.fetchText('ul li'), 'onetwothree',
'Casper.fetchText() can retrieve text contents');
}).run(function() {
test.done();
});
});
......
step = 0
casper.test.begin 'handling waits and timeouts', 13, (test) ->
step = 0
# testing resources
casper.start "tests/site/resources.html", ->
@test.assertEquals ++step, 1, "step 1"
@wait 400, ->
@test.assertEquals ++step, 2, "step 1.1"
@wait 200, ->
@test.assertEquals ++step, 3, "step 1.1.1"
casper.start "tests/site/resources.html", ->
test.assertEquals ++step, 1, "step 1"
@wait 400, ->
test.assertEquals ++step, 2, "step 1.1"
@wait 200, ->
@test.assertEquals ++step, 4, "step 1.1.1.1"
@then ->
@test.assertEquals ++step, 5, "step 1.1.2.1"
@wait 400, ->
@test.assertEquals ++step, 6, "step 1.2"
test.assertEquals ++step, 3, "step 1.1.1"
@wait 200, ->
test.assertEquals ++step, 4, "step 1.1.1.1"
@then ->
test.assertEquals ++step, 5, "step 1.1.2.1"
@wait 400, ->
test.assertEquals ++step, 6, "step 1.2"
casper.wait 200, ->
@test.assertEquals ++step, 7, "step 2"
casper.wait 200, ->
test.assertEquals ++step, 7, "step 2"
casper.waitForSelector(
'#noneExistingSelector'
-> @test.fail "should run into timeout"
-> @test.assertEquals ++step, 8, "step 3 sucessfully timed out"
1000
)
casper.then ->
@test.assertEquals ++step, 9, "step 4"
@wait 300, ->
@test.assertEquals ++step, 10, "step 4.1"
casper.waitForSelector(
'#noneExistingSelector'
-> test.fail "should run into timeout"
-> test.assertEquals ++step, 8, "step 3 sucessfully timed out"
1000
)
casper.then ->
test.assertEquals ++step, 9, "step 4"
@wait 300, ->
@test.assertEquals ++step, 11, "step 4.1.1"
@wait 100, ->
@test.assertEquals ++step, 12, "step 5.2"
test.assertEquals ++step, 10, "step 4.1"
@wait 300, ->
test.assertEquals ++step, 11, "step 4.1.1"
@wait 100, ->
test.assertEquals ++step, 12, "step 5.2"
casper.then ->
@test.assertEquals ++step, 13, "last step"
casper.then ->
test.assertEquals ++step, 13, "last step"
casper.run(-> @test.done(13))
casper.run(-> test.done())
......
/*global casper*/
/*jshint strict:false*/
casper.start('tests/site/form.html', function() {
this.test.comment('Casper.fill()');
this.fill('form[action="result.html"]', {
email: 'chuck@norris.com',
password: 'chuck',
content: 'Am watching thou',
check: true,
choice: 'no',
topic: 'bar',
file: phantom.libraryPath + '/README.md',
'checklist[]': ['1', '3']
casper.test.begin('fill() tests', 15, function(test) {
casper.start('tests/site/form.html', function() {
this.fill('form[action="result.html"]', {
email: 'chuck@norris.com',
password: 'chuck',
content: 'Am watching thou',
check: true,
choice: 'no',
topic: 'bar',
file: phantom.libraryPath + '/README.md',
'checklist[]': ['1', '3']
});
test.assertEvalEquals(function() {
return __utils__.findOne('input[name="email"]').value;
}, 'chuck@norris.com', 'Casper.fill() can fill an input[type=text] form field');
test.assertEvalEquals(function() {
return __utils__.findOne('input[name="password"]').value;
}, 'chuck', 'Casper.fill() can fill an input[type=password] form field');
test.assertEvalEquals(function() {
return __utils__.findOne('textarea[name="content"]').value;
}, 'Am watching thou', 'Casper.fill() can fill a textarea form field');
test.assertEvalEquals(function() {
return __utils__.findOne('select[name="topic"]').value;
}, 'bar', 'Casper.fill() can pick a value from a select form field');
test.assertEvalEquals(function() {
return __utils__.findOne('input[name="check"]').checked;
}, true, 'Casper.fill() can check a form checkbox');
test.assertEvalEquals(function() {
return __utils__.findOne('input[name="choice"][value="no"]').checked;
}, true, 'Casper.fill() can check a form radio button 1/2');
test.assertEvalEquals(function() {
return __utils__.findOne('input[name="choice"][value="yes"]').checked;
}, false, 'Casper.fill() can check a form radio button 2/2');
test.assertEvalEquals(function() {
return __utils__.findOne('input[name="file"]').files.length === 1;
}, true, 'Casper.fill() can select a file to upload');
test.assertEvalEquals(function() {
return (__utils__.findOne('input[name="checklist[]"][value="1"]').checked &&
!__utils__.findOne('input[name="checklist[]"][value="2"]').checked &&
__utils__.findOne('input[name="checklist[]"][value="3"]').checked);
}, true, 'Casper.fill() can fill a list of checkboxes');
});
casper.thenClick('input[type="submit"]', function() {
test.assertUrlMatch(/email=chuck@norris.com/, 'Casper.fill() input[type=email] field was submitted');
test.assertUrlMatch(/password=chuck/, 'Casper.fill() input[type=password] field was submitted');
test.assertUrlMatch(/content=Am\+watching\+thou/, 'Casper.fill() textarea field was submitted');
test.assertUrlMatch(/check=on/, 'Casper.fill() input[type=checkbox] field was submitted');
test.assertUrlMatch(/choice=no/, 'Casper.fill() input[type=radio] field was submitted');
test.assertUrlMatch(/topic=bar/, 'Casper.fill() select field was submitted');
});
casper.run(function() {
test.done();
});
this.test.assertEvalEquals(function() {
return document.querySelector('input[name="email"]').value;
}, 'chuck@norris.com', 'Casper.fill() can fill an input[type=text] form field');
this.test.assertEvalEquals(function() {
return document.querySelector('input[name="password"]').value;
}, 'chuck', 'Casper.fill() can fill an input[type=password] form field');
this.test.assertEvalEquals(function() {
return document.querySelector('textarea[name="content"]').value;
}, 'Am watching thou', 'Casper.fill() can fill a textarea form field');
this.test.assertEvalEquals(function() {
return document.querySelector('select[name="topic"]').value;
}, 'bar', 'Casper.fill() can pick a value from a select form field');
this.test.assertEvalEquals(function() {
return document.querySelector('input[name="check"]').checked;
}, true, 'Casper.fill() can check a form checkbox');
this.test.assertEvalEquals(function() {
return document.querySelector('input[name="choice"][value="no"]').checked;
}, true, 'Casper.fill() can check a form radio button 1/2');
this.test.assertEvalEquals(function() {
return document.querySelector('input[name="choice"][value="yes"]').checked;
}, false, 'Casper.fill() can check a form radio button 2/2');
this.test.assertEvalEquals(function() {
return document.querySelector('input[name="file"]').files.length === 1;
}, true, 'Casper.fill() can select a file to upload');
this.test.assertEvalEquals(function() {
return (document.querySelector('input[name="checklist[]"][value="1"]').checked &&
!document.querySelector('input[name="checklist[]"][value="2"]').checked &&
document.querySelector('input[name="checklist[]"][value="3"]').checked);
}, true, 'Casper.fill() can fill a list of checkboxes');
});
casper.then(function() {
this.test.comment('Casper.getFormValues()');
this.test.assertEquals(this.getFormValues('form'), {
"check": true,
"checklist[]": ["1", "3"],
"choice": "no",
"content": "Am watching thou",
"email": "chuck@norris.com",
"file": "C:\\fakepath\\README.md",
"password": "chuck",
"submit": "submit",
"topic": "bar"
}, 'Casper.getFormValues() retrieves filled values');
this.test.comment('submitting form');
this.click('input[type="submit"]');
});
casper.then(function() {
this.test.comment('Form submitted');
this.test.assertUrlMatch(/email=chuck@norris.com/, 'Casper.fill() input[type=email] field was submitted');
this.test.assertUrlMatch(/password=chuck/, 'Casper.fill() input[type=password] field was submitted');
this.test.assertUrlMatch(/content=Am\+watching\+thou/, 'Casper.fill() textarea field was submitted');
this.test.assertUrlMatch(/check=on/, 'Casper.fill() input[type=checkbox] field was submitted');
this.test.assertUrlMatch(/choice=no/, 'Casper.fill() input[type=radio] field was submitted');
this.test.assertUrlMatch(/topic=bar/, 'Casper.fill() select field was submitted');
});
casper.thenOpen('tests/site/form.html', function() {
this.test.comment('Unexistent fields');
this.test.assertRaises(this.fill, ['form[action="result.html"]', {
unexistent: 42
}, true], 'Casper.fill() raises an exception when unable to fill a form');
casper.test.begin('unexistent fields', 1, function(test) {
casper.start('tests/site/form.html', function() {
test.assertRaises(this.fill, ['form[action="result.html"]', {
unexistent: 42
}, true], 'Casper.fill() raises an exception when unable to fill a form');
}).run(function() {
test.done();
});
});
// multiple forms
casper.thenOpen('tests/site/multiple-forms.html', function() {
this.test.comment('Multiple forms');
this.fill('form[name="f2"]', {
yo: "ok"
}, true);
}).then(function() {
this.test.assertUrlMatch(/\?f=f2&yo=ok$/, 'Casper.fill() handles multiple forms');
casper.test.begin('multiple forms', 1, function(test) {
casper.start('tests/site/multiple-forms.html', function() {
this.fill('form[name="f2"]', {
yo: "ok"
}, true);
}).then(function() {
test.assertUrlMatch(/\?f=f2&yo=ok$/, 'Casper.fill() handles multiple forms');
}).run(function() {
test.done();
});
});
// issue #267: array syntax field names
casper.thenOpen('tests/site/field-array.html', function() {
this.test.comment('Field arrays');
this.fill('form', {
'foo[bar]': "bar",
'foo[baz]': "baz"
}, true);
}).then(function() {
this.test.assertUrlMatch('?foo[bar]=bar&foo[baz]=baz', 'Casper.fill() handles array syntax field names');
casper.test.begin('field array', 1, function(test) {
// issue #267: array syntax field names
casper.start('tests/site/field-array.html', function() {
this.fill('form', {
'foo[bar]': "bar",
'foo[baz]': "baz"
}, true);
}).then(function() {
test.assertUrlMatch('?foo[bar]=bar&foo[baz]=baz',
'Casper.fill() handles array syntax field names');
}).run(function() {
test.done();
});
});
casper.run(function() {
this.test.done(19);
casper.test.begin('getFormValues() tests', 1, function(test) {
casper.start('tests/site/form.html', function() {
this.fill('form[action="result.html"]', {
email: 'chuck@norris.com',
password: 'chuck',
content: 'Am watching thou',
check: true,
choice: 'no',
topic: 'bar',
file: phantom.libraryPath + '/README.md',
'checklist[]': ['1', '3']
});
});
casper.then(function() {
test.assertEquals(this.getFormValues('form'), {
"check": true,
"checklist[]": ["1", "3"],
"choice": "no",
"content": "Am watching thou",
"email": "chuck@norris.com",
"file": "C:\\fakepath\\README.md",
"password": "chuck",
"submit": "submit",
"topic": "bar"
}, 'Casper.getFormValues() retrieves filled values');
});
casper.run(function() {
test.done();
});
});
......
/*global casper __utils__*/
/*jshint strict:false*/
casper.start('tests/site/frames.html');
casper.test.begin('handling frames', 16, function(test) {
casper.start('tests/site/frames.html');
casper.withFrame('frame1', function() {
this.test.assertTitle('CasperJS frame 1');
this.test.assertExists("#f1");
this.test.assertDoesntExist("#f2");
this.test.assertEval(function() {
return '__utils__' in window && 'getBinary' in __utils__;
}, '__utils__ object is available in child frame');
this.test.assertMatches(this.page.frameContent, /This is frame 1/);
this.test.assertMatches(this.getHTML(), /This is frame 1/);
});
casper.withFrame('frame1', function() {
test.assertTitle('CasperJS frame 1');
test.assertExists("#f1");
test.assertDoesntExist("#f2");
test.assertEval(function() {
return '__utils__' in window && 'getBinary' in __utils__;
}, '__utils__ object is available in child frame');
test.assertMatches(this.page.frameContent, /This is frame 1/);
test.assertMatches(this.getHTML(), /This is frame 1/);
});
casper.withFrame('frame2', function() {
this.test.assertTitle('CasperJS frame 2');
this.test.assertExists("#f2");
this.test.assertDoesntExist("#f1");
this.test.assertEval(function() {
return '__utils__' in window && 'getBinary' in __utils__;
}, '__utils__ object is available in other child frame');
this.clickLabel('frame 3');
});
casper.withFrame('frame2', function() {
test.assertTitle('CasperJS frame 2');
test.assertExists("#f2");
test.assertDoesntExist("#f1");
test.assertEval(function() {
return '__utils__' in window && 'getBinary' in __utils__;
}, '__utils__ object is available in other child frame');
this.clickLabel('frame 3');
});
casper.withFrame('frame2', function() {
this.test.assertTitle('CasperJS frame 3');
});
casper.withFrame('frame2', function() {
test.assertTitle('CasperJS frame 3');
});
casper.withFrame(0, function() {
this.test.assertTitle('CasperJS frame 1');
this.test.assertExists("#f1");
this.test.assertDoesntExist("#f2");
});
casper.withFrame(0, function() {
test.assertTitle('CasperJS frame 1');
test.assertExists("#f1");
test.assertDoesntExist("#f2");
});
casper.withFrame(1, function() {
this.test.assertTitle('CasperJS frame 3');
});
casper.withFrame(1, function() {
test.assertTitle('CasperJS frame 3');
});
casper.run(function() {
this.test.assertTitle('CasperJS test frames');
this.test.done(16);
casper.run(function() {
test.assertTitle('CasperJS test frames');
test.done();
});
});
......
/*global casper*/
/*jshint strict:false*/
casper.start('tests/site/global.html', function() {
this.test.comment('Casper.getGlobal()');
this.test.assertEquals(this.getGlobal('myGlobal'), 'awesome string',
'Casper.getGlobal() can retrieve a remote global variable');
this.test.assertEquals(this.getGlobal('myObject').foo.bar, 'baz',
'Casper.getGlobal() can retrieves a serializable object');
this.test.assertRaises(this.getGlobal, ['myUnencodableGlobal'],
'Casper.getGlobal() does not fail trying to encode an unserializable global');
});
casper.run(function() {
this.test.done(3);
casper.test.begin('getGLobal() tests', 3, function(test) {
casper.start('tests/site/global.html', function() {
test.assertEquals(this.getGlobal('myGlobal'), 'awesome string',
'Casper.getGlobal() can retrieve a remote global variable');
test.assertEquals(this.getGlobal('myObject').foo.bar, 'baz',
'Casper.getGlobal() can retrieves a serializable object');
test.assertRaises(this.getGlobal, ['myUnencodableGlobal'],
'Casper.getGlobal() does not fail trying to encode an unserializable global');
}).run(function() {
test.done();
});
});
......
/*global casper*/
/*jshint strict:false*/
casper.test.comment('Casper.headers.get()');
var server = require('webserver').create();
var service = server.listen(8090, function(request, response) {
response.statusCode = 200;
......@@ -14,28 +12,22 @@ var service = server.listen(8090, function(request, response) {
response.close();
});
function dumpHeaders() {
casper.test.comment('Dumping current response headers');
casper.currentResponse.headers.forEach(function(header) {
casper.test.comment('- ' + header.name + ': ' + header.value);
});
}
// local file:// url
casper.start('file://' + phantom.casperPath + 'tests/site/index.html', function thenLocalPage(response) {
this.test.assertEquals(response, undefined, 'No response available on local page');
});
casper.thenOpen('http://localhost:8090/', function thenLocalhost(response) {
var headers = response.headers;
this.test.assertEquals(headers.get('Content-Language'), 'en', 'Checking existing header (case sensitive)');
this.test.assertEquals(headers.get('content-language'), 'en', 'Checking existing header (case insensitive)');
this.test.assertEquals(headers.get('X-Is-Troll'), null, 'Checking unexisting header');
casper.test.begin('Casper.headers.get() using file protocol', 1, function(test) {
casper.start('file://' + phantom.casperPath + 'tests/site/index.html', function(response) {
test.assertEquals(response, undefined, 'No response available on local page');
}).run(function() {
test.done();
})
});
casper.run(function() {
server.close();
this.test.done(4);
casper.test.begin('Casper.headers.get() using http protocol', 3, function(test) {
casper.start('http://localhost:8090/', function(response) {
var headers = response.headers;
test.assertEquals(headers.get('Content-Language'), 'en', 'Checking existing header (case sensitive)');
test.assertEquals(headers.get('content-language'), 'en', 'Checking existing header (case insensitive)');
test.assertEquals(headers.get('X-Is-Troll'), null, 'Checking unexisting header');
}).run(function() {
server.close();
test.done();
})
});
......
/*global casper*/
/*jshint strict:false*/
casper.start('tests/site/page1.html');
casper.thenOpen('tests/site/page2.html');
casper.thenOpen('tests/site/page3.html');
casper.back();
casper.then(function() {
this.test.comment('navigating history backward');
this.test.assertMatch(this.getCurrentUrl(), /tests\/site\/page2\.html$/, 'Casper.back() can go back an history step');
});
casper.forward();
casper.then(function() {
this.test.comment('navigating history forward');
this.test.assertMatch(this.getCurrentUrl(), /tests\/site\/page3\.html$/, 'Casper.forward() can go forward an history step');
});
casper.run(function() {
this.test.assert(this.history.length > 0, 'Casper.history contains urls');
this.test.assertMatch(this.history[0], /tests\/site\/page1\.html$/, 'Casper.history has the correct first url');
this.test.done(4);
casper.test.begin('handling navigation history', 4, function(test) {
casper.start('tests/site/page1.html');
casper.thenOpen('tests/site/page2.html');
casper.thenOpen('tests/site/page3.html');
casper.back();
casper.then(function() {
test.assertMatch(this.getCurrentUrl(), /tests\/site\/page2\.html$/,
'Casper.back() can go back an history step');
});
casper.forward();
casper.then(function() {
test.assertMatch(this.getCurrentUrl(), /tests\/site\/page3\.html$/,
'Casper.forward() can go forward an history step');
});
casper.run(function() {
test.assert(this.history.length > 0, 'Casper.history contains urls');
test.assertMatch(this.history[0], /tests\/site\/page1\.html$/,
'Casper.history has the correct first url');
test.done();
});
});
......
/*global casper*/
/*jshint strict:false*/
// Dear curious test reader,
// The on* family of methods is considered deprecated since 0.6.0; please use events instead
// Casper.options.onStepComplete
casper.start('tests/site/index.html', function() {
this.options.onStepComplete = function(self, stepResult) {
this.test.comment('Casper.options.onStepComplete()');
this.test.assertEquals(stepResult, 'ok', 'Casper.options.onStepComplete() is called on step complete');
self.options.onStepComplete = null;
casper.test.begin('onStepComplete() hook tests', 1, function(test) {
var stepResults = [];
casper.options.onStepComplete = function(self, stepResult) {
stepResults.push(stepResult);
};
return 'ok';
casper.start('tests/site/index.html', function() {
return 'ok';
});
casper.run(function() {
test.assert(stepResults.indexOf('ok') > -1,
'Casper.options.onStepComplete() is called on step complete');
this.options.onStepComplete = undefined;
test.done();
});
});
// Casper.options.onResourceRequested & Casper.options.onResourceReceived
casper.then(function() {
this.options.onResourceReceived = function(self, resource) {
this.test.comment('Casper.options.onResourceReceived()');
this.test.assertType(resource, 'object', 'Casper.options.onResourceReceived() retrieve a resource object');
this.test.assert('status' in resource, 'Casper.options.onResourceReceived() retrieve a valid resource object');
self.options.onResourceReceived = null;
casper.test.begin('onResourceRequested() & onResourceReceived() hook tests', 6, function(test) {
var requests = [], responses = [];
casper.options.onResourceRequested = function(self, request) {
requests.push(request);
};
this.options.onResourceRequested = function(self, request) {
this.test.comment('Casper.options.onResourceRequested()');
this.test.assertType(request, 'object', 'Casper.options.onResourceRequested() retrieve a request object');
this.test.assert('method' in request, 'Casper.options.onResourceRequested() retrieve a valid request object');
self.options.onResourceRequested = null;
casper.options.onResourceReceived = function(self, response) {
responses.push(response);
};
this.thenOpen('tests/site/page1.html');
casper.start('tests/site/index.html', function() {
test.assert(requests.some(function(request) {
return (/index\.html$/).test(request.url);
}), 'onResourceRequested() receives page requests');
test.assert(requests.some(function(request) {
return (/phantom\.png$/).test(request.url);
}), 'onResourceRequested() receives image requests');
test.assert(responses.some(function(response) {
return response.stage === 'start' && (/index\.html$/).test(response.url);
}), 'onResourceReceived() receives page response on load start');
test.assert(responses.some(function(response) {
return response.stage === 'end' && (/index\.html$/).test(response.url);
}), 'onResourceReceived() receives page response on load end');
test.assert(responses.some(function(response) {
return response.stage === 'start' && (/phantom\.png$/).test(response.url);
}), 'onResourceReceived() receives image response on load start');
test.assert(responses.some(function(response) {
return response.stage === 'end' && (/phantom\.png$/).test(response.url);
}), 'onResourceReceived() receives image response on load end');
});
casper.run(function() {
this.options.onResourceReceived = this.options.onResourceRequested = undefined;
test.done();
});
});
// Casper.options.onAlert()
casper.then(function() {
this.options.onAlert = function(self, message) {
self.test.assertEquals(message, 'plop', 'Casper.options.onAlert() can intercept an alert message');
casper.test.begin('onAlert() hook tests', 1, function(test) {
var message;
casper.options.onAlert = function(self, msg) {
message = msg;
};
});
casper.run(function() {
this.options.onAlert = null;
this.test.done(5);
casper.start('tests/site/alert.html', function() {
test.assertEquals(message, 'plop', 'Casper.options.onAlert() can intercept an alert message');
});
casper.run(function() {
this.options.onAlert = null;
test.done();
});
});
......
/*jshint strict:false*/
/*global CasperError casper console phantom require*/
casper.start('tests/site/form.html', function() {
this.sendKeys('input[name="email"]', 'duke@nuk.em');
this.sendKeys('textarea', "Damn, I’m looking good.");
var values = this.getFormValues('form');
this.test.assertEquals(values['email'], 'duke@nuk.em',
'Casper.sendKeys() sends keys to given input');
this.test.assertEquals(values['content'], "Damn, I’m looking good.",
'Casper.sendKeys() sends keys to given textarea');
});
casper.run(function() {
this.test.done(2);
casper.test.begin('sendKeys() tests', 2, function(test) {
casper.start('tests/site/form.html', function() {
this.sendKeys('input[name="email"]', 'duke@nuk.em');
this.sendKeys('textarea', "Damn, I’m looking good.");
var values = this.getFormValues('form');
test.assertEquals(values.email, 'duke@nuk.em',
'Casper.sendKeys() sends keys to given input');
test.assertEquals(values.content, "Damn, I’m looking good.",
'Casper.sendKeys() sends keys to given textarea');
}).run(function() {
test.done();
});
});
......
......@@ -5,17 +5,17 @@ if (phantom.version.major === 1 && phantom.version.minor < 8) {
casper.warn('document.location is broken under phantomjs < 1.8');
casper.test.done();
} else {
casper.start('tests/site/index.html', function() {
this.evaluate(function() {
document.location = '/tests/site/form.html';
casper.test.begin('document.location tests', 1, function(test) {
casper.start('tests/site/index.html', function() {
this.evaluate(function() {
document.location = '/tests/site/form.html';
});
});
casper.then(function() {
test.assertUrlMatches(/form\.html$/, 'document.location works as expected');
});
casper.run(function() {
test.done();
});
});
casper.then(function() {
this.test.assertUrlMatches(/form\.html$/);
});
casper.run(function() {
this.test.done();
});
}
......
/*jshint strict:false*/
/*global casper __utils__*/
casper.start('tests/site/index.html');
casper.test.begin('logging tests', 4, function(test) {
casper.start('tests/site/index.html');
var oldLevel = casper.options.logLevel;
casper.options.logLevel = 'info';
casper.options.verbose = false;
var oldLevel = casper.options.logLevel;
casper.test.comment('Casper.log()');
casper.log('foo', 'info');
casper.test.assert(casper.result.log.some(function(e) {
return e.message === 'foo' && e.level === 'info';
}), 'Casper.log() adds a log entry');
casper.options.logLevel = 'info';
casper.options.verbose = false;
casper.options.logLevel = oldLevel;
casper.options.verbose = true;
casper.log('foo', 'info');
casper.test.assert(casper.result.log.some(function(e) {
return e.message === 'foo' && e.level === 'info';
}), 'Casper.log() adds a log entry');
casper.then(function() {
var oldLevel = casper.options.logLevel;
casper.options.logLevel = 'debug';
casper.options.verbose = false;
casper.evaluate(function() {
__utils__.log('debug message');
__utils__.log('info message', 'info');
});
this.test.assert(casper.result.log.some(function(e) {
return e.message === 'debug message' && e.level === 'debug' && e.space === 'remote';
}), 'ClientUtils.log() adds a log entry');
this.test.assert(casper.result.log.some(function(e) {
return e.message === 'info message' && e.level === 'info' && e.space === 'remote';
}), 'ClientUtils.log() adds a log entry at a given level');
casper.options.logLevel = oldLevel;
casper.options.verbose = true;
});
casper.run(function() {
this.test.assertEquals(this.result.log.length, 3, 'Casper.log() logged messages');
this.test.done(4);
casper.then(function() {
var oldLevel = casper.options.logLevel;
casper.options.logLevel = 'debug';
casper.options.verbose = false;
casper.evaluate(function() {
__utils__.log('debug message');
__utils__.log('info message', 'info');
});
test.assert(casper.result.log.some(function(e) {
return e.message === 'debug message' && e.level === 'debug' && e.space === 'remote';
}), 'ClientUtils.log() adds a log entry');
test.assert(casper.result.log.some(function(e) {
return e.message === 'info message' && e.level === 'info' && e.space === 'remote';
}), 'ClientUtils.log() adds a log entry at a given level');
casper.options.logLevel = oldLevel;
casper.options.verbose = true;
});
casper.run(function() {
test.assertEquals(this.result.log.length, 3, 'Casper.log() logged messages');
test.done();
});
});
......
/*global casper*/
/*jshint strict:false maxstatements:99*/
casper.start('tests/site/mouse-events.html');
casper.test.begin('mouseEvent() tests', 16, function(test) {
casper.start('tests/site/mouse-events.html', function() {
test.assert(this.mouseEvent('mousedown', '#test1'), 'Casper.mouseEvent() can dispatch a mousedown event');
test.assert(this.mouseEvent('mousedown', '#test2'), 'Casper.mouseEvent() can dispatch a mousedown event handled by unobstrusive js');
test.assert(this.mouseEvent('mouseup', '#test3'), 'Casper.mouseEvent() can dispatch a mouseup event');
test.assert(this.mouseEvent('mouseup', '#test4'), 'Casper.mouseEvent() can dispatch a mouseup event handled by unobstrusive js');
test.assert(this.mouseEvent('mouseover', '#test5'), 'Casper.mouseEvent() can dispatch a mouseover event');
test.assert(this.mouseEvent('mouseover', '#test6'), 'Casper.mouseEvent() can dispatch a mouseover event handled by unobstrusive js');
test.assert(this.mouseEvent('mouseout', '#test7'), 'Casper.mouseEvent() can dispatch a mouseout event');
test.assert(this.mouseEvent('mouseout', '#test8'), 'Casper.mouseEvent() can dispatch a mouseout event handled by unobstrusive js');
});
casper.then(function() {
this.test.comment('CasperUtils.mouseEvent()');
this.test.assert(this.mouseEvent('mousedown', '#test1'), 'CasperUtils.mouseEvent() can dispatch a mousedown event');
this.test.assert(this.mouseEvent('mousedown', '#test2'), 'CasperUtils.mouseEvent() can dispatch a mousedown event handled by unobstrusive js');
this.test.assert(this.mouseEvent('mouseup', '#test3'), 'CasperUtils.mouseEvent() can dispatch a mouseup event');
this.test.assert(this.mouseEvent('mouseup', '#test4'), 'CasperUtils.mouseEvent() can dispatch a mouseup event handled by unobstrusive js');
this.test.assert(this.mouseEvent('mouseover', '#test5'), 'CasperUtils.mouseEvent() can dispatch a mouseover event');
this.test.assert(this.mouseEvent('mouseover', '#test6'), 'CasperUtils.mouseEvent() can dispatch a mouseover event handled by unobstrusive js');
this.test.assert(this.mouseEvent('mouseout', '#test7'), 'CasperUtils.mouseEvent() can dispatch a mouseout event');
this.test.assert(this.mouseEvent('mouseout', '#test8'), 'CasperUtils.mouseEvent() can dispatch a mouseout event handled by unobstrusive js');
casper.then(function() {
var results = this.getGlobal('results');
test.assert(results.test1, 'Casper.mouseEvent() triggered mousedown');
test.assert(results.test2, 'Casper.mouseEvent() triggered mousedown via unobstrusive js');
test.assert(results.test3, 'Casper.mouseEvent() triggered mouseup');
test.assert(results.test4, 'Casper.mouseEvent() triggered mouseup via unobstrusive js');
test.assert(results.test5, 'Casper.mouseEvent() triggered mouseover');
test.assert(results.test6, 'Casper.mouseEvent() triggered mouseover via unobstrusive js');
test.assert(results.test7, 'Casper.mouseEvent() triggered mouseout');
test.assert(results.test8, 'Casper.mouseEvent() triggered mouseout via unobstrusive js');
});
var results = this.getGlobal('results');
this.test.assert(results.test1, 'CasperUtils.mouseEvent() triggered mousedown');
this.test.assert(results.test2, 'CasperUtils.mouseEvent() triggered mousedown via unobstrusive js');
this.test.assert(results.test3, 'CasperUtils.mouseEvent() triggered mouseup');
this.test.assert(results.test4, 'CasperUtils.mouseEvent() triggered mouseup via unobstrusive js');
this.test.assert(results.test5, 'CasperUtils.mouseEvent() triggered mouseover');
this.test.assert(results.test6, 'CasperUtils.mouseEvent() triggered mouseover via unobstrusive js');
this.test.assert(results.test7, 'CasperUtils.mouseEvent() triggered mouseout');
this.test.assert(results.test8, 'CasperUtils.mouseEvent() triggered mouseout via unobstrusive js');
});
casper.run(function() {
this.test.done(16);
casper.run(function() {
test.done();
});
});
......
/*global casper*/
/*jshint strict:false*/
casper.test.comment("page.error event");
var error = {};
casper.start();
casper.on("page.error", function onError(msg, trace) {
error.msg = msg;
error.trace = trace;
});
casper.thenOpen('tests/site/error.html', function() {
this.test.assertEquals(error.msg, "ReferenceError: Can't find variable: plop", 'page.error event has been caught OK');
this.test.assertMatch(error.trace[0].file, /error.html/, 'page.error retrieves correct stack trace');
});
casper.run(function() {
this.test.done(2);
casper.test.begin('page.error event tests', 2, function(test) {
var error = {};
casper.on("page.error", function onError(msg, trace) {
error.msg = msg;
error.trace = trace;
});
casper.start('tests/site/error.html', function() {
test.assertEquals(error.msg, "ReferenceError: Can't find variable: plop",
"page.error event has been caught OK");
test.assertMatch(error.trace[0].file, /error.html/,
"page.error retrieves correct stack trace");
});
casper.run(function() {
test.done();
});
});
......
/*global casper*/
/*jshint strict:false*/
var t = casper.test, current = 0, tests = [
function(settings) {
t.assertEquals(settings, {
method: "get"
}, "Casper.open() used the expected GET settings");
},
function(settings) {
t.assertEquals(settings, {
method: "post",
data: "plop=42&chuck=norris"
}, "Casper.open() used the expected POST settings");
},
function(settings) {
t.assertEquals(settings, {
method: "put",
data: "plop=42&chuck=norris"
}, "Casper.open() used the expected PUT settings");
},
function(settings) {
t.assertEquals(settings, {
method: "get",
username: 'bob',
password: 'sinclar'
}, "Casper.open() used the expected HTTP auth settings");
},
function(settings) {
t.assertEquals(settings, {
method: "get"
}, "Casper.thenOpen() used the expected GET settings");
},
function(settings) {
t.assertEquals(settings, {
method: "post",
data: "plop=42&chuck=norris"
}, "Casper.thenOpen() used the expected POST settings");
},
function(settings) {
t.assertEquals(settings, {
method: "put",
data: "plop=42&chuck=norris"
}, "Casper.thenOpen() used the expected PUT settings");
},
function(settings) {
t.assertEquals(settings, {
method: "get",
username: 'bob',
password: 'sinclar'
}, "Casper.thenOpen() used the expected HTTP auth settings");
}
];
casper.test.begin('open() tests', 16, function(test) {
var current = 0,
tests = [
function(settings) {
test.assertEquals(settings, {
method: "get"
}, "Casper.open() used the expected GET settings");
},
function(settings) {
test.assertEquals(settings, {
method: "post",
data: "plop=42&chuck=norris"
}, "Casper.open() used the expected POST settings");
},
function(settings) {
test.assertEquals(settings, {
method: "put",
data: "plop=42&chuck=norris"
}, "Casper.open() used the expected PUT settings");
},
function(settings) {
test.assertEquals(settings, {
method: "get",
username: 'bob',
password: 'sinclar'
}, "Casper.open() used the expected HTTP auth settings");
},
function(settings) {
test.assertEquals(settings, {
method: "get"
}, "Casper.thenOpen() used the expected GET settings");
},
function(settings) {
test.assertEquals(settings, {
method: "post",
data: "plop=42&chuck=norris"
}, "Casper.thenOpen() used the expected POST settings");
},
function(settings) {
test.assertEquals(settings, {
method: "put",
data: "plop=42&chuck=norris"
}, "Casper.thenOpen() used the expected PUT settings");
},
function(settings) {
test.assertEquals(settings, {
method: "get",
username: 'bob',
password: 'sinclar'
}, "Casper.thenOpen() used the expected HTTP auth settings");
}
];
casper.start();
casper.start().on('open', function(url, settings) {
tests[current++](settings);
});
casper.on('open', function(url, settings) {
tests[current++](settings);
});
// GET
casper.open('tests/site/index.html').then(function() {
test.pass("Casper.open() can open and load a location using GET");
});
// GET
casper.open('tests/site/index.html').then(function() {
t.pass("Casper.open() can open and load a location using GET");
});
// POST
casper.open('tests/site/index.html', {
method: 'post',
data: {
plop: 42,
chuck: 'norris'
}
}).then(function() {
test.pass("Casper.open() can open and load a location using POST");
});
// POST
casper.open('tests/site/index.html', {
method: 'post',
data: {
plop: 42,
chuck: 'norris'
}
}).then(function() {
t.pass("Casper.open() can open and load a location using POST");
});
// PUT
casper.open('tests/site/index.html', {
method: 'put',
data: {
plop: 42,
chuck: 'norris'
}
}).then(function() {
t.pass("Casper.open() can open and load a location using PUT");
});
// PUT
casper.open('tests/site/index.html', {
method: 'put',
data: {
plop: 42,
chuck: 'norris'
}
}).then(function() {
test.pass("Casper.open() can open and load a location using PUT");
});
// HTTP Auth
casper.open('tests/site/index.html', {
method: 'get',
username: 'bob',
password: 'sinclar'
}).then(function() {
t.pass("Casper.open() can open and load a location using HTTP auth");
});
// HTTP Auth
casper.open('tests/site/index.html', {
method: 'get',
username: 'bob',
password: 'sinclar'
}).then(function() {
test.pass("Casper.open() can open and load a location using HTTP auth");
});
// GET with thenOpen
casper.thenOpen('tests/site/index.html').then(function() {
t.pass("Casper.thenOpen() can open and load a location using GET");
});
// GET with thenOpen
casper.thenOpen('tests/site/index.html').then(function() {
test.pass("Casper.thenOpen() can open and load a location using GET");
});
// POST with thenOpen
casper.thenOpen('tests/site/index.html', {
method: 'post',
data: {
plop: 42,
chuck: 'norris'
}
}, function() {
t.pass("Casper.thenOpen() can open and load a location using POST");
});
// POST with thenOpen
casper.thenOpen('tests/site/index.html', {
method: 'post',
data: {
plop: 42,
chuck: 'norris'
}
}, function() {
test.pass("Casper.thenOpen() can open and load a location using POST");
});
// PUT with thenOpen
casper.thenOpen('tests/site/index.html', {
method: 'put',
data: {
plop: 42,
chuck: 'norris'
}
}, function() {
t.pass("Casper.thenOpen() can open and load a location using PUT");
});
// PUT with thenOpen
casper.thenOpen('tests/site/index.html', {
method: 'put',
data: {
plop: 42,
chuck: 'norris'
}
}, function() {
test.pass("Casper.thenOpen() can open and load a location using PUT");
});
// HTTP Auth with thenOpen
casper.thenOpen('tests/site/index.html', {
method: 'get',
username: 'bob',
password: 'sinclar'
}, function() {
t.pass("Casper.thenOpen() can open and load a location using HTTP auth");
});
// HTTP Auth with thenOpen
casper.thenOpen('tests/site/index.html', {
method: 'get',
username: 'bob',
password: 'sinclar'
}, function() {
test.pass("Casper.thenOpen() can open and load a location using HTTP auth");
});
casper.run(function() {
this.removeAllListeners('open');
t.done(16);
casper.run(function() {
this.removeAllListeners('open');
test.done();
});
});
......
/*jshint strict:false*/
/*jshint strict:false maxstatements:99*/
/*global CasperError casper console phantom require*/
var utils = require('utils');
var x = require('casper').selectXPath;
casper.on('popup.created', function(popup) {
this.test.pass('"popup.created" event is fired');
this.test.assert(utils.isWebPage(popup),
'"popup.created" event callback get a popup page instance');
});
casper.on('popup.loaded', function(popup) {
this.test.pass('"popup.loaded" event is fired');
this.test.assertEquals(popup.evaluate(function() {
return document.title;
}), 'CasperJS test index',
'"popup.loaded" is triggered when popup content is actually loaded');
});
casper.on('popup.closed', function(popup) {
this.test.assertEquals(this.popups.length, 0, '"popup.closed" event is fired');
});
casper.start('tests/site/popup.html');
casper.waitForPopup('index.html', function() {
this.test.pass('Casper.waitForPopup() waits for a popup being created');
this.test.assertEquals(this.popups.length, 1, 'A popup has been added');
this.test.assert(utils.isWebPage(this.popups[0]), 'A popup is a WebPage');
});
casper.withPopup('index.html', function() {
this.test.assertUrlMatches(/index\.html$/,
'Casper.withPopup() switched to popup as current active one');
this.test.assertEval(function() {
return '__utils__' in window;
}, 'Casper.withPopup() has client utils injected');
this.test.assertExists('h1',
'Casper.withPopup() can perform assertions on the DOM');
this.test.assertExists(x('//h1'),
'Casper.withPopup() can perform assertions on the DOM using XPath');
});
casper.then(function() {
this.test.assertUrlMatches(/popup\.html$/,
'Casper.withPopup() has reverted to main page after using the popup');
});
casper.thenClick('.close', function() {
this.test.assertEquals(this.popups.length, 0, 'Popup is removed when closed');
});
casper.thenOpen('tests/site/popup.html');
casper.waitForPopup(/index\.html$/, function() {
this.test.pass('Casper.waitForPopup() waits for a popup being created');
});
casper.withPopup(/index\.html$/, function() {
this.test.assertTitle('CasperJS test index',
'Casper.withPopup() can use a regexp to identify popup');
});
casper.thenClick('.close', function() {
this.test.assertUrlMatches(/popup\.html$/,
'Casper.withPopup() has reverted to main page after using the popup');
this.test.assertEquals(this.popups.length, 0, 'Popup is removed when closed');
this.removeAllListeners('popup.created');
this.removeAllListeners('popup.loaded');
this.removeAllListeners('popup.closed');
});
casper.thenClick('a[target="_blank"]');
casper.waitForPopup('form.html', function() {
this.test.pass('Casper.waitForPopup() waits when clicked on a link with target=_blank');
});
casper.withPopup('form.html', function() {
this.test.assertTitle('CasperJS test form');
});
casper.run(function() {
// removes event listeners as they've now been tested already
this.test.done(25);
casper.test.begin('popup tests', 25, function(test) {
casper.on('popup.created', function(popup) {
test.pass('"popup.created" event is fired');
test.assert(utils.isWebPage(popup),
'"popup.created" event callback get a popup page instance');
});
casper.on('popup.loaded', function(popup) {
test.pass('"popup.loaded" event is fired');
test.assertEquals(popup.evaluate(function() {
return document.title;
}), 'CasperJS test index',
'"popup.loaded" is triggered when popup content is actually loaded');
});
casper.on('popup.closed', function(popup) {
test.assertEquals(this.popups.length, 0, '"popup.closed" event is fired');
});
casper.start('tests/site/popup.html');
casper.waitForPopup('index.html', function() {
test.pass('Casper.waitForPopup() waits for a popup being created');
test.assertEquals(this.popups.length, 1, 'A popup has been added');
test.assert(utils.isWebPage(this.popups[0]), 'A popup is a WebPage');
});
casper.withPopup('index.html', function() {
test.assertUrlMatches(/index\.html$/,
'Casper.withPopup() switched to popup as current active one');
test.assertEval(function() {
return '__utils__' in window;
}, 'Casper.withPopup() has client utils injected');
test.assertExists('h1',
'Casper.withPopup() can perform assertions on the DOM');
test.assertExists(x('//h1'),
'Casper.withPopup() can perform assertions on the DOM using XPath');
});
casper.then(function() {
test.assertUrlMatches(/popup\.html$/,
'Casper.withPopup() has reverted to main page after using the popup');
});
casper.thenClick('.close', function() {
test.assertEquals(this.popups.length, 0, 'Popup is removed when closed');
});
casper.thenOpen('tests/site/popup.html');
casper.waitForPopup(/index\.html$/, function() {
test.pass('Casper.waitForPopup() waits for a popup being created');
});
casper.withPopup(/index\.html$/, function() {
test.assertTitle('CasperJS test index',
'Casper.withPopup() can use a regexp to identify popup');
});
casper.thenClick('.close', function() {
test.assertUrlMatches(/popup\.html$/,
'Casper.withPopup() has reverted to main page after using the popup');
test.assertEquals(this.popups.length, 0, 'Popup is removed when closed');
this.removeAllListeners('popup.created');
this.removeAllListeners('popup.loaded');
this.removeAllListeners('popup.closed');
});
casper.thenClick('a[target="_blank"]');
casper.waitForPopup('form.html', function() {
test.pass('Casper.waitForPopup() waits when clicked on a link with target=_blank');
});
casper.withPopup('form.html', function() {
test.assertTitle('CasperJS test form');
});
casper.run(function() {
test.done();
});
});
......
/*global casper*/
/*jshint strict:false*/
casper.setFilter('page.prompt', function(message, value) {
return 'Chuck ' + value;
});
casper.start('tests/site/prompt.html', function() {
this.test.assertEquals(this.getGlobal('name'), 'Chuck Norris', 'prompted value has been received');
});
casper.run(function() {
this.test.done(1);
casper.test.begin('prompt tests', 1, function(test) {
casper.setFilter('page.prompt', function(message, value) {
return 'Chuck ' + value;
});
casper.start('tests/site/prompt.html', function() {
test.assertEquals(this.getGlobal('name'), 'Chuck Norris', 'prompted value has been received');
}).run(function() {
test.done();
});
});
......
......@@ -4,33 +4,39 @@ function testHeader(header) {
return header.name === 'Accept' && header.value === 'application/json';
}
var t = casper.test, current = 0, tests = [
function(request) {
t.assertNot(request.headers.some(testHeader), "Casper.open() sets no custom header by default");
},
function(request) {
t.assert(request.headers.some(testHeader), "Casper.open() can set a custom header");
},
function(request) {
t.assertNot(request.headers.some(testHeader), "Casper.open() custom headers option is not persistent");
}
];
casper.test.begin('requests tests', 3, function(test) {
var current = 0,
tests = [
function(request) {
test.assertNot(request.headers.some(testHeader),
"Casper.open() sets no custom header by default");
},
function(request) {
test.assert(request.headers.some(testHeader),
"Casper.open() can set a custom header");
},
function(request) {
test.assertNot(request.headers.some(testHeader),
"Casper.open() custom headers option is not persistent");
}
];
casper.on('page.resource.requested', function(request) {
tests[current++](request);
});
casper.on('page.resource.requested', function(request) {
tests[current++](request);
});
casper.start();
casper.start('tests/site/index.html');
casper.thenOpen('tests/site/index.html');
casper.thenOpen('tests/site/index.html', {
headers: {
Accept: 'application/json'
}
});
casper.thenOpen('tests/site/index.html');
casper.thenOpen('tests/site/index.html', {
headers: {
Accept: 'application/json'
}
});
casper.thenOpen('tests/site/index.html');
casper.run(function() {
this.removeAllListeners('page.resource.requested');
t.done(3);
casper.run(function() {
this.removeAllListeners('page.resource.requested');
test.done();
});
});
......
casper.start "tests/site/resources.html", ->
@test.assertEquals @resources.length, 1, "only one resource found"
onTime = ->
@test.assertEquals(
@resources.length
2
"two resources found"
)
@test.assertResourceExists(
/dummy\.js/i
"phantom image found via test RegExp"
)
@test.assertResourceExists(
(res) -> res.url.match "dummy.js"
"phantom image found via test Function"
)
@test.assertResourceExists(
"dummy.js"
"phantom image found via test String"
)
onTimeout = -> @test.fail "waitForResource timeout occured"
@waitForResource "dummy.js", onTime, onTimeout
casper.test.begin "resources tests", 5, (test) ->
casper.start "tests/site/resources.html", ->
test.assertEquals @resources.length, 1, "only one resource found"
casper.run(-> @test.done(5))
onTime = ->
test.assertEquals(
@resources.length
2
"two resources found"
)
test.assertResourceExists(
/dummy\.js/i
"phantom image found via test RegExp"
)
test.assertResourceExists(
(res) -> res.url.match "dummy.js"
"phantom image found via test Function"
)
test.assertResourceExists(
"dummy.js"
"phantom image found via test String"
)
onTimeout = -> test.fail "waitForResource timeout occured"
@waitForResource "dummy.js", onTime, onTimeout
casper.run(-> test.done())
......
/*global casper*/
/*jshint strict:false*/
casper.options.remoteScripts = [
'includes/include1.js', // local includes are actually served
'includes/include2.js', // through the local test webserver
'http://code.jquery.com/jquery-1.8.3.min.js'
];
casper.test.begin('remote script includes tests', 6, function(test) {
casper.options.remoteScripts = [
'includes/include1.js', // local includes are actually served
'includes/include2.js', // through the local test webserver
'http://code.jquery.com/jquery-1.8.3.min.js'
];
casper.start('tests/site/index.html', function() {
this.test.assertSelectorHasText('#include1', 'include1',
'Casper.includeRemoteScripts() includes a first remote script on start');
this.test.assertSelectorHasText('#include2', 'include2',
'Casper.includeRemoteScripts() includes a second remote script on start');
this.test.assertEval(function() {
return 'jQuery' in window;
}, 'Casper.includeRemoteScripts() includes a really remote file on first step');
});
casper.start('tests/site/index.html', function() {
test.assertSelectorHasText('#include1', 'include1',
'Casper.includeRemoteScripts() includes a first remote script on start');
test.assertSelectorHasText('#include2', 'include2',
'Casper.includeRemoteScripts() includes a second remote script on start');
test.assertEval(function() {
return 'jQuery' in window;
}, 'Casper.includeRemoteScripts() includes a really remote file on first step');
});
casper.thenOpen('tests/site/form.html', function() {
this.test.assertSelectorHasText('#include1', 'include1',
'Casper.includeRemoteScripts() includes a first remote script on second step');
this.test.assertSelectorHasText('#include2', 'include2',
'Casper.includeRemoteScripts() includes a second remote script on second step');
this.test.assertEval(function() {
return 'jQuery' in window;
}, 'Casper.includeRemoteScripts() includes a really remote file on second step');
});
casper.thenOpen('tests/site/form.html', function() {
test.assertSelectorHasText('#include1', 'include1',
'Casper.includeRemoteScripts() includes a first remote script on second step');
test.assertSelectorHasText('#include2', 'include2',
'Casper.includeRemoteScripts() includes a second remote script on second step');
test.assertEval(function() {
return 'jQuery' in window;
}, 'Casper.includeRemoteScripts() includes a really remote file on second step');
});
casper.run(function() {
this.options.remoteScripts = [];
this.test.done(6);
casper.run(function() {
this.options.remoteScripts = [];
test.done();
});
});
......
/*global casper*/
/*jshint strict:false*/
casper.test.comment('Casper.start()');
casper.test.begin('start() tests', 4, function(test) {
casper.start('tests/site/index.html', function() {
test.pass('Casper.start() can chain a next step');
test.assertTitle('CasperJS test index', 'Casper.start() opened the passed url');
test.assertEval(function() {
return typeof(__utils__) === "object";
}, 'Casper.start() injects ClientUtils instance within remote DOM');
});
casper.start('tests/site/index.html', function() {
this.test.pass('Casper.start() can chain a next step');
this.test.assertTitle('CasperJS test index', 'Casper.start() opened the passed url');
this.test.assertEval(function() {
return typeof(__utils__) === "object";
}, 'Casper.start() injects ClientUtils instance within remote DOM');
});
casper.test.assert(casper.started, 'Casper.start() started');
test.assert(casper.started, 'Casper.start() started');
casper.run(function() {
this.test.done(4);
casper.run(function() {
test.done();
});
});
......
/*global casper*/
/*jshint strict:false*/
casper.test.comment('Casper.then()');
casper.test.begin('steps tests', 8, function(test) {
casper.start('tests/site/index.html');
casper.start('tests/site/index.html');
var nsteps = casper.steps.length;
var nsteps = casper.steps.length;
casper.then(function(response) {
test.assertTitle('CasperJS test index',
'Casper.then() added a new step');
});
casper.then(function(response) {
this.test.assertTitle('CasperJS test index', 'Casper.then() added a new step');
});
casper.test.assertEquals(casper.steps.length, nsteps + 1, 'Casper.then() can add a new step');
test.assertEquals(casper.steps.length, nsteps + 1,
'Casper.then() can add a new step');
casper.test.comment('Casper.thenOpen()');
casper.thenOpen('tests/site/test.html');
casper.thenOpen('tests/site/test.html');
test.assertEquals(casper.steps.length, nsteps + 2,
'Casper.thenOpen() can add a new step');
casper.test.assertEquals(casper.steps.length, nsteps + 2, 'Casper.thenOpen() can add a new step');
casper.thenOpen('tests/site/test.html', function() {
test.assertTitle('CasperJS test target',
'Casper.thenOpen() opened a location and executed a step');
});
casper.thenOpen('tests/site/test.html', function() {
this.test.assertTitle('CasperJS test target', 'Casper.thenOpen() opened a location and executed a step');
});
test.assertEquals(casper.steps.length, nsteps + 4,
'Casper.thenOpen() can add a new step for opening, plus another step');
casper.test.assertEquals(casper.steps.length, nsteps + 4, 'Casper.thenOpen() can add a new step for opening, plus another step');
casper.test.comment('Casper.each()');
casper.each([1, 2, 3], function(self, item, i) {
self.test.assertEquals(i, item - 1, 'Casper.each() passes a contextualized index');
});
casper.each([1, 2, 3], function(self, item, i) {
test.assertEquals(i, item - 1,
'Casper.each() passes a contextualized index');
});
casper.run(function() {
this.test.done(8);
casper.run(function() {
test.done();
});
});
......
/*global casper*/
/*jshint strict:false*/
casper.start('tests/site/urls.html', function() {
this.clickLabel('raw unicode', 'a');
}).then(function() {
this.test.assertHttpStatus(200);
this.test.assertUrlMatches('Forlì', 'Casper.getCurrentUrl() retrieves a raw unicode URL');
this.clickLabel('escaped', 'a');
});
casper.test.begin('urls tests', 6, function(test) {
casper.start('tests/site/urls.html', function() {
this.clickLabel('raw unicode', 'a');
});
casper.then(function() {
this.test.assertHttpStatus(200);
this.test.assertUrlMatches('Forlì', 'Casper.getCurrentUrl() retrieves an escaped URL');
this.clickLabel('uri encoded', 'a');
});
casper.then(function() {
test.assertHttpStatus(200);
test.assertUrlMatches('Forlì', 'Casper.getCurrentUrl() retrieves a raw unicode URL');
this.clickLabel('escaped', 'a');
});
casper.then(function() {
test.assertHttpStatus(200);
test.assertUrlMatches('Forlì', 'Casper.getCurrentUrl() retrieves an escaped URL');
this.clickLabel('uri encoded', 'a');
});
casper.run(function() {
this.test.assertHttpStatus(200);
this.test.assertUrlMatches('Forlì', 'Casper.getCurrentUrl() retrieves a decoded URL');
this.test.done(6);
casper.run(function() {
test.assertHttpStatus(200);
test.assertUrlMatches('Forlì', 'Casper.getCurrentUrl() retrieves a decoded URL');
test.done();
});
});
......
/*global casper*/
/*jshint strict:false*/
casper.test.comment('Casper.viewport()');
casper.start();
casper.viewport(1337, 999);
casper.test.assertEquals(casper.page.viewportSize.width, 1337, 'Casper.viewport() can change the width of page viewport');
casper.test.assertEquals(casper.page.viewportSize.height, 999, 'Casper.viewport() can change the height of page viewport');
casper.test.assertRaises(casper.viewport, ['a', 'b'], 'Casper.viewport() validates viewport size data');
casper.test.done(3);
casper.test.begin('viewport() tests', 3, function(test) {
casper.start();
casper.viewport(1337, 999);
test.assertEquals(casper.page.viewportSize.width, 1337,
'Casper.viewport() can change the width of page viewport');
test.assertEquals(casper.page.viewportSize.height, 999,
'Casper.viewport() can change the height of page viewport');
test.assertRaises(casper.viewport, ['a', 'b'],
'Casper.viewport() validates viewport size data');
test.done();
});
......
/*global casper*/
/*jshint strict:false*/
casper.start('tests/site/visible.html', function() {
this.test.comment('Casper.visible()');
this.test.assert(this.visible('#img1'), 'Casper.visible() can detect if an element is visible');
this.test.assert(!this.visible('#img2'), 'Casper.visible() can detect if an element is invisible');
this.test.assert(!this.visible('#img3'), 'Casper.visible() can detect if an element is invisible');
this.waitWhileVisible('#img1', function() {
this.test.comment('Casper.waitWhileVisible()');
this.test.pass('Casper.waitWhileVisible() can wait while an element is visible');
}, function() {
this.test.comment('Casper.waitWhileVisible()');
this.test.fail('Casper.waitWhileVisible() can wait while an element is visible');
}, 2000);
});
casper.test.begin('visibility tests', 4, function(test) {
casper.start('tests/site/visible.html', function() {
test.assert(this.visible('#img1'), 'Casper.visible() can detect if an element is visible');
test.assert(!this.visible('#img2'), 'Casper.visible() can detect if an element is invisible');
test.assert(!this.visible('#img3'), 'Casper.visible() can detect if an element is invisible');
this.waitWhileVisible('#img1', function() {
test.pass('Casper.waitWhileVisible() can wait while an element is visible');
}, function() {
test.fail('Casper.waitWhileVisible() can wait while an element is visible');
}, 2000);
});
casper.run(function() {
this.test.done(4);
casper.run(function() {
test.done();
});
});
......
/*global casper*/
/*jshint strict:false*/
var waitStart;
casper.test.begin('wait*() tests', 3, function(test) {
var waitStart;
casper.start('tests/site/index.html', function() {
waitStart = new Date().getTime();
});
casper.start('tests/site/index.html', function() {
waitStart = new Date().getTime();
});
casper.wait(1000, function() {
this.test.comment('Casper.wait()');
this.test.assert(new Date().getTime() - waitStart > 1000, 'Casper.wait() can wait for a given amount of time');
});
casper.wait(250, function() {
test.assert(new Date().getTime() - waitStart > 250,
'Casper.wait() can wait for a given amount of time');
});
casper.thenOpen('tests/site/waitFor.html', function() {
this.test.comment('Casper.waitFor()');
this.waitFor(function() {
return this.evaluate(function() {
return document.querySelectorAll('li').length === 4;
casper.thenOpen('tests/site/waitFor.html', function() {
this.waitFor(function() {
return this.evaluate(function() {
return document.querySelectorAll('li').length === 4;
});
}, function() {
test.pass('Casper.waitFor() can wait for something to happen');
}, function() {
test.fail('Casper.waitFor() can wait for something to happen');
});
}, function() {
this.test.pass('Casper.waitFor() can wait for something to happen');
}, function() {
this.test.fail('Casper.waitFor() can wait for something to happen');
});
});
casper.thenOpen('tests/site/waitFor.html').waitForText('<li>four</li>', function() {
this.test.comment('Casper.waitForText()');
this.test.pass('Casper.waitForText() can wait for text');
}, function() {
this.test.comment('Casper.waitForText()');
this.test.fail('Casper.waitForText() can wait for text');
});
casper.thenOpen('tests/site/waitFor.html').waitForText('<li>four</li>', function() {
test.pass('Casper.waitForText() can wait for text');
}, function() {
test.fail('Casper.waitForText() can wait for text');
});
casper.run(function() {
this.test.done(3);
casper.run(function() {
test.done();
});
});
......