CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-htmlunit--htmlunit

A headless browser for Java programs that provides web automation, form handling, JavaScript execution, and DOM manipulation capabilities.

Pending
Overview
Eval results
Files

windows.mddocs/

Window Management

Browser window and frame management providing multiple window handling, window navigation, frame interactions, and popup control for comprehensive browser simulation.

Capabilities

WebWindow Interface

Base interface for all window types providing common window functionality.

public interface WebWindow {
    /**
     * Get the page currently displayed in this window
     * @return the Page object, or null if no page is loaded
     */
    public Page getEnclosedPage();
    
    /**
     * Set the page to display in this window
     * @param page the Page to display
     */
    public void setEnclosedPage(Page page);
    
    /**
     * Get the window name (target name for links and forms)
     * @return the window name, or empty string if unnamed
     */
    public String getName();
    
    /**
     * Set the window name
     * @param name the new window name
     */
    public void setName(String name);
    
    /**
     * Get the parent window (null for top-level windows)
     * @return parent WebWindow, or null if this is a top-level window
     */
    public WebWindow getParentWindow();
    
    /**
     * Get the top-level window in the window hierarchy
     * @return the root WebWindow in the hierarchy
     */
    public WebWindow getTopWindow();
    
    /**
     * Get the WebClient that owns this window
     * @return the WebClient instance
     */
    public WebClient getWebClient();
    
    /**
     * Get the browser history for this window
     * @return History object for navigation
     */
    public History getHistory();
    
    /**
     * Get the current window dimensions
     * @return Dimension object with width and height
     */
    public Dimension getInnerSize();
    
    /**
     * Set the window dimensions
     * @param dimension new window size
     */
    public void setInnerSize(Dimension dimension);
    
    /**
     * Check if this window is closed
     * @return true if the window has been closed
     */
    public boolean isClosed();
}

Usage Examples:

WebClient webClient = new WebClient();
HtmlPage page = webClient.getPage("https://example.com");

// Get current window
WebWindow currentWindow = webClient.getCurrentWindow();

// Window properties
String windowName = currentWindow.getName();
WebWindow parentWindow = currentWindow.getParentWindow();
WebWindow topWindow = currentWindow.getTopWindow();
Page enclosedPage = currentWindow.getEnclosedPage();

// Window dimensions
Dimension size = currentWindow.getInnerSize();
System.out.println("Window size: " + size.width + "x" + size.height);

TopLevelWindow

Main browser window implementation for primary browser windows.

public class TopLevelWindow implements WebWindow {
    /**
     * Create a new top-level window
     * @param name the window name
     * @param webClient the WebClient that owns this window
     */
    public TopLevelWindow(String name, WebClient webClient);
    
    /**
     * Open a new window with the specified URL
     * @param url the URL to load in the new window
     * @param windowName the name for the new window
     * @return the newly created WebWindow
     * @throws IOException if window creation fails
     */
    public WebWindow openWindow(URL url, String windowName) throws IOException;
    
    /**
     * Close this window
     */
    public void close();
    
    /**
     * Get all child windows (frames) of this window
     * @return List of child WebWindow objects
     */
    public List<WebWindow> getChildWindows();
    
    /**
     * Set the window position on screen
     * @param x horizontal position
     * @param y vertical position
     */
    public void setOuterPosition(int x, int y);
    
    /**
     * Get the window position
     * @return Point object with x,y coordinates
     */
    public Point getOuterPosition();
}

Usage Examples:

WebClient webClient = new WebClient();

// Create named window
TopLevelWindow mainWindow = new TopLevelWindow("main", webClient);
webClient.setCurrentWindow(mainWindow);

// Load page in window
mainWindow.setEnclosedPage(webClient.getPage("https://example.com"));

// Window positioning
mainWindow.setOuterPosition(100, 50);
Point position = mainWindow.getOuterPosition();

// Open popup window
WebWindow popupWindow = mainWindow.openWindow(
    new URL("https://example.com/popup"), 
    "popup"
);

FrameWindow

Frame and iframe window implementation for handling embedded content.

public class FrameWindow implements WebWindow {
    /**
     * Get the frame element that contains this window
     * @return the BaseFrameElement (HtmlFrame or HtmlInlineFrame)
     */
    public BaseFrameElement getFrameElement();
    
    /**
     * Get the source URL of this frame
     * @return the URL loaded in this frame
     */
    public URL getSrcAttribute();
    
    /**
     * Reload the frame content
     * @throws IOException if reload fails
     */
    public void reload() throws IOException;
    
    /**
     * Check if this frame allows navigation by parent
     * @return true if parent can navigate this frame
     */
    public boolean isAccessAllowed();
}

Usage Examples:

HtmlPage page = webClient.getPage("https://example.com/frames");

// Find frame by name
WebWindow frameWindow = null;
for (WebWindow window : webClient.getWebWindows()) {
    if ("contentFrame".equals(window.getName())) {
        frameWindow = window;
        break;
    }
}

if (frameWindow instanceof FrameWindow) {
    FrameWindow frame = (FrameWindow) frameWindow;
    
    // Get frame information
    BaseFrameElement frameElement = frame.getFrameElement();
    URL frameSrc = frame.getSrcAttribute();
    
    // Access frame content
    Page frameContent = frame.getEnclosedPage();
    if (frameContent instanceof HtmlPage) {
        HtmlPage framePage = (HtmlPage) frameContent;
        String frameTitle = framePage.getTitleText();
    }
    
    // Reload frame
    frame.reload();
}

Window Collection Management

Access and manage multiple windows and frames.

/**
 * Get all open windows
 * @return List of all WebWindow objects
 */
public List<WebWindow> getWebWindows();

/**
 * Get all top-level windows
 * @return List of TopLevelWindow objects
 */
public List<TopLevelWindow> getTopLevelWindows();

/**
 * Get the currently active window
 * @return the current WebWindow
 */
public WebWindow getCurrentWindow();

/**
 * Set the active window
 * @param window the WebWindow to make current
 */
public void setCurrentWindow(WebWindow window);

/**
 * Open a target window (handles _blank, _self, named windows)
 * @param opener the window that initiated the open
 * @param windowName the target window name
 * @param defaultName default name if windowName is generic
 * @return the target WebWindow
 */
public WebWindow openTargetWindow(WebWindow opener, String windowName, String defaultName);

/**
 * Close all windows
 */
public void closeAllWindows();

Usage Examples:

WebClient webClient = new WebClient();

// Load page with multiple frames
HtmlPage mainPage = webClient.getPage("https://example.com/frameset");

// Get all windows (main + frames)
List<WebWindow> allWindows = webClient.getWebWindows();
System.out.println("Total windows: " + allWindows.size());

// Process each window
for (WebWindow window : allWindows) {
    String name = window.getName();
    Page page = window.getEnclosedPage();
    
    if (page instanceof HtmlPage) {
        HtmlPage htmlPage = (HtmlPage) page;
        System.out.println("Window '" + name + "': " + htmlPage.getTitleText());
    }
}

// Get only top-level windows
List<TopLevelWindow> topWindows = webClient.getTopLevelWindows();

// Switch between windows
WebWindow originalWindow = webClient.getCurrentWindow();
for (WebWindow window : allWindows) {
    if ("targetFrame".equals(window.getName())) {
        webClient.setCurrentWindow(window);
        // Perform operations in this window context
        break;
    }
}

// Restore original window
webClient.setCurrentWindow(originalWindow);

Window Event Handling

Handle window lifecycle events.

public interface WebWindowListener {
    /**
     * Called when a new window is opened
     * @param event the WebWindowEvent with details
     */
    void webWindowOpened(WebWindowEvent event);
    
    /**
     * Called when a window is closed
     * @param event the WebWindowEvent with details
     */
    void webWindowClosed(WebWindowEvent event);
    
    /**
     * Called when window content changes
     * @param event the WebWindowEvent with details
     */
    void webWindowContentChanged(WebWindowEvent event);
}

public class WebWindowEvent {
    /**
     * Get the window that generated this event
     * @return the WebWindow source
     */
    public WebWindow getWebWindow();
    
    /**
     * Get the old page (for content change events)
     * @return the previous Page, or null
     */
    public Page getOldPage();
    
    /**
     * Get the new page (for content change events)
     * @return the new Page, or null
     */
    public Page getNewPage();
}

/**
 * Add a window event listener
 * @param listener the WebWindowListener to add
 */
public void addWebWindowListener(WebWindowListener listener);

/**
 * Remove a window event listener
 * @param listener the WebWindowListener to remove
 */
public void removeWebWindowListener(WebWindowListener listener);

Usage Examples:

WebClient webClient = new WebClient();

// Add window event listener
webClient.addWebWindowListener(new WebWindowListener() {
    @Override
    public void webWindowOpened(WebWindowEvent event) {
        WebWindow window = event.getWebWindow();
        System.out.println("Window opened: " + window.getName());
    }
    
    @Override
    public void webWindowClosed(WebWindowEvent event) {
        WebWindow window = event.getWebWindow();
        System.out.println("Window closed: " + window.getName());
    }
    
    @Override
    public void webWindowContentChanged(WebWindowEvent event) {
        WebWindow window = event.getWebWindow();
        Page newPage = event.getNewPage();
        System.out.println("Content changed in window: " + window.getName());
        
        if (newPage instanceof HtmlPage) {
            HtmlPage htmlPage = (HtmlPage) newPage;
            System.out.println("New page title: " + htmlPage.getTitleText());
        }
    }
});

// Navigation will trigger content change events
HtmlPage page = webClient.getPage("https://example.com");

Popup and Dialog Windows

Handle popup windows and JavaScript dialogs.

/**
 * Enable or disable popup blocking
 * @param enabled true to block popups
 */
public void setPopupBlockerEnabled(boolean enabled);

/**
 * Check if popup blocking is enabled
 * @return true if popups are blocked
 */
public boolean isPopupBlockerEnabled();

Usage Examples:

WebClient webClient = new WebClient();

// Control popup behavior
webClient.getOptions().setPopupBlockerEnabled(false); // Allow popups

// Page with popup links
HtmlPage page = webClient.getPage("https://example.com/popups");
HtmlAnchor popupLink = page.getAnchorByName("openPopup");

// Click link that opens popup
Page result = popupLink.click();

// Check if new window was created
List<WebWindow> windows = webClient.getWebWindows();
if (windows.size() > 1) {
    WebWindow popupWindow = windows.get(windows.size() - 1);
    System.out.println("Popup opened: " + popupWindow.getName());
    
    // Access popup content
    Page popupContent = popupWindow.getEnclosedPage();
}

// Close popup windows
for (WebWindow window : webClient.getWebWindows()) {
    if (window instanceof TopLevelWindow && !"main".equals(window.getName())) {
        ((TopLevelWindow) window).close();
    }
}

Install with Tessl CLI

npx tessl i tessl/maven-org-htmlunit--htmlunit

docs

cookies.md

exceptions.md

forms.md

http.md

index.md

javascript.md

page-dom.md

web-client.md

windows.md

tile.json