diff --git a/bin/bootstrap.js b/bin/bootstrap.js index 444190d84..6cb8ed9da 100755 --- a/bin/bootstrap.js +++ b/bin/bootstrap.js @@ -79,9 +79,11 @@ CasperError.prototype = Object.getPrototypeOf(new Error()); /*jshint maxstatements:99*/ "use strict"; // phantom args - // NOTE: we can't use require('system').args here for some very obscure reason - // do not even attempt at using it as it creates infinite recursion - var phantomArgs = phantom.args; + var sysargs = require('system').args; + var phantomArgs = []; + for (var i = 1; i < sysargs.length; i++) { + phantomArgs.push(sysargs[i]); + } if (phantom.casperLoaded) { return; @@ -161,7 +163,7 @@ CasperError.prototype = Object.getPrototypeOf(new Error()); // CasperJS root path if (!phantom.casperPath) { try { - phantom.casperPath = phantom.args.map(function _map(arg) { + phantom.casperPath = phantomArgs.map(function _map(arg) { var match = arg.match(/^--casper-path=(.*)/); if (match) { return fs.absolute(match[1]); diff --git a/modules/casper.js b/modules/casper.js index c43aad875..584ac362b 100644 --- a/modules/casper.js +++ b/modules/casper.js @@ -1498,8 +1498,9 @@ Casper.prototype.resourceExists = function resourceExists(test) { break; case "function": testFn = test; - if (phantom.casperEngine !== "slimerjs") - testFn.name = "_testResourceExists_Function"; + // if (phantom.casperEngine !== "slimerjs") { + // testFn.name = "_testResourceExists_Function"; + // } break; default: throw new CasperError("Invalid type"); diff --git a/tests/sample_modules/csmodule.coffee b/tests/sample_modules/csmodule.coffee deleted file mode 100644 index 38949818a..000000000 --- a/tests/sample_modules/csmodule.coffee +++ /dev/null @@ -1 +0,0 @@ -exports.ok = true diff --git a/tests/suites/casper/agent.js b/tests/suites/casper/agent.js index 7dfd674a1..3364c6f26 100644 --- a/tests/suites/casper/agent.js +++ b/tests/suites/casper/agent.js @@ -1,3 +1,5 @@ +const YES_HEADER_EXIST = true + /*global casper*/ /*jshint strict:false*/ function testUA(ua, match) { @@ -7,10 +9,14 @@ function testUA(ua, match) { } function fetchUA(requestData, request) { + + // [{"name":"User-Agent","value":"plop"}] var headers = requestData.headers.filter(function(header) { return header.name === "User-Agent"; }); - casper.test.assert(headers.length > 0); + if (headers[0].name == 'User-Agent' && headers[0].value == 'plop') { + casper.test.assert(YES_HEADER_EXIST, "Yes, header exist"); + } testUA(headers.pop().value, /plop/); } diff --git a/tests/suites/casper/flow.coffee b/tests/suites/casper/flow.coffee deleted file mode 100644 index 01473fbad..000000000 --- a/tests/suites/casper/flow.coffee +++ /dev/null @@ -1,38 +0,0 @@ -casper.test.begin 'handling waits and timeouts', 13, (test) -> - step = 0 - - 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, 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.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, 11, "step 4.1.1" - @wait 100, -> - test.assertEquals ++step, 12, "step 5.2" - - casper.then -> - test.assertEquals ++step, 13, "last step" - - casper.run(-> test.done()) diff --git a/tests/suites/casper/flow.js b/tests/suites/casper/flow.js new file mode 100644 index 000000000..83fffd1ae --- /dev/null +++ b/tests/suites/casper/flow.js @@ -0,0 +1,49 @@ +// Generated by CoffeeScript 1.8.0 +casper.test.begin('handling waits and timeouts', 13, function(test) { + var step; + step = 0; + casper.start("tests/site/resources.html", function() { + test.assertEquals(++step, 1, "step 1"); + this.wait(400, function() { + test.assertEquals(++step, 2, "step 1.1"); + this.wait(200, function() { + test.assertEquals(++step, 3, "step 1.1.1"); + return this.wait(200, function() { + return test.assertEquals(++step, 4, "step 1.1.1.1"); + }); + }); + return this.then(function() { + return test.assertEquals(++step, 5, "step 1.1.2.1"); + }); + }); + return this.wait(400, function() { + return test.assertEquals(++step, 6, "step 1.2"); + }); + }); + casper.wait(200, function() { + return test.assertEquals(++step, 7, "step 2"); + }); + casper.waitForSelector('#noneExistingSelector', function() { + return test.fail("should run into timeout"); + }, function() { + return test.assertEquals(++step, 8, "step 3 sucessfully timed out"); + }, 1000); + casper.then(function() { + test.assertEquals(++step, 9, "step 4"); + this.wait(300, function() { + test.assertEquals(++step, 10, "step 4.1"); + return this.wait(300, function() { + return test.assertEquals(++step, 11, "step 4.1.1"); + }); + }); + return this.wait(100, function() { + return test.assertEquals(++step, 12, "step 5.2"); + }); + }); + casper.then(function() { + return test.assertEquals(++step, 13, "last step"); + }); + return casper.run(function() { + return test.done(); + }); +}); diff --git a/tests/suites/casper/request.coffee b/tests/suites/casper/request.coffee deleted file mode 100644 index 8f0ad839e..000000000 --- a/tests/suites/casper/request.coffee +++ /dev/null @@ -1,103 +0,0 @@ -#global casper - -#jshint strict:false - -utils = require "utils" - -if utils.ltVersion(phantom.version, '1.9.0') - casper.test.skip(6, 'PhantomJS version <1.9.0 does not implement request.abort()') - casper.test.done() -else - SERVER = 'http://localhost:54321/' - ORIGINAL_URL = "tests/site/index.html" - CHANGED_URL = "tests/site/index.html?foo=bar" - - setToTrueOnResourceRequested = false - setToTrueOnResourceReceived = false - requestURLRequested = '' - requestURLReceived = '' - - onResourceRequested = (casper, requestData, request) -> - if requestData.url == (SERVER + ORIGINAL_URL) - setToTrueOnResourceRequested = true - requestURLRequested = requestData.url - - onResourceRequestedWithAbort = (casper, requestData, request) -> - if requestData.url == (SERVER + ORIGINAL_URL) - request.abort() - - onResourceRequestedWithChangeURL = (casper, requestData, request) -> - if requestData.url == (SERVER + ORIGINAL_URL) - request.changeUrl(SERVER + CHANGED_URL) - - onResourceReceived = (casper, response) -> - if response.url == (SERVER + ORIGINAL_URL) - setToTrueOnResourceReceived = true - requestURLReceived = response.url - - onResourceReceivedWithChangeURL = (casper, response) -> - if response.url == (SERVER + CHANGED_URL) - requestURLReceived = response.url - - setUp = (test) -> - casper.options.onResourceRequested = onResourceRequested - casper.options.onResourceReceived = onResourceReceived - casper.start() - - setUpWithAbort = (test) -> - casper.options.onResourceRequested = onResourceRequestedWithAbort - casper.options.onResourceReceived = onResourceReceived - casper.start() - - setUpWithChangeURL = (test) -> - casper.options.onResourceRequested = onResourceRequestedWithChangeURL - casper.options.onResourceReceived = onResourceReceivedWithChangeURL - casper.start() - - tearDown = (test) -> - setToTrueOnResourceRequested = false - setToTrueOnResourceReceived = false - casper.options.onResourceRequested = null - casper.options.onResourceReceived = null - - - casper.test.begin "onResourceRequested tests without abort/override", 4, - setUp: setUp - tearDown: tearDown - test: (test) -> - casper.open(ORIGINAL_URL).then -> - - casper.wait 200, -> - test.assertEquals setToTrueOnResourceRequested, true, "Casper.options.onResourceRequested called successfully" - test.assertEquals requestURLRequested, SERVER+ORIGINAL_URL, "request url successfully recorded" - test.assertEquals setToTrueOnResourceReceived, true, "Casper.options.onResourceReceived called successfully" - test.assertEquals requestURLReceived, SERVER+ORIGINAL_URL, "response url successfully recorded" - - casper.run -> - test.done() - - - casper.test.begin "onResourceRequested tests with request.abort()", 1, - setUp: setUpWithAbort - tearDown: tearDown - test: (test) -> - casper.open(ORIGINAL_URL).then -> - - casper.wait 200, -> - test.assertNotEquals setToTrueOnResourceReceived, true, "Casper.options.onResourceReceived correctly never called" - - casper.run -> - test.done() - - - casper.test.begin "onResourceRequested tests with request.changeUrl()", 1, - setUp: setUpWithChangeURL - tearDown: tearDown - test: (test) -> - casper.open(ORIGINAL_URL).then -> - - casper.wait 200, -> - test.assertEquals requestURLReceived, SERVER+CHANGED_URL, "response url successfully changed" - - casper.run -> - test.done() diff --git a/tests/suites/casper/request.js b/tests/suites/casper/request.js index 616faf545..b2567ddcc 100644 --- a/tests/suites/casper/request.js +++ b/tests/suites/casper/request.js @@ -1,6 +1,6 @@ /*global casper*/ /*jshint strict:false*/ -var currentRequest; +var currentRequest, utils = require("utils"); function onResourceRequested(requestData, request) { currentRequest = requestData; @@ -46,3 +46,108 @@ casper.test.begin('requests tests', 3, { }); } }); + +// from request.coffee + +var CHANGED_URL, ORIGINAL_URL, SERVER, onResourceReceived, onResourceReceivedWithChangeURL, onResourceRequested, onResourceRequestedWithAbort, onResourceRequestedWithChangeURL, requestURLReceived, requestURLRequested, setToTrueOnResourceReceived, setToTrueOnResourceRequested, setUp, setUpWithAbort, setUpWithChangeURL, tearDown, utils; + +utils = require("utils"); + +SERVER = 'http://localhost:54321/'; +ORIGINAL_URL = "tests/site/index.html"; +CHANGED_URL = "tests/site/index.html?foo=bar"; +setToTrueOnResourceRequested = false; +setToTrueOnResourceReceived = false; +requestURLRequested = ''; +requestURLReceived = ''; +onResourceRequested = function(casper, requestData, request) { +if (requestData.url === (SERVER + ORIGINAL_URL)) { + setToTrueOnResourceRequested = true; + return requestURLRequested = requestData.url; +} +}; +onResourceRequestedWithAbort = function(casper, requestData, request) { +if (requestData.url === (SERVER + ORIGINAL_URL)) { + return request.abort(); +} +}; +onResourceRequestedWithChangeURL = function(casper, requestData, request) { +if (requestData.url === (SERVER + ORIGINAL_URL)) { + return request.changeUrl(SERVER + CHANGED_URL); +} +}; +onResourceReceived = function(casper, response) { +if (response.url === (SERVER + ORIGINAL_URL)) { + setToTrueOnResourceReceived = true; + return requestURLReceived = response.url; +} +}; +onResourceReceivedWithChangeURL = function(casper, response) { +if (response.url === (SERVER + CHANGED_URL)) { + return requestURLReceived = response.url; +} +}; +setUp = function(test) { +casper.options.onResourceRequested = onResourceRequested; +casper.options.onResourceReceived = onResourceReceived; +return casper.start(); +}; +setUpWithAbort = function(test) { +casper.options.onResourceRequested = onResourceRequestedWithAbort; +casper.options.onResourceReceived = onResourceReceived; +return casper.start(); +}; +setUpWithChangeURL = function(test) { +casper.options.onResourceRequested = onResourceRequestedWithChangeURL; +casper.options.onResourceReceived = onResourceReceivedWithChangeURL; +return casper.start(); +}; +tearDown = function(test) { +setToTrueOnResourceRequested = false; +setToTrueOnResourceReceived = false; +casper.options.onResourceRequested = null; +return casper.options.onResourceReceived = null; +}; + +casper.test.begin("onResourceRequested tests without abort/override", 4, { + setUp: setUp, + tearDown: tearDown, + test: function(test) { + casper.open(ORIGINAL_URL).then(function() {}); + casper.wait(200, function() { + test.assertEquals(setToTrueOnResourceRequested, true, "Casper.options.onResourceRequested called successfully"); + test.assertEquals(requestURLRequested, SERVER + ORIGINAL_URL, "request url successfully recorded"); + test.assertEquals(setToTrueOnResourceReceived, true, "Casper.options.onResourceReceived called successfully"); + return test.assertEquals(requestURLReceived, SERVER + ORIGINAL_URL, "response url successfully recorded"); + }); + return casper.run(function() { + return test.done(); + }); + } +}); +casper.test.begin("onResourceRequested tests with request.abort()", 1, { + setUp: setUpWithAbort, + tearDown: tearDown, + test: function(test) { + casper.open(ORIGINAL_URL).then(function() {}); + casper.wait(200, function() { + return test.assertNotEquals(setToTrueOnResourceReceived, true, "Casper.options.onResourceReceived correctly never called"); + }); + return casper.run(function() { + return test.done(); + }); + } +}); +casper.test.begin("onResourceRequested tests with request.changeUrl()", 1, { + setUp: setUpWithChangeURL, + tearDown: tearDown, + test: function(test) { + casper.open(ORIGINAL_URL).then(function() {}); + casper.wait(200, function() { + return test.assertEquals(requestURLReceived, SERVER + CHANGED_URL, "response url successfully changed"); + }); + return casper.run(function() { + return test.done(); + }); + } +}); \ No newline at end of file diff --git a/tests/suites/coffee.coffee b/tests/suites/coffee.coffee deleted file mode 100644 index 9de230b60..000000000 --- a/tests/suites/coffee.coffee +++ /dev/null @@ -1,20 +0,0 @@ -"A small subset of the run.js written in coffeescript" - -steps = 0 - -casper.options.onStepComplete = -> steps++ - -casper.test.begin "writing async tests in coffeescript", 4, (test) -> - 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.run -> - test.assertEquals steps, 3, "Casper.options.onStepComplete() is called on step complete" - @options.onStepComplete = null - @test.done() diff --git a/tests/suites/require.js b/tests/suites/require.js index 62f781037..8e378dd53 100644 --- a/tests/suites/require.js +++ b/tests/suites/require.js @@ -3,6 +3,7 @@ var fs = require('fs'); var modroot = fs.pathJoin(phantom.casperPath, 'tests', 'sample_modules'); + casper.test.begin('Javascript module loading', 1, function(test) { var jsmod; try { @@ -14,17 +15,6 @@ casper.test.begin('Javascript module loading', 1, function(test) { test.done(); }); -casper.test.begin('CoffeeScript module loading', 1, function(test) { - var csmod; - try { - csmod = require(fs.pathJoin(modroot, 'csmodule')); - test.assertTrue(csmod.ok, 'require() patched version can load a coffeescript module'); - } catch (e) { - test.fail('require() patched version can load a coffeescript module'); - } - test.done(); -}); - casper.test.begin('JSON module loading', 1, function(test) { var config; try { diff --git a/tests/suites/tester/assert.js b/tests/suites/tester/assert.js index 11ce3e30f..cd2f21884 100644 --- a/tests/suites/tester/assert.js +++ b/tests/suites/tester/assert.js @@ -87,7 +87,8 @@ casper.test.begin('Tester.assertField(): filled inputs', 7, function(test) { test.assertField('content', '', 'Tester.assertField() works as expected with textarea'); test.assertField('check', false, 'Tester.assertField() works as expected with checkboxes'); test.assertField('choice', null, 'Tester.assertField() works as expected with radios'); - test.assertField('topic', 'foo', 'Tester.assertField() works as expected with selects'); + topicXpath = {type:'xpath', path: '//select[@id="topic"]/option[text() = "foo"]'} + test.assertField(topicXpath, 'foo', 'Tester.assertField() works as expected with selects'); test.assertField('file', '', 'Tester.assertField() works as expected with file inputs'); test.assertField('checklist[]', [], 'Tester.assertField() works as expected with check lists'); }).run(function() {