Commit 1bb0fc7c 1bb0fc7c76dd47a2875e7161f0bbf7ff6bf1e6a9 by Nicolas Perriault

fixed #281 - Casper.evaluate() method signature

`Casper.evaluate()` method signature is now compatible with PhantomJS' one, so you can now write:

```js
casper.evaluate(function(a, b) {
    return a === "foo" && b === "bar";
}, "foo", "bar"); // true
```

The old way to pass arguments has been kept backward compatible in order not to break your existing scripts though:

```js
casper.evaluate(function(a, b) {
    return a === "foo" && b === "bar";
}, {a: "foo", b: "bar"}); // true
```
1 parent f072d130
......@@ -4,13 +4,34 @@ CasperJS Changelog
XXXX-XX-XX, v1.0.0
------------------
- fixed `Casper.die()` and `Casper.evaluateOrDie()` were not printing the error onto the console
### Important Changes & Caveats
`Casper.evaluate()` method signature is now compatible with PhantomJS' one, so you can now write:
```js
casper.evaluate(function(a, b) {
return a === "foo" && b === "bar";
}, "foo", "bar"); // true
```
The old way to pass arguments has been kept backward compatible in order not to break your existing scripts though:
```js
casper.evaluate(function(a, b) {
return a === "foo" && b === "bar";
}, {a: "foo", b: "bar"}); // true
```
### Bugfixes & enhancements
- fixed [#281](https://github.com/n1k0/casperjs/issues/281) - `Casper.evaluate()` should take an array as context not object
- fixed [#266](https://github.com/n1k0/casperjs/issues/266) - Fix `tester` module and its self tests
- fixed [#268](https://github.com/n1k0/casperjs/issues/266) - Wrong message on step timeout
- fixed [#215](https://github.com/n1k0/casperjs/issues/215) - added a `--fail-fast` option to the `casper test` command, in order to terminate a test suite execution as soon as any failure is encountered
- fixed [#274](https://github.com/n1k0/casperjs/issues/274) - some headers couldn't be set
- fixed [#277](https://github.com/n1k0/casperjs/issues/277) - multiline support in `ClientUtils.echo()`
- fixed [#282](https://github.com/n1k0/casperjs/issues/282) - added support for remote client scripts loading with a new `remoteScripts` casper option
- fixed `Casper.die()` and `Casper.evaluateOrDie()` were not printing the error onto the console
- added [`Tester.assertTextDoesntExist()`](http://casperjs.org/api.html#tester.assertTextDoesntExist)
- added `Tester.assertFalse()` as an alias of `Tester.assertNot()`
- added `page.resource.requested` and `page.resource.received` events
......
Subproject commit e15667bcfb7e76b057212f3c2337cee0a39e793e
Subproject commit 7407e89f51ffa7c95f85c017654a82de86239a17
......
......@@ -603,12 +603,27 @@ Casper.prototype.evaluate = function evaluate(fn, context) {
// ensure client utils are always injected
this.injectClientUtils();
// function context
context = utils.isObject(context) ? context : {};
// the way this works is kept for BC with older casperjs versions
var args = Object.keys(context).map(function(arg) {
if (arguments.length === 1) {
return this.page.evaluate(fn);
} else if (arguments.length === 2) {
// check for closure signature if it matches context
if (utils.isObject(context) && eval(fn).length === Object.keys(context).length) {
context = Object.keys(context).map(function(arg) {
return context[arg];
});
return this.page.evaluate.apply(this.page, [fn].concat(args));
} else {
context = [context];
}
} else if (arguments.length > 2) {
// phantomjs-style signature
context = [].slice.call(arguments).slice(1);
} else {
// old casperjs method signature
context = Object.keys(context).map(function(arg) {
return context[arg];
});
}
return this.page.evaluate.apply(this.page, [fn].concat(context));
};
/**
......
/*global casper*/
/*jshint strict:false maxparams:99*/
/*jshint strict:false*/
casper.test.comment('Casper.evaluate()');
casper.start();
......@@ -32,4 +32,42 @@ casper.test.assertEquals(result.toString(),
['boolean', 'boolean', 'number', 'number', 'string', 'object', 'object', 'function'].toString(),
'Casper.evaluate() handles passed argument context correcly');
// no context
casper.test.assertEquals(casper.evaluate(function() {
return 42;
}), 42, 'Casper.evaluate() handles evaluation with no context passed');
// object context (previous casperjs versions compatibility mode)
casper.test.assertEquals(casper.evaluate(function(a) {
return [a];
}, {a: "foo"}), ["foo"], 'Casper.evaluate() accepts an object as arguments context');
casper.test.assertEquals(casper.evaluate(function(a, b) {
return [a, b];
}, {a: "foo", b: "bar"}), ["foo", "bar"], 'Casper.evaluate() accepts an object as arguments context');
casper.test.assertEquals(casper.evaluate(function(a, b, c) {
return [a, b, c];
}, {a: "foo", b: "bar", c: "baz"}), ["foo", "bar", "baz"], 'Casper.evaluate() accepts an object as arguments context');
// array context
casper.test.assertEquals(casper.evaluate(function(a) {
return [a];
}, ["foo"]), ["foo"], 'Casper.evaluate() accepts an array as arguments context');
casper.test.assertEquals(casper.evaluate(function(a, b) {
return [a, b];
}, ["foo", "bar"]), ["foo", "bar"], 'Casper.evaluate() accepts an array as arguments context');
casper.test.assertEquals(casper.evaluate(function(a, b, c) {
return [a, b, c];
}, ["foo", "bar", "baz"]), ["foo", "bar", "baz"], 'Casper.evaluate() accepts an array as arguments context');
// natural arguments context (phantomjs equivalent)
casper.test.assertEquals(casper.evaluate(function(a) {
return [a];
}, "foo"), ["foo"], 'Casper.evaluate() accepts natural arguments context');
casper.test.assertEquals(casper.evaluate(function(a, b) {
return [a, b];
}, "foo", "bar"), ["foo", "bar"], 'Casper.evaluate() accepts natural arguments context');
casper.test.assertEquals(casper.evaluate(function(a, b, c) {
return [a, b, c];
}, "foo", "bar", "baz"), ["foo", "bar", "baz"], 'Casper.evaluate() accepts natural arguments context');
casper.test.done();
......