A headless browser intended for use in testing web-based applications.
—
Browser window and frame management for handling pop-ups, iframes, modal dialogs, and multi-window scenarios. Essential for complex web application navigation and testing applications with multiple windows or frames.
Base interface for all browser window types including top-level windows, frames, and dialogs.
/**
* Base interface for browser windows and frames
*/
public interface WebWindow {
/** Get window name */
public String getName();
/** Set window name */
public void setName(String name);
/** Get current page in window */
public Page getEnclosedPage();
/** Set page in window */
public void setEnclosedPage(Page page);
/** Get parent WebClient */
public WebClient getWebClient();
/** Get parent window (null for top-level) */
public WebWindow getParentWindow();
/** Get top-level window */
public WebWindow getTopWindow();
/** Get window history */
public History getHistory();
/** Get inner window width */
public int getInnerWidth();
/** Set inner window width */
public void setInnerWidth(int innerWidth);
/** Get inner window height */
public int getInnerHeight();
/** Set inner window height */
public void setInnerHeight(int innerHeight);
/** Get outer window width */
public int getOuterWidth();
/** Set outer window width */
public void setOuterWidth(int outerWidth);
/** Get outer window height */
public int getOuterHeight();
/** Set outer window height */
public void setOuterHeight(int outerHeight);
/** Get window job manager for JavaScript */
public JavaScriptJobManager getJobManager();
/** Initialize window */
public void initialize();
/** Check if window is closed */
public boolean isClosed();
/** Close window */
public void close();
}Usage Examples:
import com.gargoylesoftware.htmlunit.*;
try (WebClient webClient = new WebClient()) {
// Get current window
WebWindow currentWindow = webClient.getCurrentWindow();
System.out.println("Current window: " + currentWindow.getName());
// Load page in current window
HtmlPage page = webClient.getPage("https://example.com");
// Window properties
currentWindow.setName("mainWindow");
currentWindow.setInnerWidth(1024);
currentWindow.setInnerHeight(768);
System.out.println("Window size: " + currentWindow.getInnerWidth() + "x" + currentWindow.getInnerHeight());
System.out.println("Page URL: " + currentWindow.getEnclosedPage().getUrl());
}Implementation for top-level browser windows.
/**
* Top-level browser window implementation
*/
public class TopLevelWindow extends WebWindowImpl {
/** Create top-level window */
public TopLevelWindow(String name, WebClient webClient);
/** Get window opener (if opened by another window) */
public WebWindow getOpener();
/** Set window opener */
public void setOpener(WebWindow opener);
/** Check if window can be closed */
public boolean isCloseable();
/** Set window closeable state */
public void setCloseable(boolean closeable);
/** Get window status text */
public String getStatus();
/** Set window status text */
public void setStatus(String status);
/** Check if window is maximized */
public boolean isMaximized();
/** Maximize window */
public void maximize();
/** Minimize window */
public void minimize();
/** Restore window */
public void restore();
/** Move window to position */
public void moveTo(int x, int y);
/** Move window by offset */
public void moveBy(int deltaX, int deltaY);
/** Resize window to size */
public void resizeTo(int width, int height);
/** Resize window by delta */
public void resizeBy(int deltaWidth, int deltaHeight);
/** Scroll window to position */
public void scrollTo(int x, int y);
/** Scroll window by offset */
public void scrollBy(int deltaX, int deltaY);
/** Get window position X */
public int getLeft();
/** Set window position X */
public void setLeft(int left);
/** Get window position Y */
public int getTop();
/** Set window position Y */
public void setTop(int top);
}Implementation for modal dialog windows.
/**
* Modal dialog window implementation
*/
public class DialogWindow extends WebWindowImpl {
/** Create dialog window */
public DialogWindow(WebClient webClient, WebWindow opener, Object dialogArguments);
/** Get window that opened this dialog */
public WebWindow getOpener();
/** Get dialog arguments passed when opened */
public Object getDialogArguments();
/** Get dialog return value */
public Object getReturnValue();
/** Set dialog return value */
public void setReturnValue(Object returnValue);
/** Close dialog with return value */
public void close(Object returnValue);
/** Check if dialog is modal */
public boolean isModal();
/** Check if dialog blocks parent window */
public boolean isBlocking();
}Implementation for frame and iframe windows.
/**
* Frame window implementation for <frame> and <iframe> elements
*/
public class FrameWindow extends WebWindowImpl {
/** Create frame window */
public FrameWindow(BaseFrameElement frameElement);
/** Get frame element that contains this window */
public BaseFrameElement getFrameElement();
/** Get frame name from element */
public String getFrameName();
/** Get frame source URL */
public String getFrameSource();
/** Check if frame content is accessible (same origin) */
public boolean isContentAccessible();
/** Get frame border width */
public int getFrameBorder();
/** Get frame margin width */
public int getMarginWidth();
/** Get frame margin height */
public int getMarginHeight();
/** Check if frame allows scrolling */
public boolean isScrollingAllowed();
/** Check if frame is resizable */
public boolean isResizable();
}Usage Examples:
// Working with frames
HtmlPage page = webClient.getPage("https://example.com/frames.html");
// Find frame by name
WebWindow frameWindow = webClient.getWebWindowByName("contentFrame");
if (frameWindow instanceof FrameWindow) {
FrameWindow frame = (FrameWindow) frameWindow;
// Get content from frame
Page framePage = frame.getEnclosedPage();
if (framePage instanceof HtmlPage) {
HtmlPage frameHtmlPage = (HtmlPage) framePage;
String frameContent = frameHtmlPage.asText();
System.out.println("Frame content: " + frameContent);
}
// Check frame properties
System.out.println("Frame source: " + frame.getFrameSource());
System.out.println("Frame accessible: " + frame.isContentAccessible());
}
// Find iframe element and get its window
HtmlInlineFrame iframe = (HtmlInlineFrame) page.getElementById("myIframe");
FrameWindow iframeWindow = (FrameWindow) iframe.getContentWindow();
Page iframePage = iframeWindow.getEnclosedPage();Browser history management for back/forward navigation.
/**
* Browser history for window navigation
*/
public class History {
/** Get history length */
public int getLength();
/** Navigate back one page */
public void back() throws IOException;
/** Navigate forward one page */
public void forward() throws IOException;
/** Navigate by relative index */
public void go(int relativeIndex) throws IOException;
/** Get current history index */
public int getIndex();
/** Get history entry at index */
public HistoryEntry getEntry(int index);
/** Get all history entries */
public List<HistoryEntry> getEntries();
/** Clear history */
public void clear();
/** Get URL at history index */
public URL getUrl(int index);
/** Get current URL */
public URL getCurrentUrl();
/** Check if can go back */
public boolean canGoBack();
/** Check if can go forward */
public boolean canGoForward();
/** Push state to history (HTML5) */
public void pushState(Object data, String title, String url);
/** Replace current state (HTML5) */
public void replaceState(Object data, String title, String url);
/** Get current state object */
public Object getCurrentState();
}
/**
* Individual history entry
*/
public class HistoryEntry {
/** Get entry URL */
public URL getUrl();
/** Get entry title */
public String getTitle();
/** Get state object */
public Object getState();
/** Get timestamp */
public Date getTimestamp();
}Usage Examples:
// History navigation
try (WebClient webClient = new WebClient()) {
WebWindow window = webClient.getCurrentWindow();
History history = window.getHistory();
// Navigate through pages
webClient.getPage("https://example.com/page1");
webClient.getPage("https://example.com/page2");
webClient.getPage("https://example.com/page3");
System.out.println("History length: " + history.getLength());
System.out.println("Current URL: " + history.getCurrentUrl());
// Navigate back
if (history.canGoBack()) {
history.back();
System.out.println("After back: " + history.getCurrentUrl());
}
// Navigate forward
if (history.canGoForward()) {
history.forward();
System.out.println("After forward: " + history.getCurrentUrl());
}
// Go to specific history index
history.go(-2); // Go back 2 pages
// Clear history
history.clear();
}/**
* WebClient methods for window management
*/
public class WebClient implements AutoCloseable {
/** Get current active window */
public WebWindow getCurrentWindow();
/** Set current active window */
public void setCurrentWindow(WebWindow window);
/** Open new window with URL */
public WebWindow openWindow(URL url, String windowName) throws IOException;
/** Open new window with target */
public WebWindow openWindow(URL url, String windowName, String windowFeatures) throws IOException;
/** Open modal dialog window */
public DialogWindow openDialogWindow(URL url, WebWindow opener, Object dialogArguments) throws IOException;
/** Find window by name */
public WebWindow getWebWindowByName(String name);
/** Get all open windows */
public List<WebWindow> getWindows();
/** Close all windows */
public void closeAllWindows();
/** Get default window for new content */
public WebWindow getDefaultWindow();
}Usage Examples:
// Open new windows
try (WebClient webClient = new WebClient()) {
// Open new window
WebWindow newWindow = webClient.openWindow(new URL("https://example.com/popup"), "popupWindow");
// Switch to new window
webClient.setCurrentWindow(newWindow);
// Load page in new window
HtmlPage popupPage = webClient.getPage("https://example.com/popup-content");
// Open modal dialog
DialogWindow dialog = webClient.openDialogWindow(
new URL("https://example.com/dialog"),
webClient.getCurrentWindow(),
"dialog arguments"
);
// Work with dialog
HtmlPage dialogPage = (HtmlPage) dialog.getEnclosedPage();
// Close dialog with return value
dialog.close("dialog result");
// Find window by name
WebWindow foundWindow = webClient.getWebWindowByName("popupWindow");
if (foundWindow != null) {
System.out.println("Found window: " + foundWindow.getName());
}
// List all windows
List<WebWindow> allWindows = webClient.getWindows();
System.out.println("Total windows: " + allWindows.size());
for (WebWindow window : allWindows) {
System.out.println("Window: " + window.getName() + " - " + window.getEnclosedPage().getUrl());
}
}HTML frame elements that create frame windows.
/**
* Base class for frame elements
*/
public abstract class BaseFrameElement extends HtmlElement {
/** Get frame content window */
public abstract FrameWindow getContentWindow();
/** Get frame name */
public String getNameAttribute();
/** Get frame source URL */
public String getSrcAttribute();
/** Set frame source URL */
public void setSrcAttribute(String src);
/** Check if frame is loading */
public boolean isContentLoading();
/** Wait for frame content to load */
public void waitForContentToLoad(long timeoutMillis) throws InterruptedException;
/** Get frame border */
public String getFrameBorderAttribute();
/** Get margin width */
public String getMarginWidthAttribute();
/** Get margin height */
public String getMarginHeightAttribute();
/** Get scrolling setting */
public String getScrollingAttribute();
}
/**
* Frame element (<frame>)
*/
public class HtmlFrame extends BaseFrameElement {
/** Check if frame is resizable */
public boolean isResizable();
/** Get frame resize setting */
public String getNoResizeAttribute();
}
/**
* Inline frame element (<iframe>)
*/
public class HtmlInlineFrame extends BaseFrameElement {
/** Get iframe width */
public String getWidthAttribute();
/** Get iframe height */
public String getHeightAttribute();
/** Get iframe align */
public String getAlignAttribute();
/** Get iframe longdesc */
public String getLongDescAttribute();
/** Check if iframe is seamless */
public boolean isSeamless();
/** Get iframe sandbox */
public String getSandboxAttribute();
/** Get iframe srcdoc */
public String getSrcdocAttribute();
/** Check if frame allows fullscreen */
public boolean isAllowFullscreen();
}/**
* Window event handling
*/
public interface WindowEventHandler {
/** Handle window opened event */
void onWindowOpened(WebWindow window);
/** Handle window closed event */
void onWindowClosed(WebWindow window);
/** Handle window focus event */
void onWindowFocused(WebWindow window);
/** Handle window blur event */
void onWindowBlurred(WebWindow window);
}
/**
* Status handler for window status updates
*/
public interface StatusHandler {
/** Handle status message update */
void statusMessageChanged(Page page, String message);
}/**
* Methods for cross-frame communication and access
*/
public class WebWindow {
/** Post message to another window (HTML5) */
public void postMessage(String message, String targetOrigin, WebWindow targetWindow);
/** Get window by frame reference */
public WebWindow getFrameByName(String frameName);
/** Get parent frame window */
public WebWindow getParent();
/** Get top-level window */
public WebWindow getTop();
/** Check if windows are same origin */
public boolean isSameOrigin(WebWindow otherWindow);
/** Check if window can access another window */
public boolean canAccessWindow(WebWindow otherWindow);
}Usage Examples:
// Cross-frame communication
HtmlPage mainPage = webClient.getPage("https://example.com/frameset.html");
// Access frame by name
WebWindow frameWindow = webClient.getWebWindowByName("contentFrame");
if (frameWindow != null) {
HtmlPage framePage = (HtmlPage) frameWindow.getEnclosedPage();
// Execute JavaScript in frame
ScriptResult result = framePage.executeJavaScript("document.title");
System.out.println("Frame title: " + result.getJavaScriptResult());
// Check frame relationships
WebWindow parentWindow = frameWindow.getParentWindow();
WebWindow topWindow = frameWindow.getTopWindow();
System.out.println("Frame parent: " + parentWindow.getName());
System.out.println("Frame top: " + topWindow.getName());
// Check same origin
boolean sameOrigin = frameWindow.isSameOrigin(parentWindow);
System.out.println("Same origin: " + sameOrigin);
}
// Handle multiple windows
List<WebWindow> windows = webClient.getWindows();
for (WebWindow window : windows) {
if (window instanceof TopLevelWindow) {
TopLevelWindow topWindow = (TopLevelWindow) window;
System.out.println("Top-level window: " + topWindow.getName());
} else if (window instanceof DialogWindow) {
DialogWindow dialog = (DialogWindow) window;
System.out.println("Dialog window with args: " + dialog.getDialogArguments());
} else if (window instanceof FrameWindow) {
FrameWindow frame = (FrameWindow) window;
System.out.println("Frame window: " + frame.getFrameName());
}
}Install with Tessl CLI
npx tessl i tessl/maven-net-sourceforge-htmlunit--htmlunit