diff --git a/.gitignore b/.gitignore index 48cbf9ff2..f5a9865df 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .DS_Store *.xml *.png +/tmp diff --git a/tests/suites/casper/capture.js b/tests/suites/casper/capture.js index f4f02a76a..6a2065a44 100644 --- a/tests/suites/casper/capture.js +++ b/tests/suites/casper/capture.js @@ -1,22 +1,20 @@ -(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)) { - fs.remove(testFile); - } +if (fs.exists(testFile) && fs.isFile(testFile)) { + fs.remove(testFile); +} - casper.start('tests/site/index.html', function(self) { - self.viewport(300, 200); - t.comment('Casper.capture()'); - self.capture(testFile); - t.assert(fs.isFile(testFile), 'Casper.capture() captured a screenshot'); - }); +casper.start('tests/site/index.html', function(self) { + self.viewport(300, 200); + this.test.comment('Casper.capture()'); + self.capture(testFile); + this.test.assert(fs.isFile(testFile), 'Casper.capture() captured a screenshot'); +}); - try { - fs.remove(testFile); - } catch(e) {} +try { + fs.remove(testFile); +} catch(e) {} - casper.run(function(self) { - t.done(); - }); -})(casper.test); +casper.run(function(self) { + this.test.done(); +}); diff --git a/tests/suites/casper/click.js b/tests/suites/casper/click.js index cbd08162d..c644b736f 100644 --- a/tests/suites/casper/click.js +++ b/tests/suites/casper/click.js @@ -1,49 +1,47 @@ -(function(t) { - casper.start('tests/site/index.html', function() { - this.click('a[href="test.html"]'); - }); +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()'); - 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'); - this.test.assert(this.click('#test4'), 'CasperUtils.click() can click an unobstrusive js handled link'); - var results = this.getGlobal('results'); - this.test.assert(results.test1, 'CasperUtils.click() has clicked an `href="javascript:` link'); - 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'); - }); +// 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'); + this.test.assert(this.click('#test4'), 'CasperUtils.click() can click an unobstrusive js handled link'); + var results = this.getGlobal('results'); + this.test.assert(results.test1, 'CasperUtils.click() has clicked an `href="javascript:` link'); + 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()'); - 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'); +// 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.mouse.up(200, 100); - results = this.getGlobal('results'); - this.test.assertEquals(results.testup, [200, 100], 'Mouse.up() has released button to the specified position'); + 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.mouse.move(200, 100); - results = this.getGlobal('results'); - this.test.assertEquals(results.testmove, [200, 100], 'Mouse.move() has moved to the specified position'); - }); + 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(); +}); diff --git a/tests/suites/casper/clientutils.js b/tests/suites/casper/clientutils.js index 1b37179b1..6cdebed2e 100644 --- a/tests/suites/casper/clientutils.js +++ b/tests/suites/casper/clientutils.js @@ -1,25 +1,23 @@ -(function(t) { - t.comment('ClientUtils.encode()'); +var fs = require('fs'); +var clientutils = require('clientutils').create(); +var testCases = { + 'an empty string': '', + 'a word': 'plop', + 'a null char': 'a\u0000', + 'an utf8 string': 'ÀÁÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ', + 'song lyrics': ("Voilà l'été, j'aperçois le soleil\n" + + "Les nuages filent et le ciel s'éclaircit\n" + + "Et dans ma tête qui bourdonnent?\n" + + "Les abeilles!"), + 'a file contents': fs.read(phantom.casperPath + '/tests/site/alert.html') +}; - var fs = require('fs'); - var clientutils = require('clientutils').create(); - var testCases = { - 'an empty string': '', - 'a word': 'plop', - 'a null char': 'a\u0000', - 'an utf8 string': 'ÀÁÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ', - 'song lyrics': ("Voilà l'été, j'aperçois le soleil\n" + - "Les nuages filent et le ciel s'éclaircit\n" + - "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) { - var source = testCases[what]; - var encoded = clientutils.encode(source); - t.assertEquals(clientutils.decode(encoded), source, 'ClientUtils can encode and decode ' + what); - } +for (var what in testCases) { + var source = testCases[what]; + var encoded = clientutils.encode(source); + casper.test.assertEquals(clientutils.decode(encoded), source, 'ClientUtils can encode and decode ' + what); +} - t.done(); -})(casper.test); +casper.test.done(); diff --git a/tests/suites/casper/encode.js b/tests/suites/casper/encode.js index 1dfe0258d..247907e5c 100644 --- a/tests/suites/casper/encode.js +++ b/tests/suites/casper/encode.js @@ -1,21 +1,20 @@ -(function(t) { - casper.start('tests/site/index.html', function(self) { - var imageUrl = 'file://' + phantom.casperPath + '/tests/site/images/phantom.png'; - var image = self.base64encode(imageUrl); - var fs = require('fs'); +var fs = require('fs'); - t.comment('Casper.base64encode()'); - t.assertEquals(image.length, 6160, 'Casper.base64encode() can retrieve base64 contents'); +casper.start('tests/site/index.html', function() { + var imageUrl = 'file://' + phantom.casperPath + '/tests/site/images/phantom.png'; + var image = this.base64encode(imageUrl); - t.comment('Casper.download()'); - self.download(imageUrl, 'logo.png'); - t.assert(fs.exists('logo.png'), 'Casper.download() downloads a file'); - if (fs.exists('logo.png')) { - fs.remove('logo.png'); - } - }); + this.test.comment('Casper.base64encode()'); + this.test.assertEquals(image.length, 6160, 'Casper.base64encode() can retrieve base64 contents'); - casper.run(function(self) { - t.done(); - }); -})(casper.test); + 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() { + this.test.done(); +}); diff --git a/tests/suites/casper/evaluate.js b/tests/suites/casper/evaluate.js index 69b3cfa36..7d80d1d6f 100644 --- a/tests/suites/casper/evaluate.js +++ b/tests/suites/casper/evaluate.js @@ -1,29 +1,27 @@ -(function(t) { - t.comment('Casper.evaluate()'); +casper.test.comment('Casper.evaluate()'); - casper.start(); +casper.start(); - var params = { - "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} - }; +var params = { + "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} +}; - var casperParams = casper.evaluate(function() { - return __casper_params__; - }, params); +var casperParams = casper.evaluate(function() { + return __casper_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) { - 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'); - } +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(); diff --git a/tests/suites/casper/exists.js b/tests/suites/casper/exists.js index 23b3100e9..c5c563069 100644 --- a/tests/suites/casper/exists.js +++ b/tests/suites/casper/exists.js @@ -1,11 +1,9 @@ -(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(); +}); diff --git a/tests/suites/casper/fetchtext.js b/tests/suites/casper/fetchtext.js index 5613855c8..d4d120cb8 100644 --- a/tests/suites/casper/fetchtext.js +++ b/tests/suites/casper/fetchtext.js @@ -1,11 +1,9 @@ -(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(); +}); diff --git a/tests/suites/casper/flow.coffee b/tests/suites/casper/flow.coffee index 7f3313955..a4285de24 100644 --- a/tests/suites/casper/flow.coffee +++ b/tests/suites/casper/flow.coffee @@ -1,39 +1,38 @@ -do(casper) -> - step = 0 +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" +# 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" @wait 200, -> - @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" + @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" +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" @wait 300, -> - @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" + @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()) \ No newline at end of file +casper.run(-> @test.done()) \ No newline at end of file diff --git a/tests/suites/casper/formfill.js b/tests/suites/casper/formfill.js index 9fc02446c..989b2503f 100644 --- a/tests/suites/casper/formfill.js +++ b/tests/suites/casper/formfill.js @@ -1,54 +1,52 @@ -(function(t) { - casper.start('tests/site/form.html', function(self) { - t.comment('Casper.fill()'); - self.fill('form[action="result.html"]', { - email: 'chuck@norris.com', - content: 'Am watching thou', - check: true, - choice: 'no', - topic: 'bar', - file: phantom.libraryPath + '/README.md', - 'checklist[]': ['1', '3'] - }); - t.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() { - return document.querySelector('textarea[name="content"]').value; - }, 'Am watching thou', 'Casper.fill() can fill a textarea form field'); - t.assertEvalEquals(function() { - return document.querySelector('select[name="topic"]').value; - }, 'bar', 'Casper.fill() can pick a value from a select form field'); - t.assertEvalEquals(function() { - return document.querySelector('input[name="check"]').checked; - }, true, 'Casper.fill() can check a form checkbox'); - t.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() { - return document.querySelector('input[name="choice"][value="yes"]').checked; - }, false, 'Casper.fill() can check a form radio button 2/2'); - t.assertEvalEquals(function() { - return document.querySelector('input[name="file"]').files.length === 1; - }, true, 'Casper.fill() can select a file to upload'); - t.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"]'); +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, + choice: 'no', + topic: 'bar', + file: phantom.libraryPath + '/README.md', + 'checklist[]': ['1', '3'] }); + 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('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'); + 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(); +}); diff --git a/tests/suites/casper/global.js b/tests/suites/casper/global.js index 769897c96..269028d95 100644 --- a/tests/suites/casper/global.js +++ b/tests/suites/casper/global.js @@ -1,11 +1,9 @@ -(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(); +}); diff --git a/tests/suites/casper/history.js b/tests/suites/casper/history.js index 6b6dcd0a7..b3e2cb522 100644 --- a/tests/suites/casper/history.js +++ b/tests/suites/casper/history.js @@ -1,23 +1,21 @@ -(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(); +}); diff --git a/tests/suites/casper/hooks.js b/tests/suites/casper/hooks.js index 0e5d8d1e6..23790c235 100644 --- a/tests/suites/casper/hooks.js +++ b/tests/suites/casper/hooks.js @@ -1,41 +1,42 @@ -(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'); - self.options.onStepComplete = null; - }; - return 'ok'; - }); +// Dear curious test reader, +// The on* family of methods is considered deprecated since 0.6.0; please use events instead - // 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'); - 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'); - self.options.onResourceRequested = null; - }; - self.thenOpen('tests/site/page1.html'); - }); +// 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.onAlert() - casper.then(function(self) { - self.options.onAlert = function(self, message) { - t.assertEquals(message, 'plop', 'Casper.options.onAlert() can intercept an alert message'); - }; - }).thenOpen('tests/site/alert.html').thenClick('button', function(self) { - self.options.onAlert = null; - }); +// 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; + }; + 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; + }; + this.thenOpen('tests/site/page1.html'); +}); - casper.run(function(self) { - t.done(); - }); -})(casper.test); +// 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() { + this.options.onAlert = null; +}); + +casper.run(function() { + this.test.done(); +}); diff --git a/tests/suites/casper/logging.js b/tests/suites/casper/logging.js index 450c59cb5..d31e2c815 100644 --- a/tests/suites/casper/logging.js +++ b/tests/suites/casper/logging.js @@ -1,40 +1,38 @@ -(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) { - return e.message === 'foo' && e.level === 'info'; - }), 'Casper.log() adds a log entry'); +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 = oldLevel; - casper.options.verbose = true; +casper.options.logLevel = oldLevel; +casper.options.verbose = true; - casper.then(function(self) { - 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'); - }); - t.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) { - 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.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(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(); +}); diff --git a/tests/suites/casper/open.js b/tests/suites/casper/open.js index 0bc125f27..3ed725251 100644 --- a/tests/suites/casper/open.js +++ b/tests/suites/casper/open.js @@ -1,75 +1,73 @@ -(function(t) { - var 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"); - } - ]; +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"); + } +]; - casper.start(); +casper.start(); - casper.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() { - t.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() { - t.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() { + t.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() { + t.pass("Casper.open() can open and load a location using HTTP auth"); +}); - casper.run(function() { - this.removeAllListeners('open'); - t.done(); - }); -})(casper.test); +casper.run(function() { + this.removeAllListeners('open'); + t.done(); +}); diff --git a/tests/suites/casper/resources.coffee b/tests/suites/casper/resources.coffee index 51b7730db..e12f787fc 100644 --- a/tests/suites/casper/resources.coffee +++ b/tests/suites/casper/resources.coffee @@ -1,25 +1,24 @@ -do(casper) -> - 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( - /phantom\.png/i - "phantom image found via test RegExp" - ) - @test.assertResourceExists( - (res) -> res.url.match "phantom.png" - "phantom image found via test Function" - ) - @test.assertResourceExists( - "phantom.png" - "phantom image found via test String" - ) - onTimeout = -> @test.fail "waitForResource timeout occured" - @waitForResource "phantom.png", onTime, onTimeout +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( + /phantom\.png/i + "phantom image found via test RegExp" + ) + @test.assertResourceExists( + (res) -> res.url.match "phantom.png" + "phantom image found via test Function" + ) + @test.assertResourceExists( + "phantom.png" + "phantom image found via test String" + ) + onTimeout = -> @test.fail "waitForResource timeout occured" + @waitForResource "phantom.png", onTime, onTimeout - casper.run(-> @test.done()) +casper.run(-> @test.done()) diff --git a/tests/suites/casper/start.js b/tests/suites/casper/start.js index 342d85d65..d37f7e660 100644 --- a/tests/suites/casper/start.js +++ b/tests/suites/casper/start.js @@ -1,17 +1,15 @@ -(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() { - 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'); +}); - 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(); +}); diff --git a/tests/suites/casper/steps.js b/tests/suites/casper/steps.js index 981269f3e..7c29531e7 100644 --- a/tests/suites/casper/steps.js +++ b/tests/suites/casper/steps.js @@ -1,34 +1,32 @@ -(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) { - self.test.assertEquals(i, item - 1, 'Casper.each() passes a contextualized index'); - }); +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(); +}); diff --git a/tests/suites/casper/viewport.js b/tests/suites/casper/viewport.js index fc628add5..4b486f1e2 100644 --- a/tests/suites/casper/viewport.js +++ b/tests/suites/casper/viewport.js @@ -1,13 +1,11 @@ -(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(); diff --git a/tests/suites/casper/visible.js b/tests/suites/casper/visible.js index 5f63cb64d..e0efab8ac 100644 --- a/tests/suites/casper/visible.js +++ b/tests/suites/casper/visible.js @@ -1,19 +1,17 @@ -(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'); - }, 2000); - }); +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(); +}); diff --git a/tests/suites/casper/wait.js b/tests/suites/casper/wait.js index 8449083da..bb3a69364 100644 --- a/tests/suites/casper/wait.js +++ b/tests/suites/casper/wait.js @@ -1,29 +1,27 @@ -(function(t) { - var waitStart; +var waitStart; - casper.start('tests/site/index.html', function(self) { - waitStart = new Date().getTime(); - }); +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.waitFor() - casper.thenOpen('tests/site/waitFor.html', function(self) { - casper.test.comment('Casper.waitFor()'); - self.waitFor(function(self) { - return self.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'); +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() { + this.test.comment('Casper.waitFor()'); + this.waitFor(function() { + return this.evaluate(function() { + return document.querySelectorAll('li').length === 4; }); + }, 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(); +}); diff --git a/tests/suites/cli.js b/tests/suites/cli.js index bfea903a7..ec7e74fb5 100644 --- a/tests/suites/cli.js +++ b/tests/suites/cli.js @@ -1,72 +1,70 @@ -(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) { - 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([])); +(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([])); - (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'); - t.assertEquals(parsed.get(1), 'bar', 'parse() retrieve second positional arg'); - 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'])); +(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'); + t.assertEquals(parsed.get(1), 'bar', 'parse() retrieve second positional arg'); + 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'])); - (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'])); +(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'])); - (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'])); +(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'])); - (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'); - t.assertEquals(parsed.get('lap'), 13.37, 'parse() can cast a float option value'); - t.assertType(parsed.get('lap'), "number", 'parse() can cast a boolean value'); - t.assert(parsed.get('chucknorris'), 'parse() can get a flag value by its option name'); - t.assertType(parsed.get('oops'), "boolean", 'parse() can cast a boolean value'); - t.assertEquals(parsed.get('oops'), false, 'parse() can cast a boolean value'); - 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("universe"), 'has() checks if an option is set'); - t.assert(parsed.has("lap"), 'has() checks if an option is set'); - t.assert(parsed.has("chucknorris"), 'has() checks if an option is set'); - t.assert(parsed.has("oops"), 'has() checks if an option is set'); +(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'); + t.assertEquals(parsed.get('lap'), 13.37, 'parse() can cast a float option value'); + t.assertType(parsed.get('lap'), "number", 'parse() can cast a boolean value'); + t.assert(parsed.get('chucknorris'), 'parse() can get a flag value by its option name'); + t.assertType(parsed.get('oops'), "boolean", 'parse() can cast a boolean value'); + t.assertEquals(parsed.get('oops'), false, 'parse() can cast a boolean value'); + 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("universe"), 'has() checks if an option is set'); + t.assert(parsed.has("lap"), 'has() checks if an option is set'); + t.assert(parsed.has("chucknorris"), 'has() checks if an option is set'); + t.assert(parsed.has("oops"), 'has() checks if an option is set'); - t.comment('drop()'); + t.comment('drop()'); - parsed.drop(0); - t.assertEquals(parsed.get(0), 'baz & boz', 'drop() dropped arg'); - parsed.drop("universe"); - t.assert(!parsed.has("universe"), 'drop() dropped option'); - t.assertEquals(parsed.args, ["baz & boz"], 'drop() did not affect other args'); - t.assertEquals(parsed.options, { - lap: 13.37, - chucknorris: true, - oops: false - }, 'drop() did not affect other options'); - })(cli.parse(['foo & bar', 'baz & boz', '--universe=42', '--lap=13.37', '--chucknorris', '--oops=false'])); + parsed.drop(0); + t.assertEquals(parsed.get(0), 'baz & boz', 'drop() dropped arg'); + parsed.drop("universe"); + t.assert(!parsed.has("universe"), 'drop() dropped option'); + t.assertEquals(parsed.args, ["baz & boz"], 'drop() did not affect other args'); + t.assertEquals(parsed.options, { + lap: 13.37, + chucknorris: true, + oops: false + }, 'drop() did not affect other options'); +})(cli.parse(['foo & bar', 'baz & boz', '--universe=42', '--lap=13.37', '--chucknorris', '--oops=false'])); - t.done(); -})(casper.test); +t.done(); diff --git a/tests/suites/coffee.coffee b/tests/suites/coffee.coffee index f4e358530..fb03260ac 100644 --- a/tests/suites/coffee.coffee +++ b/tests/suites/coffee.coffee @@ -1,20 +1,19 @@ "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", -> - @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.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 -> - @test.assertTitle "CasperJS test target", "Casper.click() casper can click on a text link" - @click "a[href=\"form.html\"]" +casper.then -> + @test.assertTitle "CasperJS test target", "Casper.click() casper can click on a text link" + @click "a[href=\"form.html\"]" - casper.run -> - @test.assertEquals steps, 3, "Casper.options.onStepComplete() is called on step complete" - @options.onStepComplete = null - @test.done() +casper.run -> + @test.assertEquals steps, 3, "Casper.options.onStepComplete() is called on step complete" + @options.onStepComplete = null + @test.done() diff --git a/tests/suites/error.js b/tests/suites/error.js index 3f0dd2262..4b6d3a215 100644 --- a/tests/suites/error.js +++ b/tests/suites/error.js @@ -1,22 +1,20 @@ -(function(t) { - var error; +var error; - function foo() { - bar(); - } +function foo() { + bar(); +} - function bar() { - throw new CasperError('bar'); - } +function bar() { + throw new CasperError('bar'); +} - try { - foo(); - } catch (e) { - error = e; - } +try { + foo(); +} 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(); diff --git a/tests/suites/injector.js b/tests/suites/injector.js index b322dd7d8..1f1c00231 100644 --- a/tests/suites/injector.js +++ b/tests/suites/injector.js @@ -1,57 +1,55 @@ -(function(t) { - function createInjector(fn, values) { - return require('injector').create(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) { + 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), { + 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), { + name: 'Plip', + args: [], + body: "return 'plop';" +}, '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), { + name: null, + args: ['$bubu_bibi', '__popo__'], + body: "" +}, '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'); - 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), { - 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), { - name: 'Plip', - args: [], - body: "return 'plop';" - }, '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), { - name: null, - args: ['$bubu_bibi', '__popo__'], - body: "" - }, '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'); +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(); diff --git a/tests/suites/tester.js b/tests/suites/tester.js index 391fc8e96..0467fa72b 100644 --- a/tests/suites/tester.js +++ b/tests/suites/tester.js @@ -1,27 +1,27 @@ -(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(); diff --git a/tests/suites/utils.js b/tests/suites/utils.js index 27d0861b7..b578d30d4 100644 --- a/tests/suites/utils.js +++ b/tests/suites/utils.js @@ -1,95 +1,93 @@ -(function(t) { - var utils = require('utils'); +var utils = require('utils'), t = casper.test; - t.comment('fileExt()'); - (function() { - var testCases = { - 'foo.ext': 'ext', - 'FOO.EXT': 'ext', - 'a.ext': 'ext', - '.ext': 'ext', - 'toto.': '', - ' plop.ext ': 'ext' - }; +t.comment('fileExt()'); +(function() { + var testCases = { + 'foo.ext': 'ext', + 'FOO.EXT': 'ext', + 'a.ext': 'ext', + '.ext': 'ext', + 'toto.': '', + ' plop.ext ': 'ext' + }; - for (var testCase in testCases) { - t.assertEquals(utils.fileExt(testCase), testCases[testCase], 'fileExt() extract file extension'); - } - })(); + for (var testCase in testCases) { + t.assertEquals(utils.fileExt(testCase), testCases[testCase], 'fileExt() extract file extension'); + } +})(); - t.comment('fillBlanks()'); - (function() { - testCases = { - 'foo': 'foo ', - ' foo bar ': ' foo bar ', - ' foo bar ': ' foo bar ' - }; +t.comment('fillBlanks()'); +(function() { + testCases = { + 'foo': 'foo ', + ' foo bar ': ' foo bar ', + ' foo bar ': ' foo bar ' + }; - for (var testCase in testCases) { - t.assertEquals(utils.fillBlanks(testCase, 10), testCases[testCase], 'fillBlanks() fills blanks'); - } - })(); + for (var testCase in testCases) { + t.assertEquals(utils.fillBlanks(testCase, 10), testCases[testCase], 'fillBlanks() fills blanks'); + } +})(); - t.comment('isClipRect()'); - (function() { - testCases = [ - [{}, false], - [{top: 2}, false], - [{top: 2, left: 2, width: 2, height: 2}, true], - [{top: 2, left: 2, height: 2, width: 2}, true], - [{top: 2, left: 2, width: 2, height: new Date()}, false] - ]; +t.comment('isClipRect()'); +(function() { + testCases = [ + [{}, false], + [{top: 2}, false], + [{top: 2, left: 2, width: 2, height: 2}, true], + [{top: 2, left: 2, height: 2, width: 2}, true], + [{top: 2, left: 2, width: 2, height: new Date()}, false] + ]; - testCases.forEach(function(testCase) { - t.assertEquals(utils.isClipRect(testCase[0]), testCase[1], 'isClipRect() checks for a ClipRect'); - }); - })(); + testCases.forEach(function(testCase) { + t.assertEquals(utils.isClipRect(testCase[0]), testCase[1], 'isClipRect() checks for a ClipRect'); + }); +})(); - t.comment('isJsFile()'); - (function() { - testCases = { - '': false, - 'toto.png': false, - 'plop': false, - 'gniii.coffee': true, - 'script.js': true - }; +t.comment('isJsFile()'); +(function() { + testCases = { + '': false, + 'toto.png': false, + 'plop': false, + 'gniii.coffee': true, + 'script.js': true + }; - for (var testCase in testCases) { - t.assertEquals(utils.isJsFile(testCase), testCases[testCase], 'isJsFile() checks for js file'); - } - })(); + for (var testCase in testCases) { + t.assertEquals(utils.isJsFile(testCase), testCases[testCase], 'isJsFile() checks for js file'); + } +})(); - t.comment('mergeObjects()'); - (function() { - testCases = [ - { - obj1: {a: 1}, obj2: {b: 2}, merged: {a: 1, b: 2} - }, - { - obj1: {}, obj2: {a: 1}, merged: {a: 1} - }, - { - obj1: {a: 1}, obj2: {}, merged: {a: 1} - }, - { - obj1: {a: 1}, obj2: {a: 2}, merged: {a: 2} - }, - { - obj1: {x: 0, double: function(){return this.x*2;}}, - obj2: {triple: function(){return this.x*3;}}, - merged: { - x: 0, - double: function(){return this.x*2;}, - triple: function(){return this.x*3;} - } +t.comment('mergeObjects()'); +(function() { + testCases = [ + { + obj1: {a: 1}, obj2: {b: 2}, merged: {a: 1, b: 2} + }, + { + obj1: {}, obj2: {a: 1}, merged: {a: 1} + }, + { + obj1: {a: 1}, obj2: {}, merged: {a: 1} + }, + { + obj1: {a: 1}, obj2: {a: 2}, merged: {a: 2} + }, + { + obj1: {x: 0, double: function(){return this.x*2;}}, + obj2: {triple: function(){return this.x*3;}}, + merged: { + x: 0, + double: function(){return this.x*2;}, + triple: function(){return this.x*3;} } - ]; + } + ]; - testCases.forEach(function(testCase) { - t.assertEquals(utils.mergeObjects(testCase.obj1, testCase.obj2), testCase.merged, 'mergeObjects() can merge objects'); - }); - })(); + testCases.forEach(function(testCase) { + t.assertEquals(utils.mergeObjects(testCase.obj1, testCase.obj2), testCase.merged, 'mergeObjects() can merge objects'); + }); +})(); - t.done(); -})(casper.test); +t.done(); diff --git a/tests/suites/xunit.js b/tests/suites/xunit.js index 20e78838b..57ee79341 100644 --- a/tests/suites/xunit.js +++ b/tests/suites/xunit.js @@ -1,11 +1,9 @@ -(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(), /wrong/, 'XUnitExporter.addFailure() adds a failed testcase'); +xunit = require('xunit').create(); +xunit.addSuccess('foo', 'bar'); +casper.test.assertMatch(xunit.getXML(), /wrong/, 'XUnitExporter.addFailure() adds a failed testcase'); - t.done(); -})(casper.test); +casper.test.done();