【问题标题】:Selenium href blank New Window testSelenium href 空白新窗口测试
【发布时间】:2011-05-10 23:30:15
【问题描述】:

所以,我想使用 Selenium 测试页面上的链接,看看它们是否打开了一个新窗口。它们不是 javascript 链接,只是一个基本的 href "target=_blank"。 我想确保新打开的窗口实际加载了一个页面。 我可以编写所有脚本来点击链接,但是当我测试页面标题时,我得到的是我正在测试的页面,而不是顶部的新窗口。 如何定位该新窗口并检查该页面是否已加载?

谢谢

【问题讨论】:

    标签: selenium-rc href target


    【解决方案1】:

    基于名称随机化,我想我可以遍历窗口名称并选择未知的名称。 这可行,但尚未完全测试...

     public function testMyTestCase() {
      $this->open("/");
      $this->click("link=Sign in");
      $this->waitForPageToLoad("30000");
      $this->type("email", "xxx@gmail.com");
      $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());
            }
        }
    
      }
    

    【讨论】:

      【解决方案2】:

      以下内容对我有用,它使用属性 target="_blank" 在新窗口上发送 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>
      

      【讨论】:

      【解决方案3】:

      您已标记问题 RC,所以我认为它不是 Selenium IDE。

      您可以使用 selenium.selectWindow 或 selenium.selectPopUp 或 selenium.windowFocus 之类的东西来定位新窗口。

      我发现一个非常有用的技术是使用 Selenium IDE 来捕获脚本,然后选择选项,然后选择您需要的编程格式(Java、C# 等),然后使用该 sn-p 作为 RC 测试的基础.

      【讨论】:

      • 我在 php 中运行 RC。所以我可以运行 $this->getAllWindowNames() 来获取窗口的名称,但是 Selenium 会随机化新打开的选项卡/窗口的名称。在 2 窗口数组上,它返回“selenium_main_app_window”和“selenium_blank66115”。我已经尝试在链接的标记中设置目标属性,但 Selenium 仍然认为目标是空白的并插入一个随机名称。
      猜你喜欢
      • 2014-08-15
      • 2015-10-08
      • 1970-01-01
      • 2023-04-03
      • 2014-04-08
      • 1970-01-01
      • 2019-05-16
      • 2012-12-17
      • 1970-01-01
      相关资源
      最近更新 更多