Commit 81f8744b 81f8744b57a37ef2f2aafe1b9d0d30e8d1cb377a by Nicolas Perriault

fixes #433 - Tester#assertField check for nonexistent fields

1 parent 3787e0af
......@@ -104,11 +104,12 @@ Last, all the casper test suites have been upgraded to use the new testing featu
### Bugfixes & enhancements
- heavy lifting of casperjs bootstrap script
- fixed [#387](https://github.com/n1k0/casperjs/issues/387) - Setting viewport isn't quite synchronous
- fixed [#410](https://github.com/n1k0/casperjs/issues/410) - trigger `mousedown` and `mousedown` events on click
- fixed [#433](https://github.com/n1k0/casperjs/issues/433) - `assertField("field", "")` will always pass even though the `field` doesn't exist
- closed [#392](https://github.com/n1k0/casperjs/issues/392) - `--direct` & `--log-level` options available for the `casperjs` executable
- closed [#350](https://github.com/n1k0/casperjs/issues/350) - Add a [`Casper#waitForSelectorTextChange()`](http://docs.casperjs.org/en/latest/modules/casper.html#waitforselectortextchange) method
- fixed [#387](https://github.com/n1k0/casperjs/issues/387) - Setting viewport isn't quite synchronous
- Added [`Casper#bypass`](http://docs.casperjs.org/en/latest/modules/casper.html#bypass), [`Casper#thenBypass`](http://docs.casperjs.org/en/latest/modules/casper.html#thenbypass), [`Casper#thenBypassIf`](http://docs.casperjs.org/en/latest/modules/casper.html#thenbypassif), [`Casper#thenBypassUnless`](http://docs.casperjs.org/en/latest/modules/casper.html#thenbypassunless) methods
- fixes [#410](https://github.com/n1k0/casperjs/issues/410) - trigger `mousedown` and `mousedown` events on click
- Added [`Tester#skip`](http://docs.casperjs.org/en/latest/modules/tester.html#skip) method
- Added [`Casper#eachThen()`](http://docs.casperjs.org/en/latest/modules/casper.html#eachThen)
- `cli`: Now dropping an arg or an option will be reflected in their *raw* equivalent
......
......@@ -242,12 +242,14 @@ Retrieves all DOM elements matching a given :ref:`XPath expression <selectors>`,
The ``scope`` argument allows to set the context for executing the XPath query.
.. _clientutils_getfieldvalue:
.. index:: Form
``getFieldValue()``
-------------------------------------------------------------------------------
**Signature:** ``getFieldValue(String inputName)``
**Signature:** ``getFieldValue(String inputName[, Object options])``
.. versionadded:: 1.0
......@@ -263,6 +265,10 @@ Using the ``getFieldValue()`` method for ``plop``::
__utils__.getFieldValue('plop'); // 42
Options:
- ``formSelector``: allows to set the selector for the form containing the target field.
.. index:: Form
``getFormValues()``
......
......@@ -160,7 +160,7 @@ Asserts that a given subject is `falsy <http://11heavens.com/falsy-and-truthy-in
``assertField()``
-------------------------------------------------------------------------------
**Signature:** ``assertField(String inputName, String expected[, String message])``
**Signature:** ``assertField(String inputName, String expected[, String message, Object options])``
Asserts that a given form field has the provided value::
......@@ -177,6 +177,11 @@ Asserts that a given form field has the provided value::
This also works with any input type: ``select``, ``textarea``, etc.
.. versionadded:: 1.1
The `options` parameter allows to set the options to use with
:ref:`ClientUtils#getFieldValue() <clientutils_getfieldvalue>`.
.. index:: HTTP, HTTP Status Code
``assertHttpStatus()``
......
......@@ -501,7 +501,7 @@
}
var inputs = this.findAll(formSelector + '[name="' + inputName + '"]');
switch (inputs.length) {
case 0: return null;
case 0: return undefined;
case 1: return getSingleValue(inputs[0]);
default: return getMultipleValues(inputs);
}
......
......@@ -420,18 +420,40 @@ Tester.prototype.assertEvalEqual = function assertEvalEquals(fn, expected, messa
};
/**
* Asserts that the provided assertion fails (used for internal testing).
*
* @param Function fn A closure calling an assertion
* @param String|null message Test description
* @return Object An assertion result object
*/
Tester.prototype.assertFail = function assertFail(fn, message) {
"use strict";
var failed = false;
try {
fn();
} catch (e) {
failed = true;
}
return this.assert(failed, message, {
type: "assertFail",
standard: "Assertion fails as expected"
});
};
/**
* Asserts that a given input field has the provided value.
*
* @param String inputName The name attribute of the input element
* @param String expected The expected value of the input element
* @param String message Test description
* @param Object options ClientUtils#getFieldValue options (optional)
* @return Object An assertion result object
*/
Tester.prototype.assertField = function assertField(inputName, expected, message) {
Tester.prototype.assertField = function assertField(inputName, expected, message, options) {
"use strict";
var actual = this.casper.evaluate(function(inputName) {
return __utils__.getFieldValue(inputName);
}, inputName);
var actual = this.casper.evaluate(function(inputName, options) {
return __utils__.getFieldValue(inputName, options);
}, inputName, options);
return this.assert(utils.equals(actual, expected), message, {
type: 'assertField',
standard: f('"%s" input field has the value "%s"', inputName, expected),
......
......@@ -2,7 +2,7 @@
/*jshint strict:false, maxstatements:99*/
var fs = require('fs');
casper.test.begin('Common assertions tests', 43, function(test) {
casper.test.begin('Common assertions tests', 45, function(test) {
casper.start('tests/site/index.html', function() {
test.assertTextExists('form', 'Tester.assertTextExists() checks that page body contains text');
test.assertTextExist('form', 'Tester.assertTextExist() checks that page body contains text [alias]');
......@@ -34,6 +34,9 @@ casper.test.begin('Common assertions tests', 43, function(test) {
test.assertElementCount('address', 0, 'Tester.assertElementCount() works as expected');
test.assertExists('body', 'Tester.assertExists() works as expected');
test.assertExist('body', 'Tester.assertExist() works as expected [alias]');
test.assertFail(function() {
test.assert(false);
}, 'Tester.assertFail() tests for a failing assertion');
test.assertSelectorExists('body', 'Tester.assertSelectorExists() works as expected [alias]');
test.assertSelectorExist('body', 'Tester.assertSelectorExist() works as expected [alias]');
test.assertDoesntExist('foobar', 'Tester.assertDoesntExist() works as expected');
......@@ -114,3 +117,14 @@ casper.test.begin('Tester.assertField(): unfilled inputs', 7, function(test) {
test.done();
});
});
casper.test.begin('Tester.assertField(): nonexistent fields', 2, function(test) {
casper.start('tests/site/form.html', function() {
test.assertFail(function() {
test.assertField('nonexistent', '');
}, 'Tester.assertField() only checks for existing fields');
});
casper.run(function() {
test.done();
})
});
......