Hack82.Test Your Application with Simulated Users


Hack 82. Test Your Application with Simulated Users

Use the Internet Explorer automation interface to test your application through the UI.

You can test the back end of an application by using unit tests for the database and business logic code. You can test a portion of the frontend of your application by using robots [Hack #83] that request pages from the server and submit data. But how do you actually test what users would do? How do you simulate the buttons pushed and the boxes filled in by a typical user?

Particularly with JavaScript-heavy applications, you need something that will actually click on buttons to ensure that your application does what it should.

On Windows, you can use Internet Explorer COM objects to tell the IE browser to navigate to your site and even to hit the appropriate buttons. Figure 8-2 illustrates the relationships among the different PHP files used in this hack. test.php and print.php are standard PHP web pages. The testagent.php script is run on the command line and drives the browser to visit these pages, fill in the forms, and check the results.

Figure 8-2. The test agent driving the browser


8.5.1. The Code

Save the code in Example 8-7 as test.php. This simple web page is used for testing the agent.

Example 8-7. The first page of the test code
 <html> <head> <title>Automated test agent test page</title> <head> <body> <form  action="print.php"> <table cellpadding="2"><tr> <td>First:</td> <td><input  name="first" type="text"></td> </tr><tr> <td>Last:</td> <td><input  name="last" type="text"></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" /> </td> </tr> </table> </form> </body> </html> 

Save the code in Example 8-8 as print.php. This is just a form receiver for Example 8-7.

Example 8-8. The second page of the test code
 <html> <head> <title>Automated test agent print page</title> </head> <body> You entered:<br/> First: <span ><?php echo( $_GET['first'] ); ?></span> <br/> Last: <span ><?php echo( $_GET['last'] ); ?></span> <br/> </body> </html> 

Save the code in Example 8-9 as testagent.php.

Example 8-9. The Internet Explorer automation test script
 <?php function delay( $ie, $amount ) {   for( $c = 0; $c < ( $amount / 100 ); $c++ )     com_message_pump( 100 ); } function test_page( $ie, $page, $first, $last ) {   $ie->Navigate( $page );   delay( $ie, 2000 );   $fn = $ie->Document->getElementById( "first_name" );   $fn->Value = $first;   $ln = $ie->Document->getElementById( "last_name" );   $ln->Value = $last;   $inf = $ie->Document->getElementById( "inp_form" );   $inf->submit();   delay( $ie, 2000 );   $rfn = $ie->Document->getElementById( "res_first" );   $rfn = $rfn->innerHTML;   $rln = $ie->Document->getElementById( "res_last" );   $rln = $rln->innerHTML;   if( strcmp( $rfn, $first ) == 0 &&   strcmp( $rln, $last ) == 0 )   {     print "Test passed.\n"; return 0;   }   else   { print "Test failed.\n"; return -1;    }  } $ie = new COM("InternetExplorer.Application"); $ie->Visible = true; $result = test_page( $ie,   "http://localhost:1222/com/test.php",   "Charles",   "Herrington" ); $ie->Quit(); exit( $result ); ?> 

This test script primarily uses the Internet Explorer COM interface via the built-in COM class wrapper. The processing flow starts at the bottom, where the code creates a COM object of type InternetExplorer.Application. The script then runs the test_page( ) function to use Internet Explorer to test the page.

The test_page() function navigates IE to the target URL, then fills in the form and submits it. The function waits a little bit, allowing the return page to be loaded. Then, it inspects the <span> tags on the returned page to make sure they contain the right information. The script quits Internet Explorer and sets the exit code to 0 or -1, depending on the result of the test. Finally, a short message about the status of the test is printed out.

These tests are obviously very specific to a particular site and even a particular page. You won't be able to code generic tests, but this sort of user automation testing is still invaluable.


8.5.2. Running the Hack

Install the test.php and print.php files in your web server. Alter the testagent.php file to use the correct URL for your test page. Then use this command:

 c:\testagent\> php testagent.php Test passed. 

As part of the test, the first thing you should see is the login page (shown in Figure 8-3).

Figure 8-3. The test data-entry page


Then the user agent will fill in the form and click on the Submit Query button. This will take the agent to the next pageFigure 8-4that simply parrots back what was entered.

Figure 8-4. The test result page


This print.php has span tags that bracket the first and last names that were entered. The user agent uses the innerHTML property of these span tags to ensure that the returned values match those that were input into the form.

This is exactly what commercial products such as SilkTest (http://segue.com/) and TestComplete (http://automatedqa.com/) provide for testing web pages. Those products cost a lot of money and this code is, well, free.


8.5.3. See Also

  • "Test Your Application with Robots" [Hack #83]

  • "Spider Your Site" [Hack #84]



PHP Hacks
PHP Hacks: Tips & Tools For Creating Dynamic Websites
ISBN: 0596101392
EAN: 2147483647
Year: 2006
Pages: 163

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