xeronuro
11/17/2012 - 6:50 PM

Example of Test::Mojo used to unit test.

Example of Test::Mojo used to unit test.

#!/usr/bin/env perl

use strict;
use warnings;
use Test::More tests => 6;
use Test::Mojo;

my $url = 'http://localhost/~rhaen/index.html';

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

# Easy - get the title string
$t->get_ok($url)
    ->text_is('html head title'
        => 'Hello', 'correct title');

# Still easy - get the <a> test
$t->get_ok($url)
    ->text_is('a[target]'
        => 'Hello page', 'correct text for a tag');

# Advanced - checking for attribute value
$t->get_ok($url)
    ->element_exists('a[href=Hello.html]', 'Yep, attribute value ok');
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
  <head>
    <title>Hello</title>
  </head>
  <body>
    <ul>
      <li>
        <a href="Goodbye.html">Goodbye page</a>
      </li>
      <li>
        <a href="Hello.html" target="_blank">Hello page</a>
      </li>
    </ul>  
  </body>
</html>