Commit 4e7d7382 4e7d7382e488c3c34f7350fb6a112e1ab5b67f39 by Nicolas Perriault

refs #28 - fixed several issues with portability

1 parent 6bfdb427
...@@ -25,34 +25,28 @@ ...@@ -25,34 +25,28 @@
25 * DEALINGS IN THE SOFTWARE. 25 * DEALINGS IN THE SOFTWARE.
26 * 26 *
27 */ 27 */
28 var fs = require('fs'); 28 (function(phantom) {
29 var fs = require('fs');
29 30
30 var casperScript = phantom.args[1]; 31 fs.pathJoin = function() {
32 return Array.prototype.join.call(arguments, this.separator);
33 };
31 34
32 if (phantom.args.length < 2) { 35 if (!fs.isDirectory(phantom.args[0]) || !fs.isFile(fs.pathJoin(phantom.args[0], 'casper.js'))) {
33 if (!fs.isDirectory(phantom.args[0])) { 36 console.log('First argument must be the absolute path to casper root path.');
34 console.log('First argument must be the path to casper root path');
35 }
36 if (!fs.isFile(casperScript)) {
37 console.log('Usage: $ casperjs script.js');
38 }
39 phantom.exit(1); 37 phantom.exit(1);
40 } 38 }
41
42 var casperPath = phantom.args[0];
43
44 function pathJoin() {
45 return Array.prototype.join.call(arguments, fs.separator);
46 }
47
48 var casperLibPath = pathJoin(casperPath, 'lib');
49 39
50 if (!fs.isDirectory(casperLibPath)) { 40 if (!fs.isFile(phantom.args[1])) {
51 console.log("Couldn't find CasperJS lib directory: " + casperLibPath); 41 console.log('Usage: $ casperjs script.[js|coffee]');
52 phantom.exit(1); 42 phantom.exit(1);
53 } 43 }
54 44
55 [ 45 phantom.casperPath = phantom.args[0];
46 phantom.casperScript = phantom.args[1];
47 phantom.casperLibPath = fs.pathJoin(phantom.casperPath, 'lib');
48
49 [
56 'casper.js', 50 'casper.js',
57 'clientutils.js', 51 'clientutils.js',
58 'colorizer.js', 52 'colorizer.js',
...@@ -60,8 +54,10 @@ if (!fs.isDirectory(casperLibPath)) { ...@@ -60,8 +54,10 @@ if (!fs.isDirectory(casperLibPath)) {
60 'tester.js', 54 'tester.js',
61 'utils.js', 55 'utils.js',
62 'xunit.js' 56 'xunit.js'
63 ].forEach(function(lib) { 57 ].forEach(function(lib) {
64 phantom.injectJs(pathJoin(casperLibPath, lib)); 58 phantom.injectJs(fs.pathJoin(phantom.casperLibPath, lib));
65 }); 59 });
60
61 phantom.injectJs(phantom.args[1]);
66 62
67 phantom.injectJs(phantom.args[1]); 63 })(phantom);
......
...@@ -56,7 +56,10 @@ ...@@ -56,7 +56,10 @@
56 onStepTimeout: null, 56 onStepTimeout: null,
57 onTimeout: null, 57 onTimeout: null,
58 page: null, 58 page: null,
59 pageSettings: { userAgent: DEFAULT_USER_AGENT }, 59 pageSettings: {
60 localToRemoteUrlAccessEnabled: true,
61 userAgent: DEFAULT_USER_AGENT
62 },
60 stepTimeout: null, 63 stepTimeout: null,
61 timeout: null, 64 timeout: null,
62 verbose: false 65 verbose: false
......
1 /**
2 * Phantom args parsing utilities.
3 *
4 */
5 (function(phantom) {
6 phantom.Casper.Cli = function() {
7 /**
8 * Extract current named parameters passed to the phantom script through
9 * the command line. Named arguments are of the form --foo=bar ou --foo
10 *
11 */
12 this.named = phantom.args.forEach(function(arg) {
13 if (arg.indexOf('--') === 0) {
14 var match = /--(.*)=(.*)\s?/i.exec(arg);
15 if (match) {
16 params[match[1]] = match[2];
17 } else {
18 var match2 = /--(.*)\s?/i.exec(arg);
19 if (match2) {
20 params[match2[1]] = true;
21 }
22 }
23 }
24 });
25
26 /**
27 * Checks that the specified named arguments have been passed to current
28 * phantom script. Dies on failure by default, but you can provide an
29 * onError callback for hooking on fail and do what you want.
30 *
31 */
32 this.requireNamedArgs = function(list, onError) {
33 var missing = [], params = phantom.extractNamedArgs();
34 for (var i = 0; i < list.length; i++) {
35 var name = list[i];
36 if (!params.hasOwnProperty(name)) {
37 missing.push(name);
38 }
39 }
40 if (missing.length > 0) {
41 if (typeof(onError) === "function") {
42 onError(missing);
43 } else {
44 var s = missing.length > 1 ? 'are' : 'is a';
45 console.log(JSON.stringify({
46 status: "error",
47 message: '"' + (missing.join('", "')) + '" ' + s + ' required named parameters.'
48 }, null, ' '));
49 phantom.exit(1);
50 }
51 }
52 };
53 };
54 })(phantom);
......
...@@ -314,7 +314,7 @@ ...@@ -314,7 +314,7 @@
314 var entries = fs.list(dir).filter(function(entry) { 314 var entries = fs.list(dir).filter(function(entry) {
315 return entry !== '.' && entry !== '..'; 315 return entry !== '.' && entry !== '..';
316 }).map(function(entry) { 316 }).map(function(entry) {
317 return fs.absolute(pathJoin(dir, entry)); 317 return fs.absolute(fs.pathJoin(dir, entry));
318 }); 318 });
319 entries.forEach(function(entry) { 319 entries.forEach(function(entry) {
320 if (fs.isDirectory(entry)) { 320 if (fs.isDirectory(entry)) {
...@@ -322,7 +322,7 @@ ...@@ -322,7 +322,7 @@
322 } 322 }
323 }); 323 });
324 return entries.filter(function(entry) { 324 return entries.filter(function(entry) {
325 return isJsFile(fs.absolute(pathJoin(dir, entry))); 325 return isJsFile(fs.absolute(fs.pathJoin(dir, entry)));
326 }); 326 });
327 }; 327 };
328 328
......
1 //phantom.injectJs('casper.js');
2 phantom.injectJs('lib/vendors/esprima.js');
3
4 var fs = require('fs'); 1 var fs = require('fs');
2
3 phantom.injectJs(fs.pathJoin(phantom.casperLibPath, 'vendors', 'esprima.js'));
4
5 var casper = new phantom.Casper({ 5 var casper = new phantom.Casper({
6 faultTolerant: false, 6 faultTolerant: false,
7 verbose: true 7 verbose: true
...@@ -11,7 +11,18 @@ var tests = []; ...@@ -11,7 +11,18 @@ var tests = [];
11 if (phantom.args.length > 2 && fs.isFile(phantom.args[2])) { 11 if (phantom.args.length > 2 && fs.isFile(phantom.args[2])) {
12 tests = [phantom.args[2]]; 12 tests = [phantom.args[2]];
13 } else { 13 } else {
14 tests = [fs.absolute(pathJoin(casperLibPath, '..', 'tests', 'suites'))]; 14 tests = [fs.absolute(fs.pathJoin(phantom.casperLibPath, '..', 'tests', 'suites'))];
15 } 15 }
16 16
17 // Overriding Casper.open to prefix all test urls
18 phantom.Casper.extend({
19 open: function(location, options) {
20 options = isType(options, "object") ? options : {};
21 this.requestUrl = location;
22 var url = 'file://' + phantom.casperPath + '/' + location;
23 this.page.open(url);
24 return this;
25 }
26 });
27
17 casper.test.runSuites.apply(casper.test, tests); 28 casper.test.runSuites.apply(casper.test, tests);
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
2 t.comment('Casper.base64encode()'); 2 t.comment('Casper.base64encode()');
3 3
4 casper.start('tests/site/index.html', function(self) { 4 casper.start('tests/site/index.html', function(self) {
5 var image = self.base64encode('file://' + phantom.libraryPath + '/site/images/phantom.png'); 5 var image = self.base64encode('file://' + phantom.casperPath + '/tests/site/images/phantom.png');
6 t.assertEquals(image.length, 6160, 'Casper.base64encode() can retrieve base64 contents'); 6 t.assertEquals(image.length, 6160, 'Casper.base64encode() can retrieve base64 contents');
7 }); 7 });
8 8
......