在使用 Test::WWW::Selenium 在网页上执行操作时,我的 perl 程序将 TAP 测试用例输出写入由 TAP::Harness 创建和更新的 html 报告。
我的 Selenium 客户端必须登录一些受密码保护的网页,这是不可避免的。因此密码显示在 TAP 输出中。
ok 1 - open, /homepage-with-loginform.html, true
ok 2 - type, id_username, JoeSixp
ok 3 - type, id_password, secrIt
...
为了从 HTML 输出中删除密码,我想出了下面的函数,但它不起作用。任何人都可以帮忙吗?
我创建了格式化程序对象
my $fmt = TAP::Formatter::HTML->new;
my $harness = TAP::Harness->new( { formatter => $fmt, merge => 1 } );
$harness->test_args( [ "--browser=$browser", "--config=$h{config}" ] );
my $agg = $harness->runtests(@tests);
#remove passwords from HTML report
remove_password( \%h, $fmt ); # %h is an options hash
...
sub remove_password {
# remove password from HTML report
my ( $h, $fmt ) = @_;
my $passwd = $h->{password} || "xxx-xxx";
my $outhtml = ${ $fmt->html() };
#from the TAP::Harness perldoc
#html() - This is a reference to the scalar containing the html generated on the last test run.
#Useful if you have "verbosity" set to silent, and have not provided a custom "output_fh" to write the report to.
note("replacing password with xxx-xxx in html file");
$fmt->html(\$outhtml); # does not work
$outhtml =~ s/$passwd/xxxxxx/msg; # works
{
local $/ = undef;
my $fh = $fmt->output_fh(); #An IO::Handle filehandle for printing the HTML report to.
if ( $fh->opened){
#
# ??? HOW DO I unobtrusively truncate the file here?
#
print $fh $outhtml; # writes back censored HTML output
}
}
}
我认为这不是一个简单的“我如何使用 perl 截断文件”的问题。
该文件已经被一些库代码打开,并且该代码应该可以在 unix 和 windows 上运行。该程序应该继续使用 $fh ,我不想自己打开和关闭它,因为它可能会混淆 TAP::Formatter::HTML 模块并且它可能会停止正常工作。
更新
#this gets the job done but it is kind of brutal
my $outf = $h->{outfile};
if ( $fh->opened){
$fh->close();
open $fh, ">", $outf or die "cannnot open '$outf' for truncating and writing:$!";
print $fh $outhtml;
close $fh;
}