Best way to error test a form

I hate error testing and I have an order for with 12 inputs and each input needs testing for allowed and non allowed input - say 5 different tests on each input :frowning:

I normaly do this manualy by inputting all the options but soon get lost with what I have and have not tried. Is there a simpler better way to do this?

I don’t know if you would benefited from some automated form testing with Selenium IDE, but I have been using this for testing different test cases and longer forms in several projects.

I’m working on something similar (not in PHP) and have to validate all kinds of input on a rather large web-facing form.
I went the TDD route which involves writing failing tests, then writing the necessary code to make the tests pass.

An example: say you want to ensure that the user doesn’t leave the email address blank, you would write something like this:

def test_should_require_email
  @applicant.email = ""
  assert !@applicant.save
end

When you run the test it will obviously fail.
Then you can write something like:

validates_presence_of :email

run the test again and it will pass.

You can then repeat this for the next desired specification, for example that an email address is valid:

def test_email_should_be_valid
    @applicant.email = "pullo@yahoo.com"
    assert @applicant.save

    @applicant.email = "pullo_at_yahoo_dot_com")
    assert !@applicant.save
end

This will fail.
Add the following code and it will pass:

validates_format_of :email, :with => /\\A([^@\\s]+)@((?:[-a-z0-9]+\\.)+[a-z]{2,})\\Z/i

So, what I might be tempted to do in your situation is to set up some unit tests on your form’s validation logic and test the form programatically.

I’m not sure about the best way to do this in PHP, but here are a couple of helpful links:

I know this might seem a bit unwieldy at first, but once you get the hang of it, it really pays dividends.

Thank you very much for the information and it has made me think:
In this case I could create some different forms that all submit to the validation code with the input variables set using value. It will be a lot quicker than filling in the form and the values can be tweeked slightly on each form by for example adding a number where it is not allowed etc.

Also since writing this I have found filter_var() which means I do not need regular expressions - I need to have a look around and see if there are any problems using it. Although I am not 100% sure about it, as validate email still lets through all these characters !#$%&'*±/=?^_`{|}~@..

I will have to check out the other options you have both posted for future use.

Hey Rubble,

That’s an interesting approach.
With the form I am working on, I wanted to do some manual testing, too, so I wrote a user script.
This script reads a bunch of values from a JS object literal, then inserts them in the form in the correct places.
I’m sure it could be easily tweaked to do what you are proposing above.
Let me know if you’re interested in this approach and I’ll give you some more info.

@Pullo ; My first thought on how to populate the table was to use php and array in some fashion but I thought it take longer to develop that than to do the error testing the manual way!

Yes I would be interested in your method although I have no experience in JS.

Well, I wrote a tutorial on how to do this using TamperMonkey and Chrome.
Have a read of that and let me know how you get on.
If you have any questions or comments I am more than happy to help out.

Pullo

I have a couple of errors and for some reason the last } was a ] or something after pasting your code.

On the $(‘Body’) line
Warning: ‘$’ Was used before it was defined
Error expected ‘;’ and instead saw ‘$’

3 Other similar errors on different lines

I presume this is preventing the code running as I do not get a Populate button.

Oh dear.
I presume you are using latest Chrome and latest TamperMonkey.

Could you post the full userscript and (if it’s not too much bother) the HTML of the form you want to autofill?

I have got rid of the errors now although I may have done it wrong and this is my code:

// ==UserScript==
// @name Auto-fill Order form
// @version 0.1
// @description Autofills fields in my massive form
// @match http://www.website.com/order.php*
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==
/global $, jQuery/
$(‘body’).prepend(‘<input type=“button” value=“Populate” id=“populate”>’);
$(“#populate”).on(“click”, function(){ console.log(“I was clicked”); });
var values = {
Forename: “Fred”,
Surname: “Blogs”,
Email: “freddyboy@gmail.com”,
Message: “Your tutorial is simply the best!”
};

$(‘body’).prepend(‘<input id=“populate” type=“button” value=“Populate”/>’);

$(“#populate”).on(“click”, function(){
Object.keys(values).forEach(function(key){
$(“#” + key).val(values[key]);
}
}

I was only trying to match a few fields to start with and see how it went.

Yes the latest version of Chrome and TamperMonkey.

I have PM’d you some further details.

That’s cool. I got your PM.
When you say, you’ve got rid of the errors, does that mean you’ve got it working?

I’m just playing around with the code now.

You had some errors in your JS.
Change your entire user script to this:

// ==UserScript==
// @name Auto-fill Order form
// @version 0.1
// @description Autofills fields in my massive form 
// @match http://www.website.com/*
// @require http://code.jquery.com/jquery-latest.js
// ==/UserScript==

var values = {
  Forname: "Fred",
  surname: "Blogs",
  email: "freddyboy@gmail.com"
};

$('body').prepend('<input type="button" value="Populate" id="populate">');

$("#populate").on("click", function(){
  Object.keys(values).forEach(function(key){
    $("#" + key).val(values[key]);
  });
});

Don’t forget to adapt the path, but leave “order.php” off the end.

Thank you Pullo and it is working now; I just need to add the other fields.

Excellent :slight_smile: