SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,395
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)

    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

    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?

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    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:

    Code Ruby:
    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:

    Code Ruby:
    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:

    Code Ruby:
    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:

    Code Ruby:
    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:
    http://www.sitepoint.com/tutorial-in...-with-phpunit/
    http://net.tutsplus.com/tutorials/ph...-with-phpunit/

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

  4. #4
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,395
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    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.

  5. #5
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Hey Rubble,

    Quote Originally Posted by Rubble View Post
    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.
    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.

  6. #6
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,395
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    @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.

  7. #7
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by Rubble View Post
    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.

  8. #8
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,395
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    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.

  9. #9
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by Rubble View Post
    I have a couple of errors
    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?

  10. #10
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,395
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    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.
    Last edited by Pullo; Sep 2, 2013 at 11:46. Reason: Removed userscript namespace and copyright

  11. #11
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    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?

  12. #12
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    I'm just playing around with the code now.

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

    Code:
    // ==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.

  13. #13
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,395
    Mentioned
    81 Post(s)
    Tagged
    3 Thread(s)
    Thank you Pullo and it is working now; I just need to add the other fields.

  14. #14
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,938
    Mentioned
    214 Post(s)
    Tagged
    12 Thread(s)
    Excellent


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •