2011-03-28 25 views
0

在硒IDE,由Perl驅動程序/格式化器安裝的代碼模板默認包含一個perl硒:測試::異常,正確的使用和良好的代碼示例?

use Test::Exception; 

行代碼。

我對這個模塊測試:: WWW :: Selenium有幾個問題。

應該在我的.t文件中使用Test :: Exception嗎?

到目前爲止,我沒有使用任何方法,我的測試運行得很好(我通常做快樂路徑測試)。

現在我想出了一個潛在的用途。我注意到,如果硒對象在頁面上找不到某些東西或定位器錯誤等情況,硒對象有時會死亡。在很多情況下,我希望我的測試繼續進行,即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"); 


} 

編輯 - (問題仍然unanswered-我只是增強了方法一點點,使其更加堅固):

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" 
    ); 

} 

回答

5

你不應該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,這是更輕量級的方法。

+2

+1提到T :: F – daxim 2011-03-28 12:46:33

+2

@daxim - 我注意到穆斯最近從T :: E變爲T :: F。這裏還有一些[rjbs測試::致命背後的基本原理](http://rjbs.manxome.org/rubric/entry/1863) – bvr 2011-03-28 12:54:00

+0

thaanks for good link(rjbs.manxome.org) – knb 2011-03-28 13:07:00