Simple Mojo::Test with Form Example Needed

52 views
Skip to first unread message

iaw4

unread,
May 23, 2017, 1:12:21 PM5/23/17
to Mojolicious

Dear M experts:

Could someone please post a simple Mojo::Test example that
  • enters text "abc" (or adds "123" at the end of the text already in the text) into an id'ed input text field of some specific form (also identified by some id)
  • then presses the submit button (identified by some id) or a link
  • and checks that the result from this request contains a certain string
because my app has state information, I cannot simply test stateless URLs (which is the example in the tutorial).

Thanks.

regards,

/iaw

Stefan Adams

unread,
May 23, 2017, 1:24:09 PM5/23/17
to mojolicious
dotan's link to jberger's Galileo is a great Simple Mojo::Test example.

See L58.

Something to note.  When you use the $t Test::Mojo object, state will be retained.  I can't explain this as well as others on the list and I may not understand it well, but the $t object has access to the previous request and what not.  State is, AFAIK, carried thru.

iaw4

unread,
May 26, 2017, 7:23:09 PM5/26/17
to Mojolicious

dear M users / stefan : still stuck.  this probably has a simple answer.  here is my attempt at a simple form page and a test:

#!/usr/bin/env perl
use Test::More;
use Test::Mojo;

my $t = Test::Mojo->new;
$t
->get_ok('http://localhost:3000/')->status_is(200)->content_like(qr{<h1>TITLE: basic mojolicious cribsheet</h1>});
$t->get_ok('http:/
/localhost:3000/')->status_is(200)->content_unlike(qr{<h1>bad: basic mojolicious cribsheet</h1>});

my $teststring='
this-is-my-test-text';

# why is this not working?
$t->get_ok( '
http://localhost:3000/' => form => { from => 'http://localhost:3000/', getsometext => $teststring, id => 'formget' } )
   
->status_is(200)
   
->content_like( qr{<span>$teststring</span>}ms );

# or this?
$t->post_ok( 'http:/
/localhost:3000/' => form => { from => 'http://localhost:3000/', postsometext => $teststring, id => 'formpost' } )
   
->status_is(200)
   
->content_like( qr{Post Result: <span>$teststring</span>}ms );

done_testing
();


on this code

#!/usr/bin/env perl
use Mojolicious::Lite;

my $indexpage= sub {
 
my $c = shift;
  $c
->render(template => 'index' );
};

get '/' => $indexpage;
post
'/' => $indexpage;

app
->start;

__DATA__

@@ index.html.ep
% layout 'default';
% title 'basic mojolicious cribsheet';

<h1>TITLE: <%= title %></h1>

 
<h2> FORM GET </h2>

  <form action='/' method='
GET' id="formget">
    <input id="getsometext" name="sometext" value="this was get" />
    <input id="getsubmit" type="submit">
  </form>

  <p>Get Result: <span><%= ($self->req->query_params->param("sometext")) || "no text yet" %></span></p>

  <h2> FORM POST </h2>

  <form action='
/?ab=12' method='POST' id="formpost">
    <input id="postsometext" name="somepost" value="this was post" />
    <input id="postsubmit" type="submit">
  </form>

  <p>Post Result: <span><%= ($self->req->params->param("somepost")) || "no text yet" %></span> .<br />
   Query Params: <span><%= ($self->req->query_params->param("ab")) || "ab not seen" %></span></p>


@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
  </head>


  <body style="margin:3em; background-color:beige">
    <%= content %>
  </body>
</html>



sri

unread,
May 27, 2017, 11:03:54 AM5/27/17
to Mojolicious
Could someone please post a simple Mojo::Test example that
  • enters text "abc" (or adds "123" at the end of the text already in the text) into an id'ed input text field of some specific form (also identified by some id)
  • then presses the submit button (identified by some id) or a link
  • and checks that the result from this request contains a certain string
We've been meaning to automate this functionality. Something like a Mojo::DOM method to extract default form values (my $form_default_hash = $dom->at('#my-form')->val),
and a Test::Mojo method to tie it all together ($t->get_ok('/form.html')->submit_ok('#my-form' => {some => 'extra_values', to => 'be merged'})->status_is(200)).

But so far nobody could come up with the correct semantics, stuff like forms with multiple submit buttons makes things complicated.

--
sebastian 

Daniel Mantovani

unread,
May 27, 2017, 4:07:21 PM5/27/17
to Mojolicious
Hi,

Here:
 
  <p>Get Result: <span><%= ($self->req->query_params->param("sometext")) || "no text yet" %></span></p>

you probably meant "getsometext" instead of "sometext", like:

 <p>Get Result: <span><%= ($self->req->query_params->param("getsometext")) || "no text yet" %></span></p>

also as a suggestion in Mojo inside a template you can use the param default helper and say:

 <p>Get Result: <span><%= (param "getsometext") || "no text yet" %></span></p>

(just another way)

And same for the postsometext param

After those changes the tests pass.

By the way, I had to run the lite app with morbo (in default port 3000), and from another terminal the Mojo::Test script, because I see you included 'http://localhost:3000/...' in all your requests.

 But you don't actually have to do that, Mojo supports a simpler way to run tests, as explained here:


BR,
Daniel

iaw4

unread,
May 27, 2017, 7:30:14 PM5/27/17
to Mojolicious

thank you, daniel.  this was somewhat embarrassing.   I had used many different .t before, and as I whittled both the app and the .t further and further, eventually what I had attributed to a bad .t had become a bad app.

thank you for seeing it.  and thank you for the testing hint on not needing localhost.

a minimal working example of a use or feature always helps, so let me just post the working code for future users.

#!/usr/bin/env perl
use Test::More;
use Test::Mojo;

use FindBin;
require "$FindBin::Bin/quicksheet";


my $t = Test::Mojo->new;

$t
->get_ok('/')->status_is(200)->content_like(qr{<h1>TITLE: basic mojolicious cribsheet</h1>});
$t->get_ok('/')->status_is(200)->content_unlike(qr{<h1>bad: basic mojolicious cribsheet</h1>});

my $teststringG='
this-is-my-Gtest-text';
$t->get_ok( '
/' => form => { from => '/', someget => $teststringG, id => 'formget' } )
    ->status_is(200)
    ->content_like( qr{<span>$teststringG</span>}ms );

my $teststringP='
this-is-my-Ptest-text';
$t->post_ok( '
/' => form => { from => '/', somepost => $teststringP, id => 'formpost' } )
    ->status_is(200)
    ->content_like( qr{Post Result: <span>$teststringP</span>}ms );

done_testing();


on

#!/usr/bin/env perl
use Mojolicious::Lite;

my $indexpage= sub {
 
my $c = shift;
  $c
->render(template => 'index' );
};

get '/' => $indexpage;
post
'/' => $indexpage;

app
->start;

__DATA__

@@ index.html.ep

% layout 'default';
% title 'basic mojolicious cribsheet';

<h1>TITLE: <%= title %></h1>

 
<h2> FORM GET </h2>

  <form action='/' method='
GET
' id="formget">
    <input id="getidsometext" name="someget" value="this was get" />

    <input id="getsubmit" type="submit">
  </form>

  <p>Get Result: <span><%= ($self->req->query_params->param("someget")) || "no text yet" %></span></p>



  <h2> FORM POST </h2>

  <form action='
/?ab=12' method='POST' id="formpost">
    <input id="postidsometext" name="somepost" value="this was post" />

    <input id="postsubmit" type="submit">
  </form>

  <p>Post Result: <span><%= ($self->req->params->param("somepost")) || "no text yet" %></span> .<br />
   Query Params: <span><%= ($self->req->query_params->param("ab")) || "ab not seen" %></span></p>

@@ layouts/default.html.ep
<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
  </head>


  <body style="margin:3em; background-color:beige">
    <%= content %>
  </body>
</html>


and the test passes. 


Reply all
Reply to author
Forward
0 new messages