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.
Best Answer
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");
towindow.open("someUrl", "someName");
, you'll be then able to select the window by the set name. More information on the MDN doc forwindow.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 thehref
attribute and callIf it is opened via JavaScript before or during the
onload
event, you'll need to callMore information on this in the bug SEL-339 or in the
openWindow()
andselectWindow()
JavaDocs.If you have only two windows / want to open the newest one, you can try
selenium.selectPopup()
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
or
selenium.selectWindow("title=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.