How to wait for a popup window to close?

2,068 views
Skip to first unread message

jerald

unread,
Sep 21, 2011, 2:38:25 AM9/21/11
to webd...@googlegroups.com
How to wait for a popup window to close? Am filling a form and hitting a save button in a popup window and the form saves in ajax mode. So the page doesn't loads and after some seconds(takes some 5 secs) the form is posted successfully the popup window closes itself and returns to main window. Here i don't know how to find if the window is closed. 

Now am doing this -

private boolean checkIfPopupWindowClosed()
    {
        for(int i=0;i<10;i++)
        {
            try{Thread.sleep(1000);}catch(Exception e){}
            Set<String> wHandle = driver.getWindowHandles();
            if(wHandle.size() < 2)
            {
                return true;
            }
        }
        return false;
    }

is there any better way to do this?

Thanks
jerald

Jerald paul

unread,
Sep 21, 2011, 2:55:32 AM9/21/11
to webd...@googlegroups.com
The code is working but it would be helpful if anyone could achieve this without a thread.sleep method call.

Bill Ross

unread,
Sep 21, 2011, 2:59:00 AM9/21/11
to webd...@googlegroups.com
That doesn't look so bad, you are leveraging the minimal tools that
webdriver gives you admirably. The only improvement might be to wait
more busily. Assuming on average that would save you half a second,
that's a 10% speedup on your 5 sec, at the expense of a few cpu
cycles. Nowadays I'd say the eventual heat to the planet might not
be worth the speed in a test app.

Bill

---

Luke Inman-Semerau

unread,
Sep 21, 2011, 12:51:03 PM9/21/11
to webd...@googlegroups.com
That is one of the oddest leaps I've heard of... save electricity by saving a few cpu cycles, thus slowing your execution time of your test. I'm all for cutting down on electrical consumption... but really? Just tell him to turn off the computer when he leaves work ;)

Jerald, Here's how I would do the same with a WebDriverWait:

private Boolean checkIfPopupWindowClosed() {
  try {
    return (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver d) {
            return d.getWindowHandles().size() < 2;
        }
    });
  } catch (TimeoutException e) {
      return false;
  }
}

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.


Jerald paul

unread,
Sep 26, 2011, 5:14:20 AM9/26/11
to webd...@googlegroups.com
Thanks luke. This is working well, i will look into these two classes WebDriverWait and ExpectedCondition.
Regards,
Jerald
Reply all
Reply to author
Forward
0 new messages