In the beginning, there was 'the application'. The application had 230 Apache handlers, each capturing one or more HTTP request. These HTTP requests often had subtly different CGI parameters, some only made sense when you were already on certain pages, and all of them, eventually, needed accessing from automated functional tests.

The tests didn't want to care about the underlying HTTP mechanism, or even the underlying HTML. The test just wanted to be able to say:

But clearly this is a trap. Because as we're all adults, actually, we want to check we're on the right page first, because it'll be super confusing otherwise if the former method left us on the wrong page, and we're trying to work out why we're not writing to the database.

And we also want to add a whole bunch of optional diagnostic output to help the poor developer trying to read the output on Jenkins from where everything stopped working.

And actually, the form has two buttons for historical reasons, one of which should be used for over 1,000 jigawtts, and one for under. So you need to add the form selection code in too.

Did I mention 14 different test scripts use this page and need to submit the jigawatt form, and the team that sits across the office for you are making noises about changing the form structure in the next iteration?

required_param - If your URL needs a trailing atom to complete it, set this to a true value. The user of the method will be required to provide an argument, and it'll be named (in diagnostic output) to the value you assigned it.

That means, in the value above, when you call the method, you also must provide an argument:

form_description - the human-readable description of the form you're submitting. You don't need to append the word 'form' to this.

assert_location - Argument to pass to assert_location()

form_* - one of the form resolvers listed below

Optional:

form_name - the name attribute of the target form. Passed to WWW::Mechanize's form_name() method. You can pass in a coderef here, which will get called just like transform_fields and should return a string. Instead of form_name you can use form_id or form_button to select forms by ID or button.

transform_fields - a code-ref. Will receive $self and the methods arguments, and expects you to return a hash-ref suitable for passing to WWW::Mechanize's set_fields method. This is a great place to put in default arguments, and also a great place to use note() to tell the test output reader what's going on.

form_button - argument to pass to WWW::Mechanize's submit_form value as button. Used for specifying which button to use to submit a form. This is a string of the button name. You can pass in a coderef here, which will get called just like transform_fields and should return a string.

link_description - what are you clicking? Human-readable, and used for diagnostics only

Optional:

assert_location - Argument to pass to assert_location

find_link - what we pass to WWW::Mechanize's find_link method to identify the link we want to click.

transform_fields - a code-ref. Will receive $self and the methods arguments, and expects you to return a hash-ref suitable for passing to WWW::Mechanize's find_link method. If you want to search for a link more specifically, and allow people to pass in, say, a shipment_id, this would be a good way of doing it.

This allows you to do whatever you like! :-) The method name output is shown, the location assertion is done if you specified one, and then your handler gets executed with the arguments. After this, note_status is called, and self returned.

You almost certainly DO NO NEED TO USE THIS. Instead, work out how to use create_form_method or simplify your method. That said:

Show the status of the HTTP call. This would be an excellent place to look for messages generated by your web-app, and to fatally die if unexpected errors have occured. However, this base class knows nothing about that, so we take the easy option and show the result of Mech's success().

Checks we're on the correct page before doing anything. The default implementation accepts a string or a regular expression, and matches it against whatever Mechanize thinks is the current unqualified URI. Non-matches call assert_location_failed.

That would be a good time to check if you have any obvious error statuses on the page you're on. assert_location_failed accepts the assertion and current URL, and the default implementation throws a simple fatal error.