SwitchWindowAction extends AbstractSeleniumAction implements SeleniumAction { @Override protected void execute(SeleniumBrowser browser, TestContext context) { if (!context.getVariables().containsKey(windowName)) { throw new CitrusRuntimeException("Failed to find window handle for window " + windowName); } String targetWindow = context.getVariable(windowName); Set<String> handles = browser.getWebDriver().getWindowHandles(); if (!handles.contains(targetWindow)) { throw new CitrusRuntimeException("Failed to find window for handle " + context.getVariable(windowName)); } String lastWindow = browser.getWebDriver().getWindowHandle(); if (!lastWindow.equals(targetWindow)) { context.setVariable(SeleniumHeaders.SELENIUM_LAST_WINDOW, lastWindow); browser.getWebDriver().switchTo().window(targetWindow); log.info("Switch window focus to " + windowName); context.setVariable(SeleniumHeaders.SELENIUM_ACTIVE_WINDOW, targetWindow); } else { log.info("Skip switch window action as window is already focused"); } } SwitchWindowAction(Builder builder); String getWindowName();  }
@Test public void testSwitchToActiveWindow() throws Exception { Set<String> windows = new HashSet<>(); windows.add("active_window"); windows.add("last_window"); windows.add("other_window"); when(webDriver.getWindowHandles()).thenReturn(windows); when(webDriver.getWindowHandle()).thenReturn("active_window"); context.setVariable(SeleniumHeaders.SELENIUM_LAST_WINDOW, "last_window"); context.setVariable(SeleniumHeaders.SELENIUM_ACTIVE_WINDOW, "active_window"); SwitchWindowAction action = new SwitchWindowAction.Builder() .browser(seleniumBrowser) .build(); action.execute(context); Assert.assertEquals(context.getVariable(SeleniumHeaders.SELENIUM_LAST_WINDOW), "last_window"); Assert.assertEquals(context.getVariable(SeleniumHeaders.SELENIUM_ACTIVE_WINDOW), "active_window"); verify(locator, times(0)).window(anyString()); }
@Test public void testSwitchWindow() throws Exception { Set<String> windows = new HashSet<>(); windows.add("active_window"); windows.add("other_window"); when(webDriver.getWindowHandles()).thenReturn(windows); when(webDriver.getWindowHandle()).thenReturn("active_window"); context.setVariable(SeleniumHeaders.SELENIUM_ACTIVE_WINDOW, "active_window"); context.setVariable("myWindow", "other_window"); SwitchWindowAction action = new SwitchWindowAction.Builder() .browser(seleniumBrowser) .window("myWindow") .build(); action.execute(context); Assert.assertEquals(context.getVariable(SeleniumHeaders.SELENIUM_LAST_WINDOW), "active_window"); Assert.assertEquals(context.getVariable(SeleniumHeaders.SELENIUM_ACTIVE_WINDOW), "other_window"); verify(locator).window("other_window"); }
@Test(expectedExceptions = CitrusRuntimeException.class, expectedExceptionsMessageRegExp = "Failed to find window.*") public void testSwitchWindowNotFound() throws Exception { Set<String> windows = new HashSet<>(); windows.add("active_window"); when(webDriver.getWindowHandles()).thenReturn(windows); when(webDriver.getWindowHandle()).thenReturn("active_window"); context.setVariable(SeleniumHeaders.SELENIUM_ACTIVE_WINDOW, "active_window"); context.setVariable("myWindow", "other_window"); SwitchWindowAction action = new SwitchWindowAction.Builder() .browser(seleniumBrowser) .window("myWindow") .build(); action.execute(context); }