#106 Passing an index to gotoFrame()

open
nobody
Core API (44)
5
2012-12-11
2008-12-18
Josh Holtzman
No

The application I am testing generates opaque names and IDs for iframes. To test the links in the iframes without knowing their names or IDs ahead of time, I need to be able to call gotoFrame(int indexOfFrame), where the first iframe is index 0, etc.

Rather than adding to the API, I simply modified the implementation of HtmlUnitTestingEngineImpl to include a search by index if the name and ID are not found:

private WebWindow getFrame(String frameNameOrId) {
// First try ID
for (FrameWindow frame : getCurrentPage().getFrames()) {
if (frameNameOrId.equals(frame.getFrameElement().getId())) {
return frame;
}
}
// Now try with Name
for (FrameWindow frame : getCurrentPage().getFrames()) {
if (frameNameOrId.equals(frame.getName())) {
return frame;
}
}
// Now try with index
try {
return getCurrentPage().getFrames().get(Integer.parseInt(frameNameOrId));
} catch (Exception e) {}

// Nothing was found.
return null;
}

Adding something like this, either via an explicit API (int rather than String) or by adding this capability to the existing getFrame(String) method, could be beneficial for others, too.

Discussion