/** * Closes the current window. */ void close() { queues.remove(driver.getCurrentWindow()); }
@Override public WebDriver defaultContent() { switchToDefaultContentOfWindow(getCurrentWindow().getTopWindow()); return HtmlUnitDriver.this; }
Queue<String> getCurrentQueue() { return queues.get(driver.getCurrentWindow().getEnclosedPage()); }
protected Page lastPage() { getWebClient(); // check that session is active return getCurrentWindow().getEnclosedPage(); }
@Override public String getWindowHandle() { WebWindow topWindow = getCurrentWindow().getTopWindow(); if (topWindow.isClosed()) { throw new NoSuchWindowException("Window is closed"); } return String.valueOf(System.identityHashCode(topWindow)); }
@Override public void forward() { try { getCurrentWindow().getHistory().forward(); } catch (IOException e) { throw new WebDriverException(e); } }
@Override public void back() { try { getCurrentWindow().getHistory().back(); } catch (IOException e) { throw new WebDriverException(e); } }
@Override public String getCurrentUrl() { getWebClient(); // check that session is active Page page = getCurrentWindow().getTopWindow().getEnclosedPage(); if (page == null) { return null; } URL url = page.getUrl(); if (url == null) { return null; } return url.toString(); }
@Override public String getTitle() { if (alert.getCurrentQueue() != null && alert.getCurrentQueue().peek() != null) { throw new UnhandledAlertException("Alert found", alert.getCurrentQueue().peek()); } Page page = lastPage(); if (page == null || !(page instanceof HtmlPage)) { return null; // no page so there is no title } if (getCurrentWindow() instanceof FrameWindow) { page = getCurrentWindow().getTopWindow().getEnclosedPage(); } return ((HtmlPage) page).getTitleText(); }
@Override public Dimension getSize() { WebWindow topWindow = getCurrentWindow().getTopWindow(); return new Dimension(topWindow.getOuterWidth(), topWindow.getOuterHeight()); }
/** * Allows HtmlUnit's about:blank to be loaded in the constructor, and may be useful for other * tests? * * @param fullUrl The URL to visit */ protected void get(URL fullUrl) { try { getWebClient().getPage(fullUrl); // A "get" works over the entire page currentWindow = getCurrentWindow().getTopWindow(); } catch (UnknownHostException e) { getCurrentWindow().getTopWindow().setEnclosedPage(new UnexpectedPage( new StringWebResponse("Unknown host", fullUrl), getCurrentWindow().getTopWindow() )); } catch (ConnectException e) { // This might be expected } catch (SocketTimeoutException e) { throw new TimeoutException(e); } catch (Exception e) { throw new WebDriverException(e); } gotPage = true; pickWindow(); resetKeyboardAndMouseState(); }
@Override public void close() { getWebClient(); // check that session is active WebWindow thisWindow = getCurrentWindow(); // check that the current window is active if (getWebClient().getWebWindows().size() == 1) { // closing the last window is equivalent to quit quit(); } else { if (thisWindow != null) { alert.close(); ((TopLevelWindow) thisWindow.getTopWindow()).close(); } if (getWebClient().getWebWindows().size() == 0) { quit(); } } }
@Override public void setSize(Dimension targetSize) { WebWindow topWindow = getCurrentWindow().getTopWindow(); int width = targetSize.getWidth(); if (width < SCROLLBAR_WIDTH) width = SCROLLBAR_WIDTH; topWindow.setOuterWidth(width); topWindow.setInnerWidth(width - SCROLLBAR_WIDTH); int height = targetSize.getHeight(); if (height < HEADER_HEIGHT) height = HEADER_HEIGHT; topWindow.setOuterHeight(height); topWindow.setInnerHeight(height - HEADER_HEIGHT); }
@Override public Object executeScript(String script, final Object... args) { HtmlPage page = getPageToInjectScriptInto(); script = "function() {" + script + "\n};"; ScriptResult result = page.executeJavaScript(script); Function func = (Function) result.getJavaScriptResult(); Object[] parameters = convertScriptArgs(page, args); try { result = page.executeJavaScriptFunctionIfPossible( func, (ScriptableObject) getCurrentWindow().getScriptObject(), parameters, page.getDocumentElement()); } catch (Throwable ex) { throw new WebDriverException(ex); } return parseNativeJavascriptResult(result); }