在selenium IDE中,perl driver/formatter安装的代码模板包括一个
use Test::Exception;
默认的代码行。
我有几个关于 Test::WWW::Selenium 模块的问题。
是否应该在我的 .t 文件中使用 Test::Exception?
直到现在我还没有使用它的任何方法,我的测试运行得很好(我通常做 Happy-Path 测试)。
现在我想出了一个潜在的用途。我注意到,如果 selenium 对象在页面上找不到某些东西或定位器错误等,有时它会死掉。在许多情况下,我希望我的测试继续进行,即 Selenium 不应该死掉,并继续在页面上做事。
这是否是 Test::Exception 方法的正确用法? 我应该尝试将它与 Try::Tiny 结合使用吗?
这是我刚写的一个小辅助方法。 lives_and 方法属于 Test::Exception。
sub verify_text_qr {
my ( $sel, $text ) = @_;
#$sel - the selenium object
#$text ||= 'I think that'; # some text I am looking for on the page
lives_and( sub {
my $found = $sel->get_text("//p[contains(text(), '$text')]");
like( $found, qr /$text/)
},
"found '$text' on page" );
}
编辑 -(问题仍未得到解答 - 我只是稍微增强了方法,使其更健壮):
sub verify_text_qr {
my ( $sel, $text ) = @_;
#my $text = 'Es ist unstrittig, dass ';
my $found;
lives_and(
sub {
try {
$found = $sel->get_text("//p[contains(text(), '$text')]");
}
catch {
fail( "cannot find '$text': " . $_ );
$found = 0;
note "on page " . $sel->get_location() . ", " . $sel->get_title();
};
SKIP: {
skip "no use in searching for '$text'", 1 unless $found;
like( $found, qr/$text/ ); # or $sel->like() ??
}
},
"looked for '$text' on page"
);
}
最佳答案
您不应该与 Try::Tiny 结合使用,因为 Test::Exception正在为你捕捉它。简单演示:
use Test::More;
use Test::Exception;
lives_and { is not_throwing(), "42" } 'passing test';
lives_and { is throwing(), "42" } 'failing test';
done_testing;
sub not_throwing { 42 }
sub throwing { die "failed" }
所以我会以与您的第一个片段类似的方式使用它。您也可以考虑使用 Test::Fatal ,这是一种更轻量级的方法。
关于perl selenium:Test::Exception、正确用法和好的代码示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5458223/