Selenium href 空白新窗口测试

发布于 2024-10-02 18:06:54 字数 198 浏览 4 评论 0原文

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

谢谢

So, using Selenium, I want to test links on a page and see if they open a new window. They are NOT javascript links, just a basic href "target=_blank".
I want to make sure the newly opened window actually loaded a page.
I can do all the scripting to get the link clicked, but when i test for page Title, I get the page I'm testing on, not the new window that is on top.
How do I target that new window and check to see if THAT page loaded?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

被你宠の有点坏 2024-10-09 18:06:54

以下内容对我有用,其属性为 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>

The following worked for me with a form with attribute target="_blank" that sends a POST request on a new window:

// 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();

The html code would be similar to:

<form id="myForm" action="/myAction.do" target="_blank">
    <input type="text" name="myText" value="some text"/>
    <input type="submit" value="Save"/>
</form>
痴情换悲伤 2024-10-09 18:06:54

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

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

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

You've tagged the question RC so I assume it's not Selenium IDE.

You can use something like selenium.selectWindow or selenium.selectPopUp or selenium.windowFocus to target the new window.

A technique I find quite useful is to use Selenium IDE to capture the script and then select Options and then the programming format you require (Java, C# etc.) and then use that snippet as the basis of the RC test.

对不⑦ 2024-10-09 18:06:54

根据名称随机化,我想我可以循环浏览窗口名称并选择未知的名称。
这有效,但尚未经过充分测试...

 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());
        }
    }

  }

Based on the name randomization, I guess I can loop through the window names and pick th unknown one.
This works, but not tested fully...

 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());
        }
    }

  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文