2012-07-17 70 views
0

我在Eclipse中使用Selenium WebDriver。如何正確檢查頁面標題?

我寫的方法來檢查標題是否顯示正確。下面是代碼:

class Check { 
    String text_to_found; 
    String reason; 

    Check (String t, String r) { 
     text_to_found=t; 
     reason=r; 
    } 

    public void check_title() { 
     try { 
      Assert.assertTrue("Title " + text_to_found + " not found", text_to_found.equals(reason)); 
     } catch (AssertionError e) { 
      System.err.println("title not found: " + e.getMessage()); 
     } 
} 

我有這樣的命令調用它:

它的工作原理正確
Check title1 = new Check ("Title", driver.getTitle()); 
title1.check_title(); 

第一次。但是第二次(依此類推),如果我調用這個方法(對於新打開的窗口),它表示沒有找到標題,但我知道它是正確的。建議,代碼有什麼問題?

回答

0

我剛剛使用您的代碼來檢查google.com的標題。在你的代碼,如果標題不匹配 -

Check title1 = new Check ("G3oogle", driver.getTitle()); 
    title1.check_title(); 

我們得到的結果 - title not found: Title G3oogle not found

但如果如下匹配 -

Check title1 = new Check ("Google", driver.getTitle()); 
    title1.check_title(); 

不打印在控制檯上的任何東西。所以如果你想打印任何東西,如果標題匹配,那麼你可以修改你的代碼 -

 public void check_title() {  

    if(text_to_found.equals(reason)){ 
      System.out.println("title found: Title "+text_to_found+" found"); 
    } 
    else{ 

     System.out.println("title not found: Title "+text_to_found+" not found"); 
    } 
    }