Test::...::Mojo(3pm)

πŸ’– NAME πŸ’–

Test::WWW::Mechanize::Mojo β€” it's Test::WWW::Mechanize, but make it Mojo / Mojolicious. Bestie, we're testing web apps in-process and it's so much faster than the alternative. ✨


πŸŽ€ VERSION πŸŽ€

version 0.0.21


🌸 SYNOPSIS 🌸

okay so picture this: you're in a t/*.t test script and you want to test a whole web app without ever starting a server. iconic behaviour, honestly.

# We're in a t/*.t test script...
use Test::WWW::Mechanize::Mojo;

my $tester = Test::Mojo->new();

# To test a Mojo application
my $mech = Test::WWW::Mechanize::Mojo->new(tester => $tester);

$mech->get_ok("/"); # no hostname needed
is($mech->ct, "text/html");
$mech->title_is("Root", "On the root page");
$mech->content_contains("This is the root page", "Correct content");
$mech->follow_link_ok({text => 'Hello'}, "Click on Hello");
# ... and all other Test::WWW::Mechanize methods

# White label site testing
$mech->host("foo.com");
$mech->get_ok("/");
not me getting emotional over $mech->get_ok("/") needing literally no hostname πŸ’…

πŸ¦‹ DESCRIPTION πŸ¦‹

Mojo is an MVC Web Application Framework. Test::WWW::Mechanize is a subclass of WWW::Mechanize that incorporates features for web application testing. The Test::WWW::Mechanize::Mojo module meshes the two to allow easy testing of Mojo applications without needing to starting up a web server. 😌✨

Testing web applications has always been a bit tricky, normally requiring starting a web server for your application and making real HTTP requests to it. This module allows you to test Mojo web applications but does not require a server or issue HTTP requests. Instead, it passes the HTTP request object directly to Mojo. Thus you do not need to use a real hostname: http://localhost/ will do. However, this is optional. The following two lines of code do exactly the same thing:

$mech->get_ok('/action');
$mech->get_ok('http://localhost/action');

Links which do not begin with / or are not for localhost can be handled as normal Web requests β€” this is handy if you have an external single sign-on system. You must set allow_external to true for this:

$mech->allow_external(1);

You can also test a remote server by setting the environment variable MOJO_SERVER; for example:

$ MOJO_SERVER=http://example.com/myapp prove -l t

will run the same tests on the application running at http://example.com/myapp regardless of whether or not you specify http:://localhost for Test::WWW::Mechanize::Mojo. πŸ”₯

Furthermore, if you set MOJO_SERVER, the server will be regarded as a remote server even if your links point to localhost. Thus, you can use Test::WWW::Mechanize::Mojo to test your live webserver running on your local machine, if you need to test aspects of your deployment environment (for example, configuration options in an http.conf file) instead of just the Mojo request handling. ngl that's kind of a serve πŸ’š

This makes testing fast and easy. Test::WWW::Mechanize provides functions for common web testing scenarios. For example:

$mech->get_ok( $page );
$mech->title_is( "Invoice Status", "Make sure we're on the invoice page" );
$mech->content_contains( "Andy Lester", "My name somewhere" );
$mech->content_like( qr/(cpan|perl)\.org/, "Link to perl.org or CPAN" );

This module supports cookies automatically. πŸͺ (cookies!! the good kind)

To use this module you must pass it the name of the application. See the SYNOPSIS above.

Note that Mojo has a special developing feature: the debug screen. By default this module will treat responses which are the debug screen as failures. If you actually want to test debug screens, please use:

$mmech->{catalyst_debug} = 1;

An alternative to this module is Test::Mojo.


πŸ‘‘ CONSTRUCTOR πŸ‘‘

πŸ’« new πŸ’«

Behaves like, and calls, WWW::Mechanize's new method. Any params passed in get passed to WWW::Mechanize's constructor. Note that we need to pass the name of the Catalyst application to the use:

use Test::WWW::Mechanize::Catalyst;

use Test::Mojo;

my $tester = Test::Mojo->new();

my $mech = Test::WWW::Mechanize::Catalyst->new(tester => $tester);

In addition, one can specify a 'tester' argument as the Test::Mojo instance.


🌈 METHODS 🌈

  • allow_external β€” Links which do not begin with / or are not for localhost can be handled as normal Web requests β€” this is handy if you have an external single sign-on system. You must set allow_external to true for this:
$m->allow_external(1);
  • mojo_app β€” The name of the Mojo app which we are testing against. Read-only, bestie, don't try it.
  • has_mojo_app β€” For internal use. πŸ”’
  • tester β€” For internal use. πŸ”’
  • host β€” The host value to set the Host: HTTP header to, if none is present already in the request. If not set (default) then Catalyst::Test will set this to localhost:80.
  • clear_host β€” Unset the host attribute. 🧹
  • has_host β€” Do we have a value set for the host attribute? πŸ€”
  • $mech->get_ok($url, [ \%LWP_options ,] $desc) β€” A wrapper around WWW::Mechanize's get(), with similar options, except the second argument needs to be a hash reference, not a hash. Returns true or false.
  • $mech->title_is( $str [, $desc ] ) β€” Tells if the title of the page is the given string.
$mech->title_is( "Invoice Summary" );
  • $mech->title_like( $regex [, $desc ] ) β€” Tells if the title of the page matches the given regex.
$mech->title_like( qr/Invoices for (.+)/
  • $mech->title_unlike( $regex [, $desc ] ) β€” Tells if the title of the page does NOT match the given regex.
$mech->title_unlike( qr/Invoices for (.+)/
  • $mech->content_is( $str [, $desc ] ) β€” Tells if the content of the page matches the given string. πŸ’―
  • $mech->content_contains( $str [, $desc ] ) β€” Tells if the content of the page contains $str.
  • $mech->content_lacks( $str [, $desc ] ) β€” Tells if the content of the page lacks $str.
  • $mech->content_like( $regex [, $desc ] ) β€” Tells if the content of the page matches $regex.
  • $mech->content_unlike( $regex [, $desc ] ) β€” Tells if the content of the page does NOT match $regex.
  • $mech->page_links_ok( [ $desc ] ) β€” Follow all links on the current page and test for HTTP status 200.
$mech->page_links_ok('Check all links');
  • $mech->page_links_content_like( $regex,[ $desc ] ) β€” Follow all links on the current page and test their contents for $regex.
$mech->page_links_content_like( qr/foo/,
  'Check all links contain "foo"' );
  • $mech->page_links_content_unlike( $regex,[ $desc ] ) β€” Follow all links on the current page and test their contents do not contain the specified regex.
$mech->page_links_content_unlike(qr/Restricted/,
  'Check all links do not contain Restricted');
  • $mech->links_ok( $links [, $desc ] ) β€” Check the current page for specified links and test for HTTP status 200. The links may be specified as a reference to an array containing WWW::Mechanize::Link objects, an array of URLs, or a scalar URL name.
my @links = $mech->find_all_links( url_regex => qr/cnn\.com$/ );
$mech->links_ok( \@links, 'Check all links for cnn.com' );

my @links = qw( index.html search.html about.html );
$mech->links_ok( \@links, 'Check main links' );

$mech->links_ok( 'index.html', 'Check link to index' );

🫢 SUPPORT 🫢

Okay bestie, so like β€” you got questions? You got vibes that need clarifying? There's a whole little support ecosystem for this module and it's lowkey adorable. As always, on top of these sites, pls just use your fave search engine to go find more resources, iykyk. ✨

🌐 Websites

These are the sites that have more info about this module and may genuinely be of help to you:

  • MetaCPAN β€” a modern, open-source CPAN search engine, super useful for viewing POD in HTML format. It's giving glow up for documentation.
https://metacpan.org/release/Test-WWW-Mechanize-Mojo
  • RT: CPAN's Bug Tracker β€” RT (that's Request Tracker) is the default bug/issue tracking system for CPAN. The OG.
https://rt.cpan.org/Public/Dist/Display.html?Name=Test-WWW-Mechanize-Mojo
  • CPANTS β€” the website that analyzes the Kwalitee (that's code metrics, yes with a K, iconic) of a distribution.
http://cpants.cpanauthors.org/dist/Test-WWW-Mechanize-Mojo
  • CPAN Testers β€” a whole network of smoke testers who run automated tests on uploaded CPAN distributions. They are the moment. πŸ”₯
http://www.cpantesters.org/distro/T/Test-WWW-Mechanize-Mojo
  • CPAN Testers Matrix β€” gives you a visual overview of the test results for a distribution across various Perls and platforms. Chart girlies rise.
http://matrix.cpantesters.org/?dist=Test-WWW-Mechanize-Mojo
  • CPAN Testers Dependencies β€” shows a chart of the test results of all the dependencies for a distribution. Yes, the whole extended fam.
http://deps.cpantesters.org/?module=Test::WWW::Mechanize::Mojo

🐞 Bugs / Feature Requests

Found a bug? Got a feature request? Please report any bugs or feature requests by email to bug-test-www-mechanize-mojo at rt.cpan.org, or through the web interface over at:

https://rt.cpan.org/Public/Bug/Report.html?Queue=Test-WWW-Mechanize-Mojo

And the best part?? You will be automatically notified of any progress on the request by the system. No refreshing, no stalking the thread, it just comes to you. Periodt. πŸ’…

πŸ’» Source Code

The code is open to the world and available for you to hack on β€” please feel free to browse it and play with it, or whatever. No gatekeeping here, bestie. And if you want to contribute patches, pls send a diff or just prod the author to pull from the repository. :)

https://github.com/shlomif/perl-test-www-mechanize-mojo
git clone git://github.com/shlomif/perl-test-www-mechanize-mojo.git

πŸ‘‘ AUTHOR πŸ‘‘

Shlomi Fish <shlomif@cpan.org>


πŸ› BUGS πŸ›

Please report any bugs or feature requests on the bugtracker website:

https://github.com/shlomif/perl-test-www-mechanize-mojo/issues
And when you're submitting a bug or a request, pls pls pls include a test-file, or a patch to an existing test-file, that illustrates the bug or the desired feature. Like, help us help you, fr fr. πŸ™ˆ

This software is copyright (c) 2005 by Leon Brocard.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.


And that's a wrap on Test::...::Mojo(3pm)! This was generated from perl v5.30.3 and the page is dated 2020-10-11 β€” so yeah, it's been living its little life since way back, and honestly? Still slaying. Go write some tests, bestie, you got this. πŸ¦‹πŸ’–βœ¨

πŸ“¦ where this page comes from