Selenium href blank New Window test

匿名 (未验证) 提交于 2019-12-03 02:30:02

问题:

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

回答1:

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> 


回答2:

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.



回答3:

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!