NAME

Test::Mojo::Role::SubmitForm - Test::Mojo role that allows to submit forms

SYNOPSIS

use Test::More;
use Test::Mojo::WithRoles 'SubmitForm';
my $t = Test::Mojo::WithRoles->new('MyApp');

# Submit a form without clicking any buttons: pass selector to the form
$t->get_ok('/')->status_is(200)->click_ok('form#one')->status_is(200);

# Click a particular button
$t->get_ok('/')->status_is(200)->click_ok('[type=submit]')->status_is(200);

# Submit a form while overriding form data
$t->get_ok('/')->status_is(200)
    ->click_ok('form#one', {
        input1        => '42',
        select1       => [ 1..3 ],
        other_select  => sub { my $r = shift; [ @$r, 42 ] },
        another_input => sub { shift . 'offix'}
    })->status_is(200);

done_testing;

DESCRIPTION

A Test::Mojo role that allows you submit forms, optionally overriding any of the values already present

METHODS

You have all the methods provided by Test::Mojo, plus these:

click_ok

$t->click_ok('form');
$t->click_ok('#button');

$t->click_ok( $dom->at('form') );

$t->click_ok('#button', {
    input1        => '42',
    select1       => [ 1..3 ],
    other_select  => sub { my $r = shift; [ @$r, 42 ] },
    another_input => sub { shift . 'offix'}
})

First parameter specifies a CSS selector matching or Mojo::DOM object containing <form> you want to submit or a particular <button>, <input type="submit">, or <input type="image"> (contained in a <form>) you want to click.

Specifying a second parameter allows you to override the form control values: the keys are name=""s of controls to override and values can be either plain scalars (use arrayrefs for multiple values) or subrefs. Subrefs will be evaluated and their first @_ element will be the current value of the form control.

DEBUGGING / ENV VARS

To see what form data is being submitted, set MOJO_SUBMITFORM_DEBUG environmental variable to a true value:

MOJO_SUBMITFORM_DEBUG=1 prove -vlr t/02-app.t

Sample output:

ok 36 - GET /
ok 37 - 200 OK

########## SUBMITTING FORM ##########
{
  "\$\"bar" => 5,
  "a" => 42,
  "b" => "B",
  "e" => "Zoffix",
  "mult_b" => [
    "C",
    "D",
    "E"
  ],
  "\x{a9}\x{263a}\x{2665}" => 55
}
##########    END FORM     ##########

[Wed Sep 23 17:34:00 2015] [debug] POST "/test"

CAVEATS

Note that you cannot override the value of buttons you're clicking on. In those cases, simply "click" the form itself, while passing the new values for buttons.

SEE ALSO

Test::Mojo, Mojo::DOM

REPOSITORY

Fork this module on GitHub: https://github.com/zoffixznet/Test-Mojo-Role-SubmitForm

BUGS

To report bugs or request features, please use https://github.com/zoffixznet/Test-Mojo-Role-SubmitForm/issues

If you can't access GitHub, you can email your request to bug-test-mojo-role-SubmitForm at rt.cpan.org

AUTHOR

ZOFFIX ZOFFIX

CONTRIBUTORS

PLICEASE PLICEASE

LICENSE

You can use and distribute this module under the same terms as Perl itself. See the LICENSE file included in this distribution for complete details.