Handling browser pop-up windows with Selenium

2020-02-08 07:51发布

We are running Selenium regression tests against our existing code base, and certain screens in our web app use pop-ups for intermediate steps.

Currently we use the commands in the test:

// force new window to open at this point - so we can select it later
selenium().getEval("this.browserbot.getCurrentWindow().open('', 'enquiryPopup')");
selenium().click("//input[@value='Submit']");
selenium().waitForPopUp("enquiryPopup", getWaitTime());
selenium().selectWindow("enquiryPopup");

...which works most of the time. Occasionally the test will fail on the waitForPopUp() line with

com.thoughtworks.selenium.SeleniumException: Permission denied

Can anyone suggest a better, more reliable method?

Also, we primarily run these tests on IE6 and 7.

5条回答
We Are One
2楼-- · 2020-02-08 08:02

If you are running in *iehta mode then you are going to run into some glitches here and there. We run Selenium at my job and there seem to be lots of issues with IE and AJAX.

However, it sounds like the issue you are running into is one where Selenium is trying to access a component in another window before it completely loads up. I am not sure what your default timeout range is set to, but you may want to try increasing it to 60 (60000ms) seconds or so to get past the issue.

Other than that I would suggest running your tests in Firefox (using *chrome) as it produces much more reliable results, but sometimes it is simply not possible due to business requirements.

查看更多
ら.Afraid
3楼-- · 2020-02-08 08:12

I just trialled adding another selenium function, windowFocus():

// force new window to open at this point - so we can select it later
selenium().getEval("this.browserbot.getCurrentWindow().open('', 'enquiryPopup')");
selenium().click("//input[@value='Submit']");
selenium().windowFocus("enquiryPopup");
selenium().waitForPopUp("enquiryPopup", getWaitTime());
selenium().selectWindow("enquiryPopup");

The test succeeded when I ran it locally, but only with all those method calls - create/focus/wait/select.

I'm about to let the build server run all the tests, and if that succeeds too, I'll be making a library function out of it...!

查看更多
爷的心禁止访问
4楼-- · 2020-02-08 08:15

It works!! Just to make it easier for the folks who prefer selenese.

This worked for me using IE7(normal mode).

What a freaking hassle. Thank the spaghetti monster in the sky for SO or there is no way I would have got this working in IE.

<tr>
    <td>getEval</td>
    <td>selenium.browserbot.getCurrentWindow().open('', 'windowName');</td>
    <td></td>
</tr>
<tr>
    <td>click</td>
    <td>buttonName</td>
    <td></td>
</tr>
<tr>
    <td>windowFocus</td>
    <td>windowName</td>
    <td></td>
</tr>
<tr>
    <td>waitForPopUp</td>
    <td>windowName</td>
    <td>3000</td>
</tr>
<tr>
    <td>selectWindow</td>
    <td>windowName</td>
    <td></td>
</tr>
查看更多
叛逆
5楼-- · 2020-02-08 08:15

Try adding some wait statements around the calls that are causing you issues.

I've had the same errors before and the only way I was able to reliably resolve them was by making calls to System.Threading.Thread.Sleep(5000)..

查看更多
聊天终结者
6楼-- · 2020-02-08 08:18

I needed to select an iframe within a popup window and fill out a form. I had trouble using the selectWindow cmd where selenium couldn't find my iframe, so I removed the command.

This selenese worked well for me (where the iframe title and id = account_frame) :

<tr>
  <td>click</td>
  <td>//a[@class='item_add']</td>
  <td></td>
</tr>
<tr>
  <td>windowFocus</td>
  <td>account_frame</td>
  <td></td>
</tr>
<tr>
  <td>waitForPopUp</td>
  <td>account_frame</td>
  <td>10000</td>
</tr>
查看更多
登录 后发表回答