Skip to content

Instantly share code, notes, and snippets.

@nicomen
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicomen/dccb9c4843b649b91c0d to your computer and use it in GitHub Desktop.
Save nicomen/dccb9c4843b649b91c0d to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use Test::More;
use Test::Mojo;
use Mojo::Util;
Mojo::Util::monkey_patch 'Test::Mojo', submit_form_ok => sub {
my ($t, $form, $override_fields, $options) = @_;
my %fields = ();
$form->find('button, input, select')
->map(sub {
my $e = shift;
my $key = $e->attr('name') || $e->attr('id');
$fields{$key} = $e->val->first;
});
%fields = (%fields, %{$override_fields});
my $method = uc ($form->attr('method') || $options->{method} || 'GET');
my $action = $form->attr('action') || $options->{action};
$action = Mojo::URL->new($action)->to_abs(Mojo::URL->new($t->tx->req->url));
my $headers = $options->{headers} || {};
my $tx = $t->ua->build_tx($method => $action => $headers => form => \%fields);
return $t->request_ok($tx);
};
my $t = Test::Mojo->new();
$t->get_ok('http://www.abcnyheter.no/user');
my $form = $t->tx->res->dom->find('form#user-login')->first;
$t->submit_form_ok($form, { name => 'fry', pass => 's3cr3t' });
done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment