Sunday, July 12, 2015

Get Control on the window

  • Switching control between two windows
  • We need to make use of window ID(dynamic in nature, has around 30 alphanumerica character)
  • driver.switchTo().window(driver.getWindowHandles().toArray()[1].toString());
  • File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
          FileUtils.copyFile(src, new File("<path>"));

Note: take the control of the window, using window id and take screenshot to make sure on which window we had control.

  • From Episteme:

  1. Basic idea to handle window is, once new window open get the number of window & switch to child window.
  2. Do some operation & close the session using driver.close.(In your case window is closing automatically but may not current session ended)
  3. Once close the window, check once again how many windows open & switch to current window using name or index
Step 1: Get the parent window handle using below statement.
String parentwindow = driver.getWindowHandle();
Step 2: Clickon link to open modal window.
Step 3: Iterate through all opened windows with help of for each loop.
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
popup=driver.switchTo().window(winHandle);
if(popup.getTitle().equals(“title of the window to which you have to switch”))
{
….
break;
}
}
Sleeper.sleepTightInSeconds(2);
driver.close();
Sleeper.sleepTightInSeconds(5);
driver.switchTo().window(parentwindow);
Step 4: Trying to switch back to parent window using below statement.
driver.switchTo().window(ParentWin);

No comments:

Post a Comment