Commit 90f33738 90f33738f968769d6abc4c1f37779d1c2db91086 by Nicolas Perriault

removed obsolete isolation tricks from test suite

1 parent 9dd655b4
.DS_Store
*.xml
*.png
/tmp
......
(function(t) {
var fs = require('fs'), testFile = '/tmp/__casper_test_capture.png';
var fs = require('fs'), testFile = '/tmp/__casper_test_capture.png';
if (fs.exists(testFile) && fs.isFile(testFile)) {
if (fs.exists(testFile) && fs.isFile(testFile)) {
fs.remove(testFile);
}
}
casper.start('tests/site/index.html', function(self) {
casper.start('tests/site/index.html', function(self) {
self.viewport(300, 200);
t.comment('Casper.capture()');
this.test.comment('Casper.capture()');
self.capture(testFile);
t.assert(fs.isFile(testFile), 'Casper.capture() captured a screenshot');
});
this.test.assert(fs.isFile(testFile), 'Casper.capture() captured a screenshot');
});
try {
try {
fs.remove(testFile);
} catch(e) {}
} catch(e) {}
casper.run(function(self) {
t.done();
});
})(casper.test);
casper.run(function(self) {
this.test.done();
});
......
(function(t) {
casper.start('tests/site/index.html', function() {
casper.start('tests/site/index.html', function() {
this.click('a[href="test.html"]');
});
});
casper.then(function() {
t.comment('Casper.click()');
t.assertTitle('CasperJS test target', 'Casper.click() can click on a link');
}).thenClick('a', function() {
t.comment('Casper.thenClick()');
t.assertTitle('CasperJS test form', 'Casper.thenClick() can click on a link');
});
casper.then(function() {
this.test.comment('Casper.click()');
this.test.assertTitle('CasperJS test target', 'Casper.click() can click on a link');
}).thenClick('a', function() {
this.test.comment('Casper.thenClick()');
this.test.assertTitle('CasperJS test form', 'Casper.thenClick() can click on a link');
});
// onclick variants tests
casper.thenOpen('tests/site/click.html', function() {
t.comment('CasperUtils.click()');
// onclick variants tests
casper.thenOpen('tests/site/click.html', function() {
this.test.comment('CasperUtils.click()');
this.test.assert(this.click('#test1'), 'CasperUtils.click() can click an `href="javascript:` link');
this.test.assert(this.click('#test2'), 'CasperUtils.click() can click an `href="#"` link');
this.test.assert(this.click('#test3'), 'CasperUtils.click() can click an `onclick=".*; return false"` link');
......@@ -23,27 +22,26 @@
this.test.assert(results.test2, 'CasperUtils.click() has clicked an `href="#"` link');
this.test.assert(results.test3, 'CasperUtils.click() has clicked an `onclick=".*; return false"` link');
this.test.assert(results.test4, 'CasperUtils.click() has clicked an unobstrusive js handled link');
});
});
// casper.mouse
casper.then(function() {
t.comment('Mouse.down()');
// casper.mouse
casper.then(function() {
this.test.comment('Mouse.down()');
this.mouse.down(200, 100);
var results = this.getGlobal('results');
this.test.assertEquals(results.testdown, [200, 100], 'Mouse.down() has pressed button to the specified position');
t.comment('Mouse.up()');
this.test.comment('Mouse.up()');
this.mouse.up(200, 100);
results = this.getGlobal('results');
this.test.assertEquals(results.testup, [200, 100], 'Mouse.up() has released button to the specified position');
t.comment('Mouse.move()');
this.test.comment('Mouse.move()');
this.mouse.move(200, 100);
results = this.getGlobal('results');
this.test.assertEquals(results.testmove, [200, 100], 'Mouse.move() has moved to the specified position');
});
});
casper.run(function() {
t.done();
});
})(casper.test);
\ No newline at end of file
casper.run(function() {
this.test.done();
});
......
(function(t) {
t.comment('ClientUtils.encode()');
var fs = require('fs');
var clientutils = require('clientutils').create();
var testCases = {
var fs = require('fs');
var clientutils = require('clientutils').create();
var testCases = {
'an empty string': '',
'a word': 'plop',
'a null char': 'a\u0000',
......@@ -13,13 +10,14 @@
"Et dans ma tête qui bourdonnent?\n" +
"Les abeilles!"),
'a file contents': fs.read(phantom.casperPath + '/tests/site/alert.html')
};
};
casper.test.comment('ClientUtils.encode()');
for (var what in testCases) {
for (var what in testCases) {
var source = testCases[what];
var encoded = clientutils.encode(source);
t.assertEquals(clientutils.decode(encoded), source, 'ClientUtils can encode and decode ' + what);
}
casper.test.assertEquals(clientutils.decode(encoded), source, 'ClientUtils can encode and decode ' + what);
}
t.done();
})(casper.test);
casper.test.done();
......
(function(t) {
casper.start('tests/site/index.html', function(self) {
var fs = require('fs');
casper.start('tests/site/index.html', function() {
var imageUrl = 'file://' + phantom.casperPath + '/tests/site/images/phantom.png';
var image = self.base64encode(imageUrl);
var fs = require('fs');
var image = this.base64encode(imageUrl);
t.comment('Casper.base64encode()');
t.assertEquals(image.length, 6160, 'Casper.base64encode() can retrieve base64 contents');
this.test.comment('Casper.base64encode()');
this.test.assertEquals(image.length, 6160, 'Casper.base64encode() can retrieve base64 contents');
t.comment('Casper.download()');
self.download(imageUrl, 'logo.png');
t.assert(fs.exists('logo.png'), 'Casper.download() downloads a file');
this.test.comment('Casper.download()');
this.download(imageUrl, 'logo.png');
this.test.assert(fs.exists('logo.png'), 'Casper.download() downloads a file');
if (fs.exists('logo.png')) {
fs.remove('logo.png');
}
});
});
casper.run(function(self) {
t.done();
});
})(casper.test);
casper.run(function() {
this.test.done();
});
......
(function(t) {
t.comment('Casper.evaluate()');
casper.test.comment('Casper.evaluate()');
casper.start();
casper.start();
var params = {
var params = {
"boolean true": true,
"boolean false": false,
"int number": 42,
......@@ -11,19 +10,18 @@
"string": "plop! \"Ÿ£$\" 'no'",
"array": [1, 2, 3],
"object": {a: 1, b: 2}
};
};
var casperParams = casper.evaluate(function() {
var casperParams = casper.evaluate(function() {
return __casper_params__;
}, params);
}, params);
casper.test.assertType(casperParams, "object", 'Casper.evaluate() exposes parameters in a dedicated object');
casper.test.assertEquals(Object.keys(casperParams).length, 7, 'Casper.evaluate() object containing parameters has the correct length');
casper.test.assertType(casperParams, "object", 'Casper.evaluate() exposes parameters in a dedicated object');
casper.test.assertEquals(Object.keys(casperParams).length, 7, 'Casper.evaluate() object containing parameters has the correct length');
for (var param in casperParams) {
for (var param in casperParams) {
casper.test.assertEquals(JSON.stringify(casperParams[param]), JSON.stringify(params[param]), 'Casper.evaluate() can pass a ' + param);
casper.test.assertEquals(typeof casperParams[param], typeof params[param], 'Casper.evaluate() preserves the ' + param + ' type');
}
}
t.done();
})(casper.test);
casper.test.done();
......
(function(t) {
t.comment('Casper.exists()');
casper.test.comment('Casper.exists()');
casper.start('tests/site/index.html', function(self) {
t.assert(self.exists('a') && !self.exists('chucknorriz'), 'Casper.exists() can check if an element 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(step) {
t.done();
});
})(casper.test);
casper.run(function() {
this.test.done();
});
......
(function(t) {
t.comment('Casper.fetchText()');
casper.test.comment('Casper.fetchText()');
casper.start('tests/site/index.html', function(self) {
t.assertEquals(self.fetchText('ul li'), 'onetwothree', 'Casper.fetchText() can retrieve text contents');
});
casper.start('tests/site/index.html', function() {
this.test.assertEquals(this.fetchText('ul li'), 'onetwothree', 'Casper.fetchText() can retrieve text contents');
});
casper.run(function() {
t.done();
});
})(casper.test);
casper.run(function() {
this.test.done();
});
......
do(casper) ->
step = 0
step = 0
# testing resources
casper.start "tests/site/resources.html", ->
# testing resources
casper.start "tests/site/resources.html", ->
@test.assertEquals ++step, 1, "step 1"
@wait 400, ->
@test.assertEquals ++step, 2, "step 1.1"
......@@ -15,16 +14,16 @@ do(casper) ->
@wait 400, ->
@test.assertEquals ++step, 6, "step 1.2"
casper.wait 200, ->
casper.wait 200, ->
@test.assertEquals ++step, 7, "step 2"
casper.waitForSelector(
casper.waitForSelector(
'#noneExistingSelector'
-> @test.fail "should run into timeout"
-> @test.assertEquals ++step, 8, "step 3 sucessfully timed out"
1000
)
casper.then ->
)
casper.then ->
@test.assertEquals ++step, 9, "step 4"
@wait 300, ->
@test.assertEquals ++step, 10, "step 4.1"
......@@ -33,7 +32,7 @@ do(casper) ->
@wait 100, ->
@test.assertEquals ++step, 12, "step 5.2"
casper.then ->
casper.then ->
@test.assertEquals ++step, 13, "last step"
casper.run(-> @test.done())
\ No newline at end of file
casper.run(-> @test.done())
\ No newline at end of file
......
(function(t) {
casper.start('tests/site/form.html', function(self) {
t.comment('Casper.fill()');
self.fill('form[action="result.html"]', {
casper.start('tests/site/form.html', function() {
this.test.comment('Casper.fill()');
this.fill('form[action="result.html"]', {
email: 'chuck@norris.com',
content: 'Am watching thou',
check: true,
......@@ -10,45 +9,44 @@
file: phantom.libraryPath + '/README.md',
'checklist[]': ['1', '3']
});
t.assertEvalEquals(function() {
this.test.assertEvalEquals(function() {
return document.querySelector('input[name="email"]').value;
}, 'chuck@norris.com', 'Casper.fill() can fill an input[type=text] form field');
t.assertEvalEquals(function() {
this.test.assertEvalEquals(function() {
return document.querySelector('textarea[name="content"]').value;
}, 'Am watching thou', 'Casper.fill() can fill a textarea form field');
t.assertEvalEquals(function() {
this.test.assertEvalEquals(function() {
return document.querySelector('select[name="topic"]').value;
}, 'bar', 'Casper.fill() can pick a value from a select form field');
t.assertEvalEquals(function() {
this.test.assertEvalEquals(function() {
return document.querySelector('input[name="check"]').checked;
}, true, 'Casper.fill() can check a form checkbox');
t.assertEvalEquals(function() {
this.test.assertEvalEquals(function() {
return document.querySelector('input[name="choice"][value="no"]').checked;
}, true, 'Casper.fill() can check a form radio button 1/2');
t.assertEvalEquals(function() {
this.test.assertEvalEquals(function() {
return document.querySelector('input[name="choice"][value="yes"]').checked;
}, false, 'Casper.fill() can check a form radio button 2/2');
t.assertEvalEquals(function() {
this.test.assertEvalEquals(function() {
return document.querySelector('input[name="file"]').files.length === 1;
}, true, 'Casper.fill() can select a file to upload');
t.assertEvalEquals(function() {
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');
self.click('input[type="submit"]');
});
this.click('input[type="submit"]');
});
casper.then(function(self) {
t.comment('Form submitted');
t.assertUrlMatch(/email=chuck@norris.com/, 'Casper.fill() input[type=email] field was submitted');
t.assertUrlMatch(/content=Am\+watching\+thou/, 'Casper.fill() textarea field was submitted');
t.assertUrlMatch(/check=on/, 'Casper.fill() input[type=checkbox] field was submitted');
t.assertUrlMatch(/choice=no/, 'Casper.fill() input[type=radio] field was submitted');
t.assertUrlMatch(/topic=bar/, 'Casper.fill() select field was submitted');
});
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(/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.run(function(self) {
t.done();
});
})(casper.test);
casper.run(function() {
this.test.done();
});
......
(function(t) {
casper.start('tests/site/global.html', function(self) {
t.comment('Casper.getGlobal()');
t.assertEquals(self.getGlobal('myGlobal'), 'awesome string', 'Casper.getGlobal() can retrieve a remote global variable');
t.assertRaises(self.getGlobal, ['myUnencodableGlobal'], 'Casper.getGlobal() does not fail trying to encode an unencodable global');
});
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.assertRaises(this.getGlobal, ['myUnencodableGlobal'], 'Casper.getGlobal() does not fail trying to encode an unencodable global');
});
casper.run(function(self) {
t.done();
});
})(casper.test);
casper.run(function() {
this.test.done();
});
......
(function(t) {
casper.start('tests/site/page1.html');
casper.thenOpen('tests/site/page2.html');
casper.thenOpen('tests/site/page3.html');
casper.start('tests/site/page1.html');
casper.thenOpen('tests/site/page2.html');
casper.thenOpen('tests/site/page3.html');
casper.back();
casper.then(function(self) {
t.comment('navigating history backward');
t.assertMatch(self.getCurrentUrl(), /tests\/site\/page2\.html$/, 'Casper.back() can go back an history step');
});
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(self) {
t.comment('navigating history forward');
t.assertMatch(self.getCurrentUrl(), /tests\/site\/page3\.html$/, 'Casper.forward() can go forward 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(self) {
t.assert(self.history.length > 0, 'Casper.history contains urls');
t.assertMatch(self.history[0], /tests\/site\/page1\.html$/, 'Casper.history has the correct first url');
t.done();
});
})(casper.test);
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();
});
......
(function(t) {
// Casper.options.onStepComplete
casper.start('tests/site/index.html', function(self) {
self.options.onStepComplete = function(self, stepResult) {
t.comment('Casper.options.onStepComplete()');
t.assertEquals(stepResult, 'ok', 'Casper.options.onStepComplete() is called on step complete');
// 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;
};
return 'ok';
});
});
// Casper.options.onResourceRequested & Casper.options.onResourceReceived
casper.then(function(self) {
self.options.onResourceReceived = function(self, resource) {
t.comment('Casper.options.onResourceReceived()');
t.assertType(resource, 'object', 'Casper.options.onResourceReceived() retrieve a resource object');
t.assert('status' in resource, 'Casper.options.onResourceReceived() retrieve a valid resource object');
// 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;
};
self.options.onResourceRequested = function(self, request) {
t.comment('Casper.options.onResourceRequested()');
t.assertType(request, 'object', 'Casper.options.onResourceRequested() retrieve a request object');
t.assert('method' in request, 'Casper.options.onResourceRequested() retrieve a valid request object');
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;
};
self.thenOpen('tests/site/page1.html');
});
this.thenOpen('tests/site/page1.html');
});
// Casper.options.onAlert()
casper.then(function(self) {
self.options.onAlert = function(self, message) {
t.assertEquals(message, 'plop', 'Casper.options.onAlert() can intercept an alert message');
// 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');
};
}).thenOpen('tests/site/alert.html').thenClick('button', function(self) {
self.options.onAlert = null;
});
}).thenOpen('tests/site/alert.html').thenClick('button', function() {
this.options.onAlert = null;
});
casper.run(function(self) {
t.done();
});
})(casper.test);
casper.run(function() {
this.test.done();
});
......
(function(t) {
casper.start('tests/site/index.html');
casper.start('tests/site/index.html');
var oldLevel = casper.options.logLevel;
var oldLevel = casper.options.logLevel;
casper.options.logLevel = 'info';
casper.options.verbose = false;
casper.options.logLevel = 'info';
casper.options.verbose = false;
t.comment('Casper.log()');
casper.log('foo', 'info');
t.assert(casper.result.log.some(function(e) {
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.log() adds a log entry');
casper.options.logLevel = oldLevel;
casper.options.verbose = true;
casper.options.logLevel = oldLevel;
casper.options.verbose = true;
casper.then(function(self) {
casper.then(function() {
var oldLevel = casper.options.logLevel;
casper.options.logLevel = 'debug';
casper.options.verbose = false;
......@@ -23,18 +22,17 @@
__utils__.log('debug message');
__utils__.log('info message', 'info');
});
t.assert(casper.result.log.some(function(e) {
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');
t.assert(casper.result.log.some(function(e) {
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(self) {
t.assertEquals(self.result.log.length, 3, 'Casper.log() logged messages');
t.done();
});
})(casper.test);
casper.run(function() {
this.test.assertEquals(this.result.log.length, 3, 'Casper.log() logged messages');
this.test.done();
});
......
(function(t) {
var current = 0, tests = [
var t = casper.test, current = 0, tests = [
function(settings) {
t.assertEquals(settings, {
method: "get"
......@@ -24,52 +23,51 @@
password: 'sinclar'
}, "Casper.open() used the expected HTTP auth settings");
}
];
];
casper.start();
casper.start();
casper.on('open', function(url, settings) {
casper.on('open', function(url, settings) {
tests[current++](settings);
});
});
// GET
casper.open('tests/site/index.html').then(function() {
// 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', {
// POST
casper.open('tests/site/index.html', {
method: 'post',
data: {
plop: 42,
chuck: 'norris'
}
}).then(function() {
}).then(function() {
t.pass("Casper.open() can open and load a location using POST");
});
});
// PUT
casper.open('tests/site/index.html', {
// PUT
casper.open('tests/site/index.html', {
method: 'put',
data: {
plop: 42,
chuck: 'norris'
}
}).then(function() {
}).then(function() {
t.pass("Casper.open() can open and load a location using PUT");
});
});
// HTTP Auth
casper.open('tests/site/index.html', {
// HTTP Auth
casper.open('tests/site/index.html', {
method: 'get',
username: 'bob',
password: 'sinclar'
}).then(function() {
}).then(function() {
t.pass("Casper.open() can open and load a location using HTTP auth");
});
});
casper.run(function() {
casper.run(function() {
this.removeAllListeners('open');
t.done();
});
})(casper.test);
});
......
do(casper) ->
casper.start "tests/site/resources.html", ->
casper.start "tests/site/resources.html", ->
@test.assertEquals @resources.length, 1, "only one resource found"
onTime = ->
@test.assertEquals(
......@@ -22,4 +21,4 @@ do(casper) ->
onTimeout = -> @test.fail "waitForResource timeout occured"
@waitForResource "phantom.png", onTime, onTimeout
casper.run(-> @test.done())
casper.run(-> @test.done())
......
(function(t) {
t.comment('Casper.start()');
casper.test.comment('Casper.start()');
casper.start('tests/site/index.html', function(self) {
t.pass('Casper.start() can chain a next step');
t.assertTitle('CasperJS test index', 'Casper.start() opened the passed url');
t.assertEval(function() {
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');
});
});
t.assert(casper.started, 'Casper.start() started');
casper.test.assert(casper.started, 'Casper.start() started');
casper.run(function(self) {
t.done();
});
})(casper.test);
casper.run(function() {
this.test.done();
});
......
(function(t) {
t.comment('Casper.then()');
casper.test.comment('Casper.then()');
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(self) {
t.assertTitle('CasperJS test index', 'Casper.then() added a new step');
});
casper.then(function(self) {
this.test.assertTitle('CasperJS test index', 'Casper.then() added a new step');
});
t.assertEquals(casper.steps.length, nsteps + 1, 'Casper.then() can add a new step');
casper.test.assertEquals(casper.steps.length, nsteps + 1, 'Casper.then() can add a new step');
t.comment('Casper.thenOpen()');
casper.test.comment('Casper.thenOpen()');
casper.thenOpen('tests/site/test.html');
casper.thenOpen('tests/site/test.html');
t.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(self) {
t.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');
});
t.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');
t.comment('Casper.each()');
casper.each([1, 2, 3], function(self, item, i) {
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.run(function() {
t.done();
});
})(casper.test);
casper.run(function() {
this.test.done();
});
......
(function(t) {
t.comment('Casper.viewport()');
casper.test.comment('Casper.viewport()');
casper.start();
casper.start();
casper.viewport(1337, 999);
casper.viewport(1337, 999);
t.assertEquals(casper.page.viewportSize.width, 1337, 'Casper.viewport() can change the width of page viewport');
t.assertEquals(casper.page.viewportSize.height, 999, 'Casper.viewport() can change the height of page viewport');
t.assertRaises(casper.viewport, ['a', 'b'], 'Casper.viewport() validates viewport size data');
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');
t.done();
})(casper.test);
casper.test.done();
......
(function(t) {
casper.start('tests/site/visible.html', function(self) {
self.test.comment('Casper.visible()');
self.test.assert(self.visible('#img1'), 'Casper.visible() can detect if an element is visible');
self.test.assert(!self.visible('#img2'), 'Casper.visible() can detect if an element is invisible');
self.test.assert(!self.visible('#img3'), 'Casper.visible() can detect if an element is invisible');
self.waitWhileVisible('#img1', function(self) {
self.test.comment('Casper.waitWhileVisible()');
self.test.pass('Casper.waitWhileVisible() can wait while an element is visible');
}, function(self) {
self.test.comment('Casper.waitWhileVisible()');
self.test.fail('Casper.waitWhileVisible() can wait while an element is visible');
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.run(function(self) {
t.done();
});
})(casper.test);
casper.run(function() {
this.test.done();
});
......
(function(t) {
var waitStart;
var waitStart;
casper.start('tests/site/index.html', function(self) {
casper.start('tests/site/index.html', function() {
waitStart = new Date().getTime();
});
});
casper.wait(1000, function(self) {
self.test.comment('Casper.wait()');
self.test.assert(new Date().getTime() - waitStart > 1000, 'Casper.wait() can wait for a given amount of time');
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.waitFor()
casper.thenOpen('tests/site/waitFor.html', function(self) {
casper.test.comment('Casper.waitFor()');
self.waitFor(function(self) {
return self.evaluate(function() {
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;
});
}, function(self) {
self.test.pass('Casper.waitFor() can wait for something to happen');
}, function(self) {
self.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.run(function(self) {
t.done();
});
})(casper.test);
casper.run(function() {
this.test.done();
});
......
(function(t) {
var cli = require('cli');
var cli = require('cli'), t = casper.test;
t.comment('parse(), get(), has()');
t.comment('parse(), get(), has()');
(function(parsed) {
(function(parsed) {
t.assertEquals(parsed.args, [], 'parse() returns expected positional args array');
t.assertEquals(parsed.options, {}, 'parse() returns expected options object');
t.assertEquals(parsed.get(0), undefined, 'parse() does not return inexistant positional arg');
t.assertEquals(parsed.get('blah'), undefined, 'parse() does not return inexistant option');
t.assert(!parsed.has(0), 'has() checks if an arg is set');
t.assert(!parsed.has('blah'), 'has() checks if an option is set');
})(cli.parse([]));
})(cli.parse([]));
(function(parsed) {
(function(parsed) {
t.assertEquals(parsed.args, ['foo', 'bar'], 'parse() returns expected positional args array');
t.assertEquals(parsed.options, {}, 'parse() returns expected options object');
t.assertEquals(parsed.get(0), 'foo', 'parse() retrieve first positional arg');
......@@ -20,25 +19,25 @@
t.assert(parsed.has(0), 'has() checks if an arg is set');
t.assert(parsed.has(1), 'has() checks if an arg is set');
t.assert(!parsed.has(2), 'has() checks if an arg is not set');
})(cli.parse(['foo', 'bar']));
})(cli.parse(['foo', 'bar']));
(function(parsed) {
(function(parsed) {
t.assertEquals(parsed.args, [], 'parse() returns expected positional args array');
t.assertEquals(parsed.options, {foo: 'bar', baz: true}, 'parse() returns expected options object');
t.assertEquals(parsed.get('foo'), 'bar', 'parse() retrieve an option value');
t.assert(parsed.get('baz'), 'parse() retrieve boolean option flag');
t.assert(parsed.has("foo"), 'has() checks if an option is set');
t.assert(parsed.has("baz"), 'has() checks if an option is set');
})(cli.parse(['--foo=bar', '--baz']));
})(cli.parse(['--foo=bar', '--baz']));
(function(parsed) {
(function(parsed) {
t.assertEquals(parsed.args, [], 'parse() returns expected positional args array');
t.assertEquals(parsed.options, { '&é"à': 42 }, 'parse() returns expected options object');
t.assertEquals(parsed.get('&é"à'), 42, 'parse() handles options with exotic names');
t.assert(parsed.has('&é"à'), 'has() checks if an option is set');
})(cli.parse(['--&é"à=42']));
})(cli.parse(['--&é"à=42']));
(function(parsed) {
(function(parsed) {
t.assertEquals(parsed.args, ['foo & bar', 'baz & boz'], 'parse() returns expected positional args array');
t.assertEquals(parsed.options, { universe: 42, lap: 13.37, chucknorris: true, oops: false }, 'parse() returns expected options object');
t.assertEquals(parsed.get('universe'), 42, 'parse() can cast a numeric option value');
......@@ -66,7 +65,6 @@
chucknorris: true,
oops: false
}, 'drop() did not affect other options');
})(cli.parse(['foo & bar', 'baz & boz', '--universe=42', '--lap=13.37', '--chucknorris', '--oops=false']));
})(cli.parse(['foo & bar', 'baz & boz', '--universe=42', '--lap=13.37', '--chucknorris', '--oops=false']));
t.done();
})(casper.test);
t.done();
......
"A small subset of the run.js written in coffeescript"
do (casper) ->
steps = 0
steps = 0
casper.options.onStepComplete = -> steps++
casper.options.onStepComplete = -> steps++
casper.start "tests/site/index.html", ->
casper.start "tests/site/index.html", ->
@test.assertTitle "CasperJS test index", "Casper.start() casper can start itself an open an url"
@test.assertEquals @fetchText("ul li"), "onetwothree", "Casper.fetchText() can retrieves text contents"
@click "a[href=\"test.html\"]"
casper.then ->
casper.then ->
@test.assertTitle "CasperJS test target", "Casper.click() casper can click on a text link"
@click "a[href=\"form.html\"]"
casper.run ->
casper.run ->
@test.assertEquals steps, 3, "Casper.options.onStepComplete() is called on step complete"
@options.onStepComplete = null
@test.done()
......
(function(t) {
var error;
var error;
function foo() {
function foo() {
bar();
}
}
function bar() {
function bar() {
throw new CasperError('bar');
}
}
try {
try {
foo();
} catch (e) {
} catch (e) {
error = e;
}
}
t.assertType(error.stack, "string", "CasperError() has a stack string property set");
t.assertMatch(error.stack, /^CasperError: bar\s/, "CasperError() has the expected stack value");
casper.test.assertType(error.stack, "string", "CasperError() has a stack string property set");
casper.test.assertMatch(error.stack, /^CasperError: bar\s/, "CasperError() has the expected stack value");
t.done();
})(casper.test);
\ No newline at end of file
casper.test.done();
......
(function(t) {
function createInjector(fn, values) {
var t = casper.test;
var createInjector = function(fn, values) {
return require('injector').create(fn, values);
}
};
var testFn = function(a, b) { return a + b; };
var injector = createInjector(testFn);
var extract = injector.extract(testFn);
var testFn = function(a, b) { return a + b; };
var injector = createInjector(testFn);
var extract = injector.extract(testFn);
t.comment('FunctionArgsInjector.extract()');
t.assertType(extract, "object", 'FunctionArgsInjector.extract() returns an object');
t.assertEquals(extract.name, null, 'FunctionArgsInjector.extract() process function name as expected');
t.assertEquals(extract.body, 'return a + b;', 'FunctionArgsInjector.extract() process function body as expected');
t.assertEquals(extract.args, ['a', 'b'], 'FunctionArgsInjector.extract() process function args as expected');
t.comment('FunctionArgsInjector.extract()');
t.assertType(extract, "object", 'FunctionArgsInjector.extract() returns an object');
t.assertEquals(extract.name, null, 'FunctionArgsInjector.extract() process function name as expected');
t.assertEquals(extract.body, 'return a + b;', 'FunctionArgsInjector.extract() process function body as expected');
t.assertEquals(extract.args, ['a', 'b'], 'FunctionArgsInjector.extract() process function args as expected');
function Plop(foo, bar) {
function Plop(foo, bar) {
return 'foo: ' + foo +', bar: ' + bar;
}
function Plip() { return 'plop'; }
function foo_bar(boz) {}
var gni = function ($bubu_bibi, __popo__) {};
var gno = function ( arg1, /*plop*/ arg2 ) { };
t.assertEquals(injector.extract(Plop), {
}
function Plip() { return 'plop'; }
function foo_bar(boz) {}
var gni = function ($bubu_bibi, __popo__) {};
var gno = function ( arg1, /*plop*/ arg2 ) { };
t.assertEquals(injector.extract(Plop), {
name: 'Plop',
args: ['foo', 'bar'],
body: "return 'foo: ' + foo +', bar: ' + bar;"
}, 'FunctionArgsInjector.extract() handles named functions with arguments and body');
t.assertEquals(injector.extract(Plip), {
}, 'FunctionArgsInjector.extract() handles named functions with arguments and body');
t.assertEquals(injector.extract(Plip), {
name: 'Plip',
args: [],
body: "return 'plop';"
}, 'FunctionArgsInjector.extract() handles functions with no arguments');
t.assertEquals(injector.extract(foo_bar), {
}, 'FunctionArgsInjector.extract() handles functions with no arguments');
t.assertEquals(injector.extract(foo_bar), {
name: 'foo_bar',
args: ['boz'],
body: ""
}, 'FunctionArgsInjector.extract() handles functions with no body');
t.assertEquals(injector.extract(gni), {
}, 'FunctionArgsInjector.extract() handles functions with no body');
t.assertEquals(injector.extract(gni), {
name: null,
args: ['$bubu_bibi', '__popo__'],
body: ""
}, 'FunctionArgsInjector.extract() handles anonymous functions with complex args passed');
t.assertEquals(injector.extract(gno), {
}, 'FunctionArgsInjector.extract() handles anonymous functions with complex args passed');
t.assertEquals(injector.extract(gno), {
name: null,
args: ['arg1', 'arg2'],
body: ""
}, 'FunctionArgsInjector.extract() handles can filter comments in function args');
}, 'FunctionArgsInjector.extract() handles can filter comments in function args');
t.comment('FunctionArgsInjector.process()');
var processed;
eval('processed = ' + injector.process({ a: 1, b: 2 }));
t.comment('FunctionArgsInjector.process()');
var processed;
eval('processed = ' + injector.process({ a: 1, b: 2 }));
t.assertType(processed, "function", 'FunctionArgsInjector.process() processed a function');
t.assertEquals(processed(), 3, 'FunctionArgsInjector.process() processed the function correctly');
t.assertType(processed, "function", 'FunctionArgsInjector.process() processed a function');
t.assertEquals(processed(), 3, 'FunctionArgsInjector.process() processed the function correctly');
t.done();
})(casper.test);
t.done();
......
(function(t) {
t.comment('Tester.testEquals()');
t.assert(casper.test.testEquals(null, null), 'Tester.testEquals() null equality');
t.assertNot(casper.test.testEquals(null, undefined), 'Tester.testEquals() null vs. undefined inequality');
t.assert(casper.test.testEquals("hi", "hi"), 'Tester.testEquals() string equality');
t.assertNot(casper.test.testEquals("hi", "ih"), 'Tester.testEquals() string inequality');
t.assert(casper.test.testEquals(5, 5), 'Tester.testEquals() number equality');
t.assertNot(casper.test.testEquals("5", 5), 'Tester.testEquals() number equality without implicit cast');
t.assert(casper.test.testEquals(5, 5.0), 'Tester.testEquals() number equality with cast');
t.assertNot(casper.test.testEquals(5, 10), 'Tester.testEquals() number inequality');
t.assert(casper.test.testEquals([], []), 'Tester.testEquals() empty array equality');
t.assert(casper.test.testEquals([1,2], [1,2]), 'Tester.testEquals() array equality');
t.assert(casper.test.testEquals([1,2,[1,2,function(){}]], [1,2,[1,2,function(){}]]), 'Tester.testEquals() complex array equality');
t.assertNot(casper.test.testEquals([1,2,[1,2,function(a){}]], [1,2,[1,2,function(b){}]]), 'Tester.testEquals() complex array inequality');
t.assertNot(casper.test.testEquals([1,2], [2,1]), 'Tester.testEquals() shuffled array inequality');
t.assertNot(casper.test.testEquals([1,2], [1,2,3]), 'Tester.testEquals() array length inequality');
t.assert(casper.test.testEquals({}, {}), 'Tester.testEquals() empty object equality');
t.assert(casper.test.testEquals({a:1,b:2}, {a:1,b:2}), 'Tester.testEquals() object length equality');
t.assert(casper.test.testEquals({a:1,b:2}, {b:2,a:1}), 'Tester.testEquals() shuffled object keys equality');
t.assertNot(casper.test.testEquals({a:1,b:2}, {a:1,b:3}), 'Tester.testEquals() object inequality');
t.assert(casper.test.testEquals({1:{name:"bob",age:28}, 2:{name:"john",age:26}}, {1:{name:"bob",age:28}, 2:{name:"john",age:26}}), 'Tester.testEquals() complex object equality');
t.assertNot(casper.test.testEquals({1:{name:"bob",age:28}, 2:{name:"john",age:26}}, {1:{name:"bob",age:28}, 2:{name:"john",age:27}}), 'Tester.testEquals() complex object inequality');
t.assert(casper.test.testEquals(function(x){return x;}, function(x){return x;}), 'Tester.testEquals() function equality');
t.assertNot(casper.test.testEquals(function(x){return x;}, function(y){return y+2;}), 'Tester.testEquals() function inequality');
var t = casper.test;
t.done();
})(casper.test);
t.comment('Tester.testEquals()');
t.assert(casper.test.testEquals(null, null), 'Tester.testEquals() null equality');
t.assertNot(casper.test.testEquals(null, undefined), 'Tester.testEquals() null vs. undefined inequality');
t.assert(casper.test.testEquals("hi", "hi"), 'Tester.testEquals() string equality');
t.assertNot(casper.test.testEquals("hi", "ih"), 'Tester.testEquals() string inequality');
t.assert(casper.test.testEquals(5, 5), 'Tester.testEquals() number equality');
t.assertNot(casper.test.testEquals("5", 5), 'Tester.testEquals() number equality without implicit cast');
t.assert(casper.test.testEquals(5, 5.0), 'Tester.testEquals() number equality with cast');
t.assertNot(casper.test.testEquals(5, 10), 'Tester.testEquals() number inequality');
t.assert(casper.test.testEquals([], []), 'Tester.testEquals() empty array equality');
t.assert(casper.test.testEquals([1,2], [1,2]), 'Tester.testEquals() array equality');
t.assert(casper.test.testEquals([1,2,[1,2,function(){}]], [1,2,[1,2,function(){}]]), 'Tester.testEquals() complex array equality');
t.assertNot(casper.test.testEquals([1,2,[1,2,function(a){}]], [1,2,[1,2,function(b){}]]), 'Tester.testEquals() complex array inequality');
t.assertNot(casper.test.testEquals([1,2], [2,1]), 'Tester.testEquals() shuffled array inequality');
t.assertNot(casper.test.testEquals([1,2], [1,2,3]), 'Tester.testEquals() array length inequality');
t.assert(casper.test.testEquals({}, {}), 'Tester.testEquals() empty object equality');
t.assert(casper.test.testEquals({a:1,b:2}, {a:1,b:2}), 'Tester.testEquals() object length equality');
t.assert(casper.test.testEquals({a:1,b:2}, {b:2,a:1}), 'Tester.testEquals() shuffled object keys equality');
t.assertNot(casper.test.testEquals({a:1,b:2}, {a:1,b:3}), 'Tester.testEquals() object inequality');
t.assert(casper.test.testEquals({1:{name:"bob",age:28}, 2:{name:"john",age:26}}, {1:{name:"bob",age:28}, 2:{name:"john",age:26}}), 'Tester.testEquals() complex object equality');
t.assertNot(casper.test.testEquals({1:{name:"bob",age:28}, 2:{name:"john",age:26}}, {1:{name:"bob",age:28}, 2:{name:"john",age:27}}), 'Tester.testEquals() complex object inequality');
t.assert(casper.test.testEquals(function(x){return x;}, function(x){return x;}), 'Tester.testEquals() function equality');
t.assertNot(casper.test.testEquals(function(x){return x;}, function(y){return y+2;}), 'Tester.testEquals() function inequality');
t.done();
......
(function(t) {
var utils = require('utils');
var utils = require('utils'), t = casper.test;
t.comment('fileExt()');
(function() {
t.comment('fileExt()');
(function() {
var testCases = {
'foo.ext': 'ext',
'FOO.EXT': 'ext',
......@@ -15,10 +14,10 @@
for (var testCase in testCases) {
t.assertEquals(utils.fileExt(testCase), testCases[testCase], 'fileExt() extract file extension');
}
})();
})();
t.comment('fillBlanks()');
(function() {
t.comment('fillBlanks()');
(function() {
testCases = {
'foo': 'foo ',
' foo bar ': ' foo bar ',
......@@ -28,10 +27,10 @@
for (var testCase in testCases) {
t.assertEquals(utils.fillBlanks(testCase, 10), testCases[testCase], 'fillBlanks() fills blanks');
}
})();
})();
t.comment('isClipRect()');
(function() {
t.comment('isClipRect()');
(function() {
testCases = [
[{}, false],
[{top: 2}, false],
......@@ -43,10 +42,10 @@
testCases.forEach(function(testCase) {
t.assertEquals(utils.isClipRect(testCase[0]), testCase[1], 'isClipRect() checks for a ClipRect');
});
})();
})();
t.comment('isJsFile()');
(function() {
t.comment('isJsFile()');
(function() {
testCases = {
'': false,
'toto.png': false,
......@@ -58,10 +57,10 @@
for (var testCase in testCases) {
t.assertEquals(utils.isJsFile(testCase), testCases[testCase], 'isJsFile() checks for js file');
}
})();
})();
t.comment('mergeObjects()');
(function() {
t.comment('mergeObjects()');
(function() {
testCases = [
{
obj1: {a: 1}, obj2: {b: 2}, merged: {a: 1, b: 2}
......@@ -89,7 +88,6 @@
testCases.forEach(function(testCase) {
t.assertEquals(utils.mergeObjects(testCase.obj1, testCase.obj2), testCase.merged, 'mergeObjects() can merge objects');
});
})();
})();
t.done();
})(casper.test);
t.done();
......
(function(t) {
t.comment('phantom.Casper.XUnitExporter');
casper.test.comment('phantom.Casper.XUnitExporter');
xunit = require('xunit').create();
xunit.addSuccess('foo', 'bar');
t.assertMatch(xunit.getXML(), /<testcase classname="foo" name="bar"/, 'XUnitExporter.addSuccess() adds a successful testcase');
xunit.addFailure('bar', 'baz', 'wrong', 'chucknorriz');
t.assertMatch(xunit.getXML(), /<testcase classname="bar" name="baz"><failure type="chucknorriz">wrong/, 'XUnitExporter.addFailure() adds a failed testcase');
xunit = require('xunit').create();
xunit.addSuccess('foo', 'bar');
casper.test.assertMatch(xunit.getXML(), /<testcase classname="foo" name="bar"/, 'XUnitExporter.addSuccess() adds a successful testcase');
xunit.addFailure('bar', 'baz', 'wrong', 'chucknorriz');
casper.test.assertMatch(xunit.getXML(), /<testcase classname="bar" name="baz"><failure type="chucknorriz">wrong/, 'XUnitExporter.addFailure() adds a failed testcase');
t.done();
})(casper.test);
casper.test.done();
......