Seleniumのテストファイルを結合する

SeleniumIDEで作ったテストのファイルを1つのHTMLにまとめられたら便利だなと思ったのでLet's Try!

イメージ的には

  • スクリプトの引数にディレクトリを指定すると、そのディレクトリ配下にあるHTMLを検索。
  • 各ファイルを読み込んでコマンドの配列を抜き出し、それらを結合。
  • 最終的に1つのHTMLのファイルにする。

こんな感じ。
で、試行錯誤の挙句、こんなスクリプトになりました
使ったモジュールはFile::Find、Template、HTML::TagParserの3つ。

  • sel_join.pl
use strict;
use warnings;
use Template;
use File::Find;
use HTML::TagParser;

my $search_root = "./test";
my $target_ext = 'html';

#テストコマンドの配列を取得する
my @files;
find(sub{push(@files, $File::Find::name)},$search_root);
my @target_files = grep {ext_filter($_,$target_ext)} @files;
my @cmd_array;
map {push(@cmd_array,parse_html($_))} @target_files;

#テンプレートを使ってHTMLを出力する
my $template = Template->new();
$template->process(
	'selenium_base.tt',
	{cmd_array => \@cmd_array,},
	'SeleniumJoinTest.html',
) or die $template->error();

#HTMLファイルからコマンドの配列を抜き出す
sub parse_html{
	my $filename = shift;
	my $parser = HTML::TagParser->new($filename);
	my @list = $parser->getElementsByTagName("tr");
	my @result;
	foreach my $elem (@list){
		my $inner = $elem->innerText;
		$inner =~ s/\t//;
		my ($cmd,$target,$value) = split(/\n/,$inner);
		next unless $cmd && $target;
		my $param = {
			cmd => $cmd,
			target => $target,
			value => $value,
		};
		push(@result,$param);
	}
	@result;
}

#拡張子フィルタ
sub ext_filter{
	my ($path, $regex) = @_;
	$regex = '*' unless $regex;
	$path =~ /(\.($regex))$/
}

__END__

parser_htmlの部分がまだまだ改良の余地がありそう。
で、テンプレートはこんな感じ。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>結合されたテスト</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">結合されたテスト</td></tr>
</thead><tbody>
[% FOREACH item IN cmd_array %]<tr>
	<td>[% item.cmd %]</td>
	<td>[% item.target %]</td>
	<td>[% item.value %]</td>
</tr>
[% END %]
</tbody></table>
</body>
</html>

これで./testに以下の2つのSeleniumIDEで出力されたHTMLを置いてみる

  • ./test/sample1.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>sample1</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">sample1</td></tr>
</thead><tbody>
<tr>
	<td>open</td>
	<td>/test.html</td>
	<td></td>
</tr>
<tr>
	<td>verifyTextPresent</td>
	<td>テスト1</td>
	<td></td>
</tr>
</tbody></table>
</body>
</html>
  • ./test/sample2.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>sample2</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">sample2</td></tr>
</thead><tbody>
<tr>
	<td>open</td>
	<td>/test.html</td>
	<td></td>
</tr>
<tr>
	<td>verifyTextPresent</td>
	<td>テスト2</td>
	<td></td>
</tr>
</tbody></table>
</body>
</html>

この状態で

perl sel_join.pl

と実行して出来たファイルを見てみるとこんな感じ。

  • SeleniumJoinTest.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>結合されたテスト</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">結合されたテスト</td></tr>
</thead><tbody>
<tr>
	<td>open</td>
	<td>/test.html</td>
	<td></td>
</tr>
<tr>
	<td>verifyTextPresent</td>
	<td>テスト1</td>
	<td></td>
</tr>
<tr>
	<td>open</td>
	<td>/test.html</td>
	<td></td>
</tr>
<tr>
	<td>verifyTextPresent</td>
	<td>テスト2</td>
	<td></td>
</tr>

</tbody></table>
</body>
</html>

おお、うまくいってる!