<!DOCTYPE html>
<html>
<head>
<title>Simple</title>
</head>
<body>
<p id="name">Leela</p>
</body>
</html>
ex/templates/simple.html.ep
use Mojolicious::Lite;
use Test::More;
use Test::Mojo;
any '/' => 'simple';
my $t = Test::Mojo->new;
$t->get_ok('/')->status_is(200)
->text_is('title' => 'Simple')
->text_is('body p#name' => 'Leela');
done_testing;
ex/simple.t
Selenium requires external running servers
<!DOCTYPE html>
<html>
<head><title>Dynamic</title></head>
<body>
<p id="name">Leela</p>
<script>
(function(){
document.getElementById('name').innerHTML = 'Bender';
})();
</script>
</body>
</html>
ex/templates/dynamic.html.ep
use Mojolicious::Lite; use Test::More;
use Test::Mojo::WithRoles 'Phantom';
any '/' => 'dynamic';
my $t = Test::Mojo::WithRoles->new;
$t->get_ok('/')->status_is(200)
->text_is('body p#name' => 'Leela');
$t->phantom_ok('/' => <<'JS');
var text = page.evaluate(function(){
return document.getElementById('name').innerHTML;
});
perl.is(text, 'Bender', 'name changed after loading');
JS
done_testing;
ex/dynamic.t
use Mojolicious::Lite;
any '/' => 'click';
app->start;
__DATA__
@@ click.html.ep
<script>
var current = 0, targets = ['Leela', 'Bender', 'Fry'];
function cycle() {
current = (current + 1) % targets.length;
return targets[current];
}
</script>
Marked For Annihilation:
<input type="button" id="marked"
value="Leela" onclick="this.value = cycle()">
ex/click.pl
use Mojo::Base -strict; use Test::More;
use Test::Mojo::WithRoles 'Phantom';
require 'ex/click.pl';
my $t = Test::Mojo::WithRoles->new;
$t->phantom_ok('/' => <<'JS');
function click_get() {
return page.evaluate(function() {
var marked = document.getElementById('marked');
marked.click(); return marked.value;
})
}
perl.is(click_get(), 'Bender', 'once');
perl.is(click_get(), 'Fry', 'twice');
perl.is(click_get(), 'Leela', 'three times a lady');
perl.is(click_get(), 'Bender', 'kiss my shine metal ...');
JS
done_testing;
ex/click.t
use Dancer;
use Path::Tiny;
my $html = path('ex/templates/dynamic.html.ep')->slurp;
get '/' => sub { return $html };
dance;
ex/dancer.pl
use Mojo::Base -strict; use Test::More;
use Test::Mojo::WithRoles 'PSGI', 'Phantom';
my $t = Test::Mojo::WithRoles->new('ex/dancer.pl');
$t->get_ok('/')->status_is(200)
->text_is('body p#name' => 'Leela');
$t->phantom_ok('/' => <<'JS');
var text = page.evaluate(function(){
return document.getElementById('name').innerHTML;
});
perl.is(text, 'Bender', 'name changed after loading');
JS
done_testing;
ex/dancer.t