From fe6087e4b0e767645b62053faab3be629135fc3a Mon Sep 17 00:00:00 2001 From: Alex Gorbatovsky Date: Fri, 3 Jan 2020 16:42:09 -0500 Subject: [PATCH] HTML Frame-related setters and getters. When interacting with content within element, the frame must be set as the current window. Otherwise HtmlUnit fails to find elements or perform actions on elements within the frame. --- .../gargoylesoftware/htmlunit/WebClient.java | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/src/main/java/com/gargoylesoftware/htmlunit/WebClient.java b/src/main/java/com/gargoylesoftware/htmlunit/WebClient.java index 052c85fe8e7..56945da27ab 100644 --- a/src/main/java/com/gargoylesoftware/htmlunit/WebClient.java +++ b/src/main/java/com/gargoylesoftware/htmlunit/WebClient.java @@ -840,6 +840,34 @@ public void setCurrentWindow(final WebWindow window) { } } } + + /** + * Set the current window to the original window instantiated by the browser + * (the earliest opened window). + */ + public void setFirstWindow() { + setCurrentWindow(topLevelWindows_.get(0)); + } + + /** + * Set the current window to a frame, identified by index. + */ + public void setCurrentFrame(final int index) throws IndexOutOfBoundsException { + HtmlPage page = (HtmlPage) getCurrentWindow().getEnclosedPage(); + List frames = page.getFrames(); + if (index >= frames.size()) + throw new IndexOutOfBoundsException("Attempt to select frame " + index + " of " + frames.size() + " frames"); + setCurrentWindow(frames.get(index)); + } + + /** + * Set the current window to a frame, identified by name. + */ + public void setCurrentFrame(final String name) throws ElementNotFoundException { + HtmlPage page = (HtmlPage) getCurrentWindow().getEnclosedPage(); + FrameWindow frame = page.getFrameByName(name); + setCurrentWindow(frame); + } /** * Adds a listener for {@link WebWindowEvent}s. All events from all windows associated with this @@ -1093,6 +1121,29 @@ public WebWindow getWebWindowByName(final String name) throws WebWindowNotFoundE throw new WebWindowNotFoundException(name); } + + /** + * Get @{@code TopLevelWindow} or {@code DialogWindow} with the specified name. + */ + public WebWindow getTopLevelOrDialog(final String name) throws WebWindowNotFoundException { + WebAssert.notNull("name", name); + + for (final WebWindow webWindow : windows_) { + if ((webWindow instanceof TopLevelWindow || webWindow instanceof DialogWindow ) && name.equals(webWindow.getName())) { + return webWindow; + } + } + + throw new WebWindowNotFoundException(name); + } + + /** + * Get the {@code FrameWindow}s within the current page. + */ + public List getFrames() { + HtmlPage page = (HtmlPage) getCurrentWindow().getEnclosedPage(); + return page.getFrames(); + } /** * INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.