开发者

Selenium href blank New Window test

开发者 https://www.devze.com 2023-01-25 04:01 出处:网络
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\".

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 new开发者_StackOverflow社区ly 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


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>


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.


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

  }
0

精彩评论

暂无评论...
验证码 换一张
取 消