Top / Programming / Ruby / WebUnit / WebUnitのサンプル

開発環境

  1. VineLinux 2.6rc3

httpの設定

CGIプログラムは、http://localhost/ruby/sample.rb でアクセスできるように設定しました。
/etc/httpd/conf/httpd.conf の設定はこんな感じです。

Alias /ruby/ /home/yamamoto/ruby/
<Directory /home/yamamoto/ruby>
Options +ExecCGI
AddType text/html rb
AddHandler cgi-script .rb
</Directory>

プログラムのソースコード

上記の3つのファイルを同じディレクトリに起きます。
sample.rb のアクセス権は、755にします。
http://localhost/ruby/sample.rb にアクセスして、ページが表示されたら成功です。

すべてのファイルを一括してダウンロードできるように、圧縮したファイルを用意しました。erbも同封しています。(2004年01月24日)
webunit_sample.lzh(erbを含むすべてのファイル)8.78KB

WebUnitサンプルプログラムのソースコード解説

ENV['URLBASE'] = 'http://localhost/ruby/'

環境変数URLBASEを設定します。
ここで指定する意味はないので、SampleTest#setup内で@urlにURLを指定していいと思います。

assert_title

ページのタイトルのテスト

  def test_title
response = Response::get(@url)
assert_title('WebUnit Sample', response)
end

Response::get(@url)で@urlにアクセスし、その結果のタイトルをテストします。

assert_urls

リンクのテスト

  def test_urls
response = Response::get(@url)
#リンクの一覧
urls = response.links
#外部リンクのテスト
assert_urls('http://www.google.co.jp/', urls[0].href)

Response#links でテキストリンクの一覧(配列)を取得します。
先頭のテキストリンク(<a href="http://www.google.co.jp/">Google</a>)のリンク先(urls[0].href)をテストします。

assert_input

Inputフィールドのテスト

  def test_input
response = Response::get(@url)
form = response.form #Response#forms[0]
parameters = form.parameters #フォームに含まれるフィールドの配列
#テキストフィールド
#<input name="textfield" type="text" value="" />
assert_input('textfield', 'text', '', parameters[0])

response.form で最初のFormを取得し、form.parameters でフォームに含まれるフィールドを配列で取得します。
最初のフィールド(parameters[0)のnameが 'textfield'、typeが'text'、valueが''であることをテストします。

assert_select_option

選択肢(option)のテスト

    response = Response::get(@url)
form = response.form #Response#forms[0]
params = form.params #フォームに含まれるフィールドのHash({|名前,HtmlElem|,..})
#<select name="menu" id="menu">
#<option value="perl" selected="selected">Perl</option>
#<option value="ruby">Ruby</option>
#<option value="c++">C++</option>
#</select>
options = params['menu'].children
assert_select_option('perl', 'Perl', true, options[0])
assert_select_option('ruby', 'Ruby', false, options[1])
assert_select_option('c++', 'C++', false, options[2])

Form#params でフォームに含まれるフィールドをHash(form.params)で取得します。
nameが'menu'であるフィールド(params['menu'])に含まれているタグの配列(params['menu'].children)を取得し、name、表示文字列、選択状態をテストします。

assert_attrs

タグの属性のテスト

  def test_attribute
response = Response::get(@url)
#<table border="1">
assert_attrs({'border' => '1'}, response.tables[0])

最初のテーブルタグのborder属性の値が'1'であることをテストします。

assert_include

タグの含有のテスト

  def test_include
response = Response::get(@url)
#文字列を含む
assert_include('テキストエリア', response.tables[1])

2番目のテーブルタグ(response.tables[1])の配下に、'テキストエリア'の文字が含まれているかをテストします。

submit

フォームの送信

  def test_submit
response = Response::get(@url)
params = response.form.params #フォームに含まれるフィールドのHash({|名前,HtmlElem|,..})
#テキストフィールド
params['textfield'].value = 'てきすとふぃーるど'
#非表示フィールド
# 非表示フィールドの値も設定できる
params['hiddenfield'].value = '非表示ふぃーるど' (中略) #Submitした結果を取得する。
response = response.form.submit

フォームの各フィールドの値(Param#value)を設定します。
response.form.submit で送信した結果を取得します。

GeSource > Ruby覚え書き > WebUnitのサンプル

2004年07月23日