private String selectWindowByRemoteTitle(String title) { String match = null; boolean windowFound = false; for (String uniqueId : uniqueIdToCommandQueue.keySet()) { CommandQueue commandQueue = uniqueIdToCommandQueue.get(uniqueId); String windowName; try { windowName = getRemoteWindowTitle(commandQueue); } catch (WindowClosedException e) { // If the window is closed, then it can't be the window we're looking for continue; } if (windowName.equals(title)) { windowFound = true; match = uniqueId; break; } } // Return with an error if we didn't find the window if (!windowFound) { return "ERROR: could not find window " + title; } setCurrentFrameAddress(match); return "OK"; }
private String selectWindowByNameOrVar(String seleniumWindowName) { String match = findMatchingFrameAddress( uniqueIdToCommandQueue.keySet(), seleniumWindowName, DEFAULT_LOCAL_FRAME_ADDRESS); if (match == null) { return "ERROR: could not find window " + seleniumWindowName; } setCurrentFrameAddress(match); return "OK"; }
public String waitForLoad(long timeoutInMilliseconds) throws RemoteCommandException { final String uniqueId; int timeoutInSeconds = (int) (timeoutInMilliseconds / 1000l); if (timeoutInSeconds == 0) { timeoutInSeconds = 1; } uniqueId = waitForLoad(currentSeleniumWindowName, currentLocalFrameAddress, timeoutInSeconds); setCurrentFrameAddress(uniqueId); if (uniqueId == null) { throw new RuntimeException("uniqueId is null in waitForLoad...this should not happen."); } return "OK"; }
private String selectWindow(String seleniumWindowName) { if (!proxyInjectionMode) { String result; try { result = doCommand("selectWindow", seleniumWindowName, ""); } catch (RemoteCommandException rce) { result = rce.getMessage(); } return result; } if (seleniumWindowName == null) { seleniumWindowName = DEFAULT_SELENIUM_WINDOW_NAME; } if (seleniumWindowName.startsWith("title=")) { return selectWindowByRemoteTitle(seleniumWindowName.substring(6)); } // TODO separate name and var into separate functions if (seleniumWindowName.startsWith("name=")) { seleniumWindowName = seleniumWindowName.substring(5); return selectWindowByNameOrVar(seleniumWindowName); } if (seleniumWindowName.startsWith("var=")) { seleniumWindowName = seleniumWindowName.substring(4); return selectWindowByNameOrVar(seleniumWindowName); } // no locator prefix; try the default strategies String match = findMatchingFrameAddress( uniqueIdToCommandQueue.keySet(), seleniumWindowName, DEFAULT_LOCAL_FRAME_ADDRESS); // If we didn't find a match, try finding the frame address by window title if (match == null) { return selectWindowByRemoteTitle(seleniumWindowName); } // we found a match setCurrentFrameAddress(match); return "OK"; }
/** * Schedules the specified command to be retrieved by the next call to handle command result, and * returns the result of that command. * * @param command - the remote command verb * @param arg - the first remote argument (meaning depends on the verb) * @param value - the second remote argument * @return - the command result, defined by the remote JavaScript. "getX" style commands may * return data from the browser; other "doX" style commands may just return "OK" or an error * message. * @throws RemoteCommandException if a waitForLoad failed. */ public String doCommand(String command, String arg, String value) throws RemoteCommandException { if (proxyInjectionMode) { if (command.equals("selectFrame")) { if ("".equals(arg)) { arg = "top"; } boolean newFrameFound = false; // DGF iterate in lexical order for testability Set<String> idSet = uniqueIdToCommandQueue.keySet(); String[] ids = idSet.toArray(new String[0]); Arrays.sort(ids); for (String uniqueId : ids) { CommandQueue frameQ = uniqueIdToCommandQueue.get(uniqueId); if (frameQ.isClosed()) { continue; } FrameAddress frameAddress = frameQ.getFrameAddress(); if (frameAddress.getWindowName().equals(currentSeleniumWindowName)) { if (queueMatchesFrameAddress(frameQ, currentLocalFrameAddress, arg)) { setCurrentFrameAddress(uniqueId); newFrameFound = true; break; } } } if (!newFrameFound) { return "ERROR: starting from frame " + currentFrameAddress + ", could not find frame " + arg; } return "OK"; } if (command.equals("selectWindow")) { return selectWindow(arg); } if (command.equals("waitForPopUp")) { String waitingForThisWindowName = arg; long timeoutInMilliseconds = Long.parseLong(value); String uniqueId; try { // Wait for the popup window to load, if it throws // an exception then we should simply return the // command result uniqueId = waitForLoad(waitingForThisWindowName, "top", (int) (timeoutInMilliseconds / 1000l)); // if (!result.equals("OK")) { // return result; // } } catch (RemoteCommandException ex) { return ex.getResult(); } // Return the result of selecting the frame address, not the window name setCurrentFrameAddress(uniqueId); return "OK"; } if (command.equals("waitForPageToLoad")) { return waitForLoad(arg); } if (command.equals("waitForFrameToLoad")) { String waitingForThisFrameName = arg; long timeoutInMilliseconds = Long.parseLong(value); String currentWindowName = getCommandQueue().getFrameAddress().getWindowName(); String result; try { result = waitForLoad( currentWindowName, waitingForThisFrameName, (int) (timeoutInMilliseconds / 1000l)); } catch (RemoteCommandException e) { return e.getMessage(); } setCurrentFrameAddress(result); return "OK"; } if (command.equals("setTimeout")) { try { pageLoadTimeoutInMilliseconds = Integer.parseInt(arg); } catch (NumberFormatException e) { return "ERROR: setTimeout arg is not a number: " + arg; } return "OK"; } if (command.equals("getAllWindowNames")) { return getAttributeFromAllWindows("name"); } if (command.equals("getAllWindowTitles")) { return getAttributeFromAllWindows("document.title"); } if (command.equals("getAllWindowIds")) { return getAttributeFromAllWindows("id"); } if (command.equals("getAttributeFromAllWindows")) { return getAttributeFromAllWindows(arg); } // handle closed queue (the earlier commands don't care about closed queues) CommandQueue queue = getCommandQueue(); if (queue.isClosed()) { try { String uniqueId = waitForLoad(currentSeleniumWindowName, currentLocalFrameAddress, 1); setCurrentFrameAddress(uniqueId); } catch (RemoteCommandException e) { return WindowClosedException.WINDOW_CLOSED_ERROR; } } if (command.equals("open")) { markWhetherJustLoaded(currentUniqueId, false); String t = getCommandQueue().doCommand(command, arg, value); if (!"OK".equals(t)) { return t; } return waitForLoad(pageLoadTimeoutInMilliseconds); } // strip off AndWait - in PI mode we handle this in the server rather than in core... if (command.endsWith("AndWait")) { markWhetherJustLoaded(currentUniqueId, false); command = command.substring(0, command.length() - "AndWait".length()); String t = getCommandQueue().doCommand(command, arg, value); if (!t.startsWith("OK")) { return t; } return waitForLoad(pageLoadTimeoutInMilliseconds); } } // if (proxyInjectionMode) markWhetherJustLoaded(currentUniqueId, false); return getCommandQueue().doCommand(command, arg, value); }