CloseWindowAction 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); } Set<String> handles = browser.getWebDriver().getWindowHandles(); if (!handles.contains(context.getVariable(windowName))) { throw new CitrusRuntimeException("Failed to find window for handle " + context.getVariable(windowName)); } log.info("Current window: " + browser.getWebDriver().getWindowHandle()); log.info("Window to close: " + context.getVariable(windowName)); if (browser.getWebDriver().getWindowHandle().equals((context.getVariable(windowName)))) { browser.getWebDriver().close(); log.info("Switch back to main window!"); if (context.getVariables().containsKey(SeleniumHeaders.SELENIUM_LAST_WINDOW)) { browser.getWebDriver().switchTo().window(context.getVariable(SeleniumHeaders.SELENIUM_LAST_WINDOW)); context.setVariable(SeleniumHeaders.SELENIUM_ACTIVE_WINDOW, context.getVariable(SeleniumHeaders.SELENIUM_LAST_WINDOW)); } else { browser.getWebDriver().switchTo().defaultContent(); context.setVariable(SeleniumHeaders.SELENIUM_ACTIVE_WINDOW, browser.getWebDriver().getWindowHandle()); } } else { String activeWindow = browser.getWebDriver().getWindowHandle(); browser.getWebDriver().switchTo().window(context.getVariable(windowName)); browser.getWebDriver().close(); if (context.getVariables().containsKey(SeleniumHeaders.SELENIUM_ACTIVE_WINDOW)) { browser.getWebDriver().switchTo().window(context.getVariable(SeleniumHeaders.SELENIUM_ACTIVE_WINDOW)); } else { browser.getWebDriver().switchTo().window(activeWindow); context.setVariable(SeleniumHeaders.SELENIUM_ACTIVE_WINDOW, activeWindow); } } } CloseWindowAction(Builder builder); String getWindowName();  }
@Test public void testCloseActiveWindow() throws Exception { Set<String> windows = new HashSet<>(); windows.add("active_window"); windows.add("last_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"); CloseWindowAction action = new CloseWindowAction.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), "last_window"); verify(webDriver).close(); verify(locator).window("last_window"); }
@Test public void testCloseActiveWindowReturnToDefault() throws Exception { Set<String> windows = new HashSet<>(); windows.add("active_window"); windows.add("main_window"); when(webDriver.getWindowHandles()).thenReturn(windows); when(webDriver.getWindowHandle()).thenReturn("active_window").thenReturn("active_window").thenReturn("main_window"); context.setVariable(SeleniumHeaders.SELENIUM_ACTIVE_WINDOW, "active_window"); CloseWindowAction action = new CloseWindowAction.Builder() .browser(seleniumBrowser) .build(); action.execute(context); Assert.assertFalse(context.getVariables().containsKey(SeleniumHeaders.SELENIUM_LAST_WINDOW)); Assert.assertEquals(context.getVariable(SeleniumHeaders.SELENIUM_ACTIVE_WINDOW), "main_window"); verify(webDriver).close(); verify(locator).defaultContent(); }
@Test public void testCloseOtherWindow() 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"); context.setVariable("myWindow", "other_window"); CloseWindowAction action = new CloseWindowAction.Builder() .browser(seleniumBrowser) .window("myWindow") .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(webDriver).close(); verify(locator).window("other_window"); verify(locator).window("active_window"); }
@Test public void testCloseOtherWindowNoActiveWindow() 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("myWindow", "other_window"); CloseWindowAction action = new CloseWindowAction.Builder() .browser(seleniumBrowser) .window("myWindow") .build(); action.execute(context); Assert.assertFalse(context.getVariables().containsKey(SeleniumHeaders.SELENIUM_LAST_WINDOW)); Assert.assertEquals(context.getVariable(SeleniumHeaders.SELENIUM_ACTIVE_WINDOW), "active_window"); verify(webDriver).close(); verify(locator).window("other_window"); verify(locator).window("active_window"); }
@Test(expectedExceptions = CitrusRuntimeException.class, expectedExceptionsMessageRegExp = "Failed to find window handle.*") public void testCloseWindowInvalidWindowName() throws Exception { CloseWindowAction action = new CloseWindowAction.Builder() .browser(seleniumBrowser) .window("myWindow") .build(); action.execute(context); }
@Test(expectedExceptions = CitrusRuntimeException.class, expectedExceptionsMessageRegExp = "Failed to find window.*") public void testCloseWindowNotFound() throws Exception { Set<String> windows = new HashSet<>(); windows.add("active_window"); windows.add("last_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"); context.setVariable("myWindow", "other_window"); CloseWindowAction action = new CloseWindowAction.Builder() .browser(seleniumBrowser) .window("myWindow") .build(); action.execute(context); }