perl:不显眼地写入由TAP :: Formatter :: HTML打开的IO :: Handle

时间:2010-11-25 09:47:52

标签: perl testing selenium tap

在使用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输出中删除密码,我已经提出了下面的功能,但它不起作用。 anyony可以帮忙吗?

我用

创建了一个formater对象
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;
}

1 个答案:

答案 0 :(得分:1)

嗯...一种可能但简单化的方法是从TAP :: Parser :: Result :: Test覆盖描述方法(我很确定它应该通过一个子类来完成但是在早上太过于错出正确的方式)。

以下草案的内容:

# Original: sub description { shift->{description} }
*TAP::Parser::Result::Test::description = sub {
     my $description = shift->{description}; 
     $description =~ s/type, id_password, .*$/type, id_password, XXX_PASSWORD_REMOVED_XXX/;
     return $description;
 };