edszr
edszr

Reputation: 27

Selenium IDE window focus in Internet Explorer

I'm using selenium IDE and webdriver to test a web page in Internet Explorer. I discovered a while back that IE will not fully accept commands from Selenium if it's not the window in focus. For example, if the Selenium IDE window is in focus, and the command is to click a button in IE, the button will push down, but it won't let go.

With that in mind, my test involves popping up a window, doing a few things in it, leaving it open and returning to the null window to do a few things, then returning to the popup for a few more commands.

Is there a way I can make the null window come forward (over the popup) when I need to execute the commands for the null window? And then vice versa, can I make the popup then come forward when I need to return to it? I tried using windowFocus, but that did not work.

Upvotes: 1

Views: 1828

Answers (2)

edszr
edszr

Reputation: 27

I remembered that webdriver sometimes acts differently than running non-webdriver tests. It turns out that using windowSelect followed by windowFocus switches between windows when running webdriver tests.

Upvotes: 1

Brandon
Brandon

Reputation: 645

Use the SwitchTo() method and the TargetLocator Interface in Selenium.

A really simple example would look like this:

// Switch to new window
public String SwitchToNewWindow()
{
   // Get the original window handle
   String winHandleBefore = driver.getWindowHandle();

   foreach(String winHandle in driver.getWindowHandles())
   {
      driver.switchTo().window(winHandle);
   }
   return Constants.KEYWORD_PASS;
}

// Switch back to original window
public String switchwindowback()
{
   String winHandleBefore = driver.getWindowHandle();
   driver.close(); 
   //Switch back to original browser (first window)
   driver.switchTo().window(winHandleBefore);
   //continue with original browser (first window)
   return Constants.KEYWORD_PASS;
}

Upvotes: 4

Related Questions