Test::WWW::Selenium を使用して Web ページでアクションを実行している間、私の perl プログラムは TAP テストケースの出力を、TAP::Harness によって作成および更新された html レポートに書き込みます。
私の Selenium クライアントは、パスワードで保護された Web ページにログインする必要があり、これは避けられません。したがって、パスワードは 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 で動作するはずです。TAP::Formatter::HTML モジュールを混乱させ、正しく動作しなくなる可能性があるため、プログラムは $fh で引き続き動作するはずです。自分で開いたり閉じたりしたくありません。
アップデート
#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;
}