Testing Your Frontend

     

Once you've fully tested the backend of your web application, you should test its frontend as well. Assume that you have expanded the Scorekeeper application (see "Testing Your Backend," earlier in this chapter) to contain interfaces for adding players and games . The steps for testing by hand are straightforward: open the application in the browser, type things into the form fields, click Submit, and check the contents of the resulting page. Then repeat. Unfortunately, as the application grows, so does the punch list of manual regression tests you need to perform to make sure everything works.

This lab shows how to automate the testing of web applications using Test::WWW::Mechanize , a subclass of WWW::Mechanize that works well for test programs.

How do I do that?

This lab tests the frontend of the CPAN Search site (http://search.cpan.org/). This web site has one primary form that allows users to find modules as well as some navigational links to take visitors to the most-frequented parts of the site.


Note: You could use similar code to test the frontend of the Scorekeeper application .

When constructing tests for web applications, always start by listing the behavior you expect from the application. How do you expect the CPAN Search Site to work?

  • I should be able to retrieve the CPAN Search Site home page successfully.

  • If I search the modules for "frobnicate", there shouldn't be any results.

  • If I search the modules for "test", there should be many results.

  • Once I've searched for "test", all of the links on the resulting page should work.

These assertions sound pretty solid. Save the following file as mech.t :

 #!perl          use strict;     use warnings;          use Test::More tests => 6;     use Test::WWW::Mechanize;          my $mech = Test::WWW::Mechanize->new(  );          $mech->get_ok( 'http://search.cpan.org/' );          $mech->title_is( 'search.cpan.org: The CPAN Search Site' );          $mech->form_name( 'f' );     $mech->field( 'query', 'frobnicate' );     $mech->select( 'mode', 'module'     );     $mech->submit(  );          $mech->content_contains( 'No matches' );          $mech->back(  );          $mech->field( 'query', 'test' );     $mech->submit(  );          $mech->content_like( qr/ Results .+ Found /sx );     $mech->content_lacks( 'No matches' );     $mech->page_links_ok(  ); 

Running mech.t should result in six successful tests. The last test may take a bit longer than the first five, depending on the speed of your network connection.

What just happened ?

After use ing the Test::WWW::Mechanize module, the test file creates an object of that class, $mech . The $mech object pretends to be a real human that fills in forms and clicks on links and buttons . It even keeps a history, meaning that the back( ) method works just like the Back button in your favorite browser.


Note: WWW::Mechanize provides other methods to fill out and submit a form in one statement .

The first step is to instruct $mech to retrieve the CPAN Search home page, which contains a single form named simply f . The get_ok( ) method not only does this, but also reports a successful test if it fetched the web page without an error.

Next , $mech checks the title of the fetch page. title_is( ) ensures that the title is exactly the string specified. Test::WWW::Mechanize also provides alternative title_like( ) and title_unlike( ) methods that check whether the title matches or does not match a given regular expression.

Many of the other methods on Test::WWW::Mechanize objects have is( ) / isnt( ) or like( ) / unlike( ) variants. See the Test::WWW::Mechanize module documentation for details.

The test selects the form named f as the form for which to specify input values. $mech then simulates filling out the text field named query and selecting the item from the pop-up menu named mode with the value of module . The submit( ) method then "clicks" the Submit button for the form, and the $mech object happily retrieves the resulting page.

At the time of this writing, there aren't any modules with names containing the word "frobnicate," thus the search results should be empty. $mech ensures that the resulting page contains the phrase "No matches" by using the content_contains( ) method.

$mech next clicks its virtual Back button and jumps back to the page containing the original web form. Because the object has already selected the correct pop-up menu item in the form, $mech only needs to change the text field to contain "test." It then submits the form again.

This time, there are lots of modules with the word "test" in their names. The test checks that the results page does not contain the phrase "No matches" as seen earlier.

Test::WWW::Mechanize provides a convenience function, page_links_ok( ) , to test that it can follow all of the links on the current page successfully. Because there are more than 50 links on the results page, and Mechanize retrieves each one, this takes a little while. If all of the links are indeed valid, page_links_ok( ) produces a sixth successful test.



Perl Testing. A Developer's Notebook
Perl Testing: A Developers Notebook
ISBN: 0596100922
EAN: 2147483647
Year: 2003
Pages: 107

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net