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('#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 a <form> you want to submit or a particular <button>, <input type="submit">, or <input type="image"> 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 55 - GET /
ok 56 - 200 OK

########## SUBMITTING FORM ##########
$FORM1 = {
  'desc-1' => 'Description 1',
  'desc-2' => 'Description 2',
  'month' => [
    '8'
  ],
  'num-1' => '001-001',
  'num-2' => '001-002',
  'price-1' => 43,
  'price-2' => 44,
  'qty-1' => '11',
  'qty-2' => '12',
  'um-1' => 'box',
  'um-2' => 'box',
  'year' => [
    '2015'
  ]
};
##########    END FORM     ##########

[Tue Sep 22 10:03:02 2015] [debug] POST "/save"

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

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.