Вы находитесь на странице: 1из 5

Selenium selectWindow failing

A ton of research and testing has gone into my conclusion that the method selectWindow
simply does not work for anything but an empty string or NULL. The investigation started with
Selenium IDE and continued in SeleniumRC with java in Eclipse. As of this writing the
versions used were

Selenium IDE 1.0.2


Selenium Remote Control 1.0.1
Firefox 3.0.11
IE 6

Use Case:
1. Navigate to the site’s main page
2. Click on a link that spawns a new window/tab
3. Select the new window
4. Validate stuff in the new window with asserts and verifies
5. Close the new window
6. Return to the main window
7. Rinse and repeat with other browsers and platforms ( IE6, IE7, FF2, FF3, WinXP, Mac,
Vista, …)

1st attempt
Firefox + Selenium IDE record/replay
All steps recorded as expected except for the close method at the end
open /
assertElementPresent link=Texas Accessibility
Policy
click link=Texas Accessibility
Policy
selectWindow name=AccessibilityPolicy
waitForTitle AccessibilityPolicy
verifyText //div[@id='fullPage']/h1 Electronic And
Information Resources
Accessibility Policy

This has never replayed correctly. Each time we get to the selectWindow step it fails and says
“[error] Window does not exist. If this looks like a Selenium bug, make sure to read
http://selenium-core.openqa.org/reference.html#openWindow for potential workarounds.”

The suggested URL was wrong so I had to search for their “workarounds”. In short, none of
the workarounds work, which violates the basic premise of the term. They should be called
“time wasters” instead because I have about 60 hours into this problem so far with no working
tests in either IDE or RC that the QA group could use.
What I have tried and observed so far:

The first case that we tried was complex, in that the new window was a completely different
domain as the original window.

Main URL https://www.texascollegesavings.com/


New Window URL http://www.window.state.tx.us/accessibility.html

This led me to believe that we were violating the JavaScript Same Origin Policy
(http://www.devarticles.com/c/a/JavaScript/JavaScript-Security/). In order to eliminate the
same origin policy as a problem I changed to a different site

Main URL http://opnet/HomeSite/HomePage/HomePage.asp


New URL http://opnet/departments/hr_contacts/learningdev/hrportal/Index.html

Since neither the domain or the port changed for the new window, this should not violate the
security policy.

Next step was to create a script that would conform to the use case. In the Selenium IDE
recording produced:

open /HomeSite/HomePage/HomePage.asp
waitForTitle OPnet, The OFI Corporate Intranet
verifyTitle OPnet, The OFI Corporate Intranet
clickAndWait //table[@id='TVAds_Table']/tbody/tr/td[2]/a/img
selectWindow name=NEW
waitForTitle E-HR Home Page
selectWindow name=null
waitForTitle OPnet, The OFI Corporate Intranet

At first I was ecstatic that it recorded very much the way I thought it should look. However on
replay I got the exact same problem. On the step
selectWindow | name=NEW |

it returned more than 1 error. Output:


• [info] Executing: |open | /HomeSite/HomePage/HomePage.asp | |
• [info] Executing: |waitForTitle | OPnet, The OFI Corporate Intranet | |
• [info] Executing: |verifyTitle | OPnet, The OFI Corporate Intranet | |
• [info] Executing: |clickAndWait | //table[@id='TVAds_Table']/tbody/tr/td[2]/a/img | |
• [error] Timed out after 30000ms
• [info] Executing: |selectWindow | name=NEW | |
• [error] Window does not exist. If this looks like a Selenium bug, make sure to read
http://selenium-core.openqa.org/reference.html#openWindow for potential workarounds.

So the clickAndWait did not work since the original page was not ever re-loading, so turn that
back to a click. But the last error is confounding because Selenium recorder captured the
“name” of the new window so it must be able to “see” the window, only that’s not true on
playback. Further research on the forums and documentation shows I should try using the
waitForPopUp method
waitForPopUp ( windowID,timeout )

Apparently name and windowID are synonymous, so I used the value of NEW which is the
value of the target attribute on our main page that creates the new window. Again, it did not
work

 [info] Executing: |waitForPopUp | NEW | 2000 |


 [error] Timed out after 2000ms
 [info] Executing: |selectWindow | name=NEW | |
 [error] Window does not exist. If this looks like a Selenium bug, make sure to read http://selenium-
core.openqa.org/reference.html#openWindow for potential workarounds.

This is when I bailed out of the IDE and started working with Java in Eclipse with Selenium
Remote control. This way I could use some other methods like
getAllWindowIds()
getAllWindowNames()
getAllWindowTitles()

And I could also get some more logging options from the RC server. This led to some
interesting discoveries.

If the browser creates a new window using the method


<a target="NEW" href="/departments/hr_contacts/learningdev/hrportal/Index.html">
<img border="0" src="graphics/ehr2.gif"/>
</a>
Selenium cannot see the new window with the name “NEW”. I came to that conclusion
because all three getAll… methods return only the main window, not the child.
windowIds String[1] (id=51)
[0] "undefined" (id=55)
windowNames String[1] (id=53)
[0] "selenium_main_app_window" (id=56)
windowTitles String[1] (id=54)
[0] "OPnet, The OFI Corporate Intranet" (id=57)

No wonder the selectWindow | NEW statement does not work, because the collection of
available windows does not have that window in it. I can see the window open and investigate
it’s properties with Firebug or at the Javascript console, but Selenium cannot. This means that
an extremely common method of opening new windows in web programming is unusable and
not testable using Selenium. Another interesting discrepancy is that the windowIds array
shows the window id to be “undefined” which does not match the documentation for Selenium.

Out of curiosity I modified the steps to perform the open window myself instead of emulating
the click behavior on the object in the page.

The steps:
- capture the HREF attribute of the object and store it in a variable
- use the openWindow method from Selenium to create a new window with an ID that I
control and the href from the page

The code:
String eHRhref =
selenium.getAttribute("//table[@id='TVAds_Table']/tbody/tr/td[2]/a@href");
selenium.openWindow(eHRhref, "eHR");

Now when I run Selenium CAN see both the main window and the child window
windowIds String[2] (id=47)
[0] "undefined" (id=828)
[1] "undefined" (id=829)
windowNames String[2] (id=52)
[0] "selenium_main_app_window" (id=54)
[1] "eHR" (id=824)
windowTitles String[2] (id=53)
[0] "OPnet, The OFI Corporate Intranet" (id=823)
[1] "E-HR Home Page" (id=826)

However the window IDs still come up as “undefined” which does not match they say the ID
should be. Continuing on to the selectWindow step, based on the Selenium documentation I
should be able to use the name of “eHR” to select the new window, a name I made up.

The code:
selenium.selectWindow("eHR");

The output/error:
com.thoughtworks.selenium.SeleniumException: ERROR: Window does not exist. If this looks
like a Selenium bug, make sure to read http://selenium-
core.openqa.org/reference.html#openWindow for potential workarounds.
at
com.thoughtworks.selenium.HttpCommandProcessor.throwAssertionFailureExceptionOrError(
HttpCommandProcessor.java:97)
at
com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.ja
va:91)
at
com.thoughtworks.selenium.DefaultSelenium.selectWindow(DefaultSelenium.java:343)
at OpnetNewWindow.testOpnetNewWindow(OpnetNewWindow.java:60)…

Window does not exist?! I made the window, I named the window, I asked Selenium if it can
see the window and sure enough it can, now it says it cannot. This is what is called a defect.
The thing just does not work as designed.

I then played some more with the selectWindow method and found that only and empty string
“” or the value of NULL would run without error.

Вам также может понравиться