2010-11-16 103 views
4

所以,使用Selenium,我想測試頁面上的鏈接,看看他們是否打開一個新窗口。他們不是JavaScript鏈接,只是一個基本的href「target = _blank」。 我想確保新打開的窗口實際上加載了一個頁面。 我可以做所有的腳本來獲得鏈接點擊,但是當我測試頁面標題時,我得到了我正在測試的頁面,而不是頂部的新窗口。 如何定位新窗口並檢查THAT頁面是否已加載?硒href空白新窗口測試

謝謝

回答

3

以下爲我工作與屬性的目標=「_空白」發送一個新的窗口POST請求的一種形式:

// Open the action in a new empty window 
selenium.getEval("this.page().findElement(\"//form[@id='myForm']\").target='my_window'"); 
selenium.getEval("selenium.browserbot.getCurrentWindow().open('', 'my_window')"); 

//The contents load in the previously opened window 
selenium.click("//form[@id='myForm']//input[@value='Submit']"); 
Thread.sleep(2000); 

//Focus in the new window 
selenium.selectWindow("my_window"); 
selenium.windowFocus(); 
/* .. Do something - i.e.: assertTrue(.........); */ 

//Close the window and back to the main one 
selenium.close(); 
selenium.selectWindow(null); 
selenium.windowFocus(); 

該html代碼將類似於:

<form id="myForm" action="/myAction.do" target="_blank"> 
    <input type="text" name="myText" value="some text"/> 
    <input type="submit" value="Save"/> 
</form> 
+0

不錯的解決方案,但我[不能讓它在Selenium IDE中工作](http://stackoverflow.com/questions/10092693/result-is-null-error-when-working-around-blank-deficiency-in-硒IDE)。 – l0b0 2012-04-11 07:46:54

1

您已經標記了RC問題,因此我認爲它不是Selenium IDE。

您可以使用類似selenium.selectWindow或selenium.selectPopUp或selenium.windowFocus的對象來定位新窗口。

我發現一個非常有用的技術是使用Selenium IDE捕獲腳本,然後選擇選項,然後選擇需要的編程格式(Java,C#等),然後使用該片段作爲RC測試的基礎。

+0

我在php中運行RC。因此,我可以運行$ this-> getAllWindowNames()來獲取窗口的名稱,但Selenium隨機化新打開的選項卡/窗口的名稱。在2個窗口的數組上,它返回「selenium_main_app_window」和「selenium_blank66115」。我已經嘗試在鏈接的標記中設置目標屬性,但Selenium仍然認爲目標是空白的並插入隨機名稱。 – hogsolo 2010-11-16 19:13:32

1

基於名稱隨機化,我想我可以遍歷窗口名稱並選擇未知的名稱。 這工作,但不完全測試...

public function testMyTestCase() { 
    $this->open("/"); 
    $this->click("link=Sign in"); 
    $this->waitForPageToLoad("30000"); 
    $this->type("email", "[email protected]"); 
    $this->type("password", "xxx"); 
    $this->click("login"); 
    $this->waitForPageToLoad("30000"); 
    $this->click("link=Resources"); 
    $this->waitForPageToLoad("30000"); 
    $this->click("link=exact:http://100pages.org/"); 

    $cc = $this->getAllWindowNames(); 
    foreach($cc as $v) {   
     if (strpos($v, "blank")) {     
      $this->selectWindow($v); 
      $this->waitForPageToLoad("30000");   
      $this->assertRegExp("/100/", $this->getTitle()); 
     } 
    } 

    }