=encoding utf8 =head1 NAME =begin original HTTP::WebTest::Cookbook - Recipes for typical web tests =end original HTTP::WebTest::Cookbook - ウェブページのテストのレシピ =head1 SYNOPSIS =begin original Not Applicable =end original 応用なし =head1 DESCRIPTION =begin original This document contains some examples of L usage. =end original このドキュメントには L の使い方の いくつかの例を載せてあります. =begin original Unless otherwise is stated all examples are either runnable programs (see L) or runnable wtscript files (see L). =end original 特に記述がない例は実行可能なプログラム( L 参照) もしくは実行可能な wtscript ファイル(L参照)です. =head1 BASICS (基本) =head2 Test Static Web Pages (静的なウェブページのテスト) =begin original This wtscript file tests static pages on the author's website: =end original 以下の wtscript ファイルは著者(訳注:原文の著者)のウェブサイト上の 静的なページをテストします: test_name = First page url = http://martynov.org/ text_require = ( Ilya Martynov's Web Site ) end_test test_name = Mail-CheckUser page url = http://martynov.org/checkuser text_require = ( Mail-CheckUser Download ) regex_require = ( Mail-CheckUser-[\d\.]+\.tar\.gz ) end_test =begin original The same tests in the form of a Perl script: =end original 同じテストの Perl スクリプト版です: use HTTP::WebTest; my $webtest = new HTTP::WebTest; $webtest->run_tests( [ { test_name => 'First page', url => 'http://martynov.org/', text_require => [ "Ilya Martynov's Web Site" ] }, { test_name => 'Mail-CheckUser page', url => 'http://martynov.org/checkuser', text_require => [ 'Mail-CheckUser', 'Download' ], regex_require => [ 'Mail-CheckUser-[\d\.]+\.tar\.gz' ] } ]); =head2 Test a Login Form (ログインフォームのテスト) =begin original This wtscript file tests the login form at http://fsck.com/rt2/: =end original 以下の wtscript ファイルは http://fsck.com/rt2/ のログインフォームを テストします: test_name = Login page url = http://fsck.com/rt2/ text_require = ( Login Username: Password:) end_test test_name = Submit wrong username & password url = http://fsck.com/rt2/ params = ( user => unknownUser pass => somePassword ) text_require = ( Error Your username or password is incorrect ) end_test test_name = Submit correct username & password url = http://fsck.com/rt2/ params = ( user => guest pass => guest ) regex_require = ( Signed in as.*?guest.*?\. ) end_test =head2 Using link and button names instead of URLs in tests (テスト中に URL ではなくリンクやボタンの名前を使う) =begin original This wtscript file tests static pages on the author's website. It is similar to the example in section L but it uses the test parameter C to specify the link to be followed on the next test request instead of a hardcoded URL: =end original =begin original この wtscript ファイルでは著者のウェブサイトの静的なページをテストします. これは L<< |/静的なウェブページのテスト >> と 似ていますが, テストパラメータ C に次に要求されるテストを URL のハードコードではなくリンクを指定することで行っています: =end original # load HTTP::WebTest::Plugin::Click module which provides test # parameter 'click_link' plugins = ( ::Click ) test_name = First page url = http://martynov.org/ text_require = ( Ilya Martynov's Web Site ) end_test test_name = Mail-CheckUser page click_link = Mail-CheckUser text_require = ( Mail-CheckUser Download ) regex_require = ( Mail-CheckUser-[\d\.]+\.tar\.gz ) end_test =begin original This wtscript file tests the login form at http://fsck.com/rt2/. It is similar to the example in section L but avoids using a hardcoded URL for the page the form should be submitted to by using the test parameter C: =end original この wtscript ファイルは http://fsck.com/rt2/ にあるログインフォームを テストします. これは L<< |/ログインフォームのテスト >> と似ていますが, URL のハードコードを避けてテストパラメータ C を使ってフォームを送信しています: # load HTTP::WebTest::Plugin::Click module which provides test # parameter 'click_button' plugins = ( ::Click ) test_name = Login page url = http://fsck.com/rt2/ text_require = ( Login Username: Password:) end_test test_name = Submit correct username & password click_button = Login params = ( user => guest pass => guest ) regex_require = ( Signed in as.*?guest.*?\. ) end_test =head1 ADVANCED (上級編) =head2 Test::Harness Compatible Output (Test::Harness 互換の出力) =begin original This Perl script reads a test specification from file C and generates L compatible output: =end original 以下の Perl スクリプトは C ファイルからテスト手順を読み込み L 互換の出力を生成します: use Test::More qw(no_plan); use HTTP::WebTest; my $webtest = new HTTP::WebTest; $webtest->run_wtscript('test.wt', { default_report => 'no', plugins => [ '::HarnessReport' ] }); =begin original This script uses reporting plugin L which internally uses L module to generate L compatible output. It should be compatible with other testing libraries built using L (like L or L) so you can freely intermix them in one test script. =end original このスクリプトではレポートプラグイン L を使っています. これは L 互換の出力を 生成するために内部で L を使っています. これは L を使って構築された他のテスト ライブラリ(L や L 等)と互換であるため, 1つのテストスクリプトの中にこれらを自由に取り混ぜることができます. =head2 User-Defined Tests (ユーザ定義のテスト) =begin original It is possible to define new tests without writing new plugin module. This is a fragment of a wtscript file that checks if a new record has been inserted into a database as a result of the Add Record test. =end original 新しいプラグインモジュールを書かなくとも新しいテストを定義する ことができます. 以下の wtscript ファイルは Add Record テストの結果 新しいレコードがデータベースに追加されたことを調べる抜粋です. # load HTTP::WebTest::Plugin::Hooks module which provides test # parameters 'on_start', 'on_finish' and 'on_response' plugins = ( ::Hooks ) on_start = { # initialize a database handle used later in the tests require DBI; $dbh = DBI->connect('dbi:mysql:test', 'login', 'password'); } on_finish = { # disconnect from the database $dbh->disconnect; } .... test_name = Add Record # request to this URL with parameter 'name' adds new record url = http://some.server/add-record params = ( name => 'John' ) # define check on_response = { my $has_record = $dbh->selectrow_array( 'SELECT COUNT(*) FROM USERS ' . 'WHERE NAME = ?', undef, 'John' ); # return result of check with a comment [ $has_record > 0 ? 'yes' : 'no', 'Have got John' ]; } end_test =head2 Dynamic Tests (動的なテスト) =begin original Sometimes you want to feed the results of a previous test into the next test. In this example, C creates a database record, emits HTML containing the new record ID, and C deletes the database record using the record ID from C. =end original あるテストの結果を次のテストに反映したいこともあるでしょう. 次の例では C がデータベースにレコードを生成て その新しいレコードのIDを含んだ HTML を発行し, C にておいて C のレコードIDを使って データベースからレコードを削除します. # load HTTP::WebTest::Plugin::Hooks module which provides test # parameter on_response plugins = ( ::Hooks ) .... test_name = Add Record # request to this URL with parameter 'name' adds new record url = http://some.server/add-record params = ( name => 'John' ) # get ID from a page on_response = { # get webtest object my $webtest = shift; # find ID in the returned page ($ID) = $webtest->current_response->content =~ /ID=(\d+)/; # because no checks are defined a reference on empty array # must be returned []; } end_test .... test_name = Delete Record # request to this URL with parameter 'id' deletes record url = http://some.server/delete-record params = ( id => "$ID" ) end_test =head1 COPYRIGHT Copyright (c) 2001-2003 Ilya Martynov. All rights reserved. =begin original This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =end original このプログラムはフリーソフトウェアです. このプログラムは Perl 自身と同じ条件下で再配布・改変可能です. =head1 SEE ALSO L L L L L L