I run my selenium rc test in Eclipse with TestNG. I have a link which tries to open a new browser page. How can I select this new page to operate in? I use this code:
selenium.selectWindow("name=NewPage");
however it says page not found. I also try to define page ids or titles with this code:
String[] wins = selenium.getAllWindowIds();
for (String s : wins)
System.out.println("win: " + s);
It does not define my new opened window:
win: MainPage
win:
If use selenium.getAllWindowNames()
I get win: selenium_main_app_window
win: selenium_blank65815
.
I write this code selenium.selectWindow("name=blank99157");
but get the error - ERROR: Window does not exist. If this looks like a Selenium bug, make sure to read http://seleniumhq.org/docs/02_selenium_ide.html#alerts-popups-and-multiple-windows for potential workarounds.
The window obviously has no name, so you can't select it by name.
If the window is opened via JavaScript and you can change the script, try changing window.open("someUrl");
to window.open("someUrl", "someName");
, you'll be then able to select the window by the set name. More information on the MDN doc for window.open()
.
Selenium RC doesn't support <a href="someUrl" target="_blank">
links (which open the link in a new window). Therefore, if the window is opened by a link of this type, you have to find this <a>
element, get the href
attribute and call
selenium.openWindow(theFoundUrl, "theNewWindow");
selenium.selectWindow("id=theNewWindow");
If it is opened via JavaScript before or during the onload
event, you'll need to call
selenium.openWindow("", "theNewWindow");
selenium.selectWindow("id=theNewWindow");
More information on this in the bug SEL-339 or in the openWindow()
and selectWindow()
JavaDocs.
If you have only two windows / want to open the newest one, you can try
That is, obviously, the easiest way, because it selects the first non-top window. Therefore, it's only useful when you want to select the newest popup.
If the new window has a unique title, you can do
selenium.selectPopup("Title of the window");
Otherwise, you must iterate over selenium.getAllWindowNames()
to get the right name (Selenium creates names for windows without one). However, you can't hardcode that name into your testcase, because it will change every time, so you'll need to work out some dynamic logic for this.
You won't like this: Go for WebDriver. It should be far more resistant to such problems.