Seleniumで個別のテスト(TestCase)を分割してTestSuiteでまとめることがよくあるけど、
TestSuiteはSeleniumIDEで扱えないので一連のテストをSeleniumIDEで流すときはTestCaseを何回も読み込まなくちゃいけなくて面倒くさい。
あんまり開いたり閉じたりすると作ったTestCase保存し忘れたりするし…。でもTestCase個別で流したいときもあるので分割はしておきたい。
なので長いTestCaseを分割してTestSuiteを出力するスクリプトを書いてみた。
#! ruby -Ku require 'kconv' def output_testcase(header,section,body) out = open(header.tosjis + "-" + section.tosjis + ".html","w") out.puts <<EOS <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>#{header}-#{section}</title> </head> <body> <table cellpadding="1" cellspacing="1" border="1"> <thead> <tr><td rowspan="1" colspan="3">#{header}-#{section}</td></tr> </thead><tbody> #{body} </tbody></table> </body> </html> EOS end def output_testsuite(suite_body, header) print <<EOS <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Test Suite</title> </head> <body> <table border="1"> <thead> <tr><td>#{header}</td></tr> </thead><tbody> #{suite_body} </tbody> </table> </body> </html> EOS end File::open(ARGV[0]) {|f| header = nil section = nil body = "" suite_body = "" f.each {|line| if /<td rowspan="1" colspan="3">/ =~ line header = line.scan(/<tr><td rowspan="1" colspan="3">(.\S*)<\/td><\/tr>/).join elsif /<!--\s*\*/ =~ line if section output_testcase(header,section,body) body = "" end section = line.scan(/<!--\s*\*(.\S*)\s*-->/).join suite_body += "<tr><td><a href=\"./#{header}-#{section}.html\">#{section}</a></td></tr>\r\n" elsif /<\/table>/ =~ line output_testcase(header,section,body) break elsif section body += line end } output_testsuite(suite_body,header) }
「*」で始まるコメントを見出しにしてそこでTestCaseに分割。TestSuiteは標準出力へ。