Commit 02a19f6e 02a19f6e5e4e0e04e4050aa14062f0cd8b89a91b by Nicolas Perriault

fixes #259 - enhanced Tester.assertField()

Tester.assertField() can now test for field values from other types than inputs.
1 parent 88229501
CasperJS Changelog
==================
XXXX-XX-XX, v1.0.0
------------------
This version is yet to be released.
- fixed [#259](https://github.com/n1k0/casperjs/issues/259) - enhanced `Tester.assertField()` method, which can now tests for other field types than `input`s.
- added [`ClientUtils.getFieldValue()`](http://casperjs.org/api.html#clientutils.getFieldValue)
2012-10-23, v1.0.0-RC3
----------------------
......
Subproject commit 9f6b066084b50ff44ebe4e3380226db4b5393d61
Subproject commit 4826e2bb9a2ecbaf0511b6aec0fd3b68d566e23b
......
......@@ -408,6 +408,34 @@
return nodes;
};
this.getFieldValue = function getFieldValue(inputName) {
var inputs = this.findAll('[name="' + inputName + '"]');
switch (inputs.length) {
case 0:
return null;
case 1:
return inputs[0].value;
default:
var type = inputs[0].getAttribute('type').toLowerCase();
if (type === 'radio') {
var value;
[].forEach.call(inputs, function(radio) {
value = radio.checked ? radio.value : undefined;
});
return value;
} else if (type === 'checkbox') {
var values = [];
[].forEach.call(inputs, function(checkbox) {
if (checkbox.checked) {
values.push(checkbox.value);
}
});
return values;
}
break;
}
};
/**
* Logs a message. Will format the message a way CasperJS will be able
* to log phantomjs side.
......
......@@ -28,7 +28,7 @@
*
*/
/*global CasperError exports phantom require*/
/*global CasperError exports phantom require __utils__*/
var fs = require('fs');
var events = require('events');
......@@ -238,24 +238,23 @@ Tester.prototype.assertEvalEquals = Tester.prototype.assertEvalEqual = function
/**
* Asserts that a given input field has the provided value.
*
* @param String input_name The name attribute of the input element
* @param String expected_value The expected value of the input element
* @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
* @return Object An assertion result object
*/
Tester.prototype.assertField = function assertField(input_name, expected_value, message) {
Tester.prototype.assertField = function assertField(inputName, expected, message) {
"use strict";
var actual_value = this.casper.evaluate(function(input_name) {
var input = document.querySelector('input[name="' + input_name + '"]');
return input ? input.value : null;
}, { input_name: input_name });
return this.assert(this.testEquals(actual_value, expected_value), message, {
var actual = this.casper.evaluate(function(inputName) {
return __utils__.getFieldValue(inputName);
}, { inputName: inputName });
return this.assert(this.testEquals(actual, expected), message, {
type: 'assertField',
standard: f('"%s" input field has the value "%s"', input_name, expected_value),
standard: f('"%s" input field has the value "%s"', inputName, expected),
values: {
input_name: input_name,
actual_value: actual_value,
expected_value: expected_value
inputName: inputName,
actual: actual,
expected: expected
}
});
};
......
......@@ -90,9 +90,6 @@ exports.dump = dump;
*/
function equals(v1, v2) {
"use strict";
if (betterTypeOf(v1) !== betterTypeOf(v2)) {
return false;
}
if (isFunction(v1)) {
return v1.toString() === v2.toString();
}
......
/*global casper*/
/*jshint strict:false maxstatements:99*/
var fs = require('fs');
var t = casper.test;
casper.start();
......@@ -21,9 +22,6 @@ var expected = [
t.assertEquals(files, expected, 'findTestFiles() find test files and sort them');
casper.thenOpen('tests/site/index.html', function() {
t.comment('Tester.assertField()');
t.assertField('dummy_name', 'dummy_value', 'Tester.assertField() works as expected');
t.comment('Tester.assertTextExists()');
t.assertTextExists('form', 'Tester.assertTextExists() checks that page body contains text');
......@@ -97,6 +95,27 @@ casper.then(function() {
t.assertNotVisible('p#hidden', 'Tester.assertNotVisible() works as expected');
});
casper.thenOpen('tests/site/form.html', function() {
t.comment('Tester.assertField()');
var fpath = phantom.libraryPath + '/README.md';
this.fill('form[action="result.html"]', {
'email': 'chuck@norris.com',
'content': 'Am watching thou',
'check': 'on',
'choice': 'no',
'topic': 'bar',
'file': fpath,
'checklist[]': ['1', '3']
});
t.assertField('email', 'chuck@norris.com', 'Tester.assertField() works as expected with inputs');
t.assertField('content', 'Am watching thou', 'Tester.assertField() works as expected with textarea');
t.assertField('check', 'on', 'Tester.assertField() works as expected with checkboxes');
t.assertField('choice', 'no', 'Tester.assertField() works as expected with radios');
t.assertField('topic', 'bar', 'Tester.assertField() works as expected with selects');
t.assertField('file', 'C:\\fakepath\\README.md', 'Tester.assertField() works as expected with file inputs');
t.assertField('checklist[]', ['1', '3'], 'Tester.assertField() works as expected with check lists');
});
casper.then(function() {
t.comment('Tester.getFailures()');
t.assertEquals(typeof t.getFailures().length, "number", "Tester.getFailures() works as expected");
......