private void closeNewWindows(WebDriver webDriver, String currentWindowHandle, Set<String> currentWindows) { Set<String> windowHandles = webDriver.getWindowHandles(); if (windowHandles.size() != currentWindows.size()) { Set<String> newWindows = new HashSet<>(windowHandles); newWindows.removeAll(currentWindows); log.info("File has been opened in a new window, let's close " + newWindows.size() + " new windows"); for (String newWindow : newWindows) { log.info(" Let's close " + newWindow); try { webDriver.switchTo().window(newWindow); webDriver.close(); } catch (NoSuchWindowException windowHasBeenClosedMeanwhile) { log.info(" Failed to close " + newWindow + ": " + Cleanup.of.webdriverExceptionMessage(windowHasBeenClosedMeanwhile)); } catch (Exception e) { log.warning(" Failed to close " + newWindow + ": " + e); } } webDriver.switchTo().window(currentWindowHandle); } }
@Override public WebDriver apply(WebDriver driver) { try { List<String> windowHandles = new ArrayList<>(driver.getWindowHandles()); return driver.switchTo().window(windowHandles.get(index)); } catch (IndexOutOfBoundsException windowWithIndexNotFound) { return null; } }
/** * Switch to window/tab by name/handle/title except some windows handles * @param title title of window/tab */ protected static WebDriver windowByTitle(WebDriver driver, String title) { Set<String> windowHandles = driver.getWindowHandles(); for (String windowHandle : windowHandles) { driver.switchTo().window(windowHandle); if (title.equals(driver.getTitle())) { return driver; } } throw new NoSuchWindowException("Window with title not found: " + title); }
public static void executeForLink(WebElement link, Runnable function) { // Open link in new tab and execute code when in this tab String oldWindowHandle = openInNewTabAndFocus(link); function.run(); driver().close(); driver().switchTo().window(oldWindowHandle); }
@Init public void init() { origHandle = driver.getWindowHandle(); driver.switchTo().window(ICON_PICKER_WINDOW); table = finder.find(By.id("icon-picker-table")); iconInput = finder.find(By.name("iconurl")); updateButton = finder.find(By.id("icon-picker-submit")); }
/** * Switch to this window. Future webdriver commands * will be done in this window * * @return the new active window */ public BrowserWindow switchTo() { checkIsOpen(); driver.switchTo().window(windowName); return this; }
/** * Clicks the user picker button and switches to the user picker popup window */ public UserPickerPopup chooseUser() { userPickerLink.click(); driver.switchTo().window(UserPickerPopup.POPUP_WINDOW_NAME); return pageBinder.bind(UserPickerPopup.class); }
@Override public void switchToDefaultWindow() { WindowHandle handle = windowHandleRegistry.getDefaultHandle(); driver.switchTo().window(handle.getWindowHandle()); }
/** * Closes this window. Remember to {@link com.atlassian.jira.pageobjects.WindowSession#switchToDefault()} * after this. * * @return parent window session */ public WindowSession close() { driver.switchTo().window(windowName); driver.executeScript("self.close()"); return switchBack(); }
public WindowSession switchToDefault() { driver.switchTo().window(defaultWindow); return this; }
/** * Clicks on the choose user button to select users to grant permission to. */ public UserPickerPopup clickChooseUser() { userPickerPopupButton.click(); driver.switchTo().window(UserPickerPopup.POPUP_WINDOW_NAME); return pageBinder.bind(UserPickerPopup.class); }
public void submitIconUrl(String url) { iconInput.clear(); if (StringUtils.isNotBlank(url)) { iconInput.type(url); } updateButton.click(); driver.switchTo().window(origHandle); } }
public static void switchBackAfterPopup(WebDriver driver) { try { Set<String> beforeHandles = windows.get((driver).hashCode()); String newWindowHandle = beforeHandles.iterator().next(); driver.switchTo().window(newWindowHandle); } catch (Exception e) { LOGGER.warn("Switching to bottom window was not performed!"); } }
public void switchWindow() throws NoSuchWindowException { WebDriver drv = getDriver(); Set<String> handles = drv.getWindowHandles(); String current = drv.getWindowHandle(); if (handles.size() > 1) { handles.remove(current); } String newTab = handles.iterator().next(); drv.switchTo().window(newTab); }
driver.switchTo().window("Test window");
@Override public void forceFocusOnCurrentWindow() { webDriver.switchTo().window(webDriver.getWindowHandle()); }
/** * Switches to new browser window for browsing. */ public void switchToNewWindow() { String curWin = driver.getWindowHandle(); for (String handle : driver.getWindowHandles()) { if (!handle.equals(curWin) && !windowHandles.contains(curWin)) { windowHandles.push(curWin); driver.switchTo().window(handle); break; } } }
@Test public void windowListenerTest() { try { driver.switchTo().window("Test window"); assertThat(listener.messages, hasItems(WEBDRIVER_EVENT_LISTENER + "Attempt to switch to window Test window", WEBDRIVER_EVENT_LISTENER + "driver is switched to window Test window")); } finally { listener.messages.clear(); } } }
/** * Closes the current browser window and switches back to the last window used previously. */ public void closeCurrentWindowAndSwitchToParentWindow() { driver.close(); driver.switchTo().window(windowHandles.pop()); }
@Override public WebDriver apply(WebDriver driver) { try { return driver.switchTo().window(nameOrHandleOrTitle); } catch (NoSuchWindowException windowWithNameOrHandleNotFound) { try { return windowByTitle(driver, nameOrHandleOrTitle); } catch (NoSuchWindowException e) { return null; } } }