2010-11-16 88 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()); 
     } 
    } 

    }