List of usage examples for org.openqa.selenium WebElement getSize
Dimension getSize();
From source file:org.richfaces.tests.page.fragments.impl.Utils.java
License:Open Source License
/** * Returns Locations of input element.//from w ww . j a v a 2s .c om * @see Locations */ public static Locations getLocations(WebElement root) { Point topLeft = root.getLocation(); Dimension dimension = root.getSize(); Point topRight = topLeft.moveBy(dimension.getWidth(), 0); Point bottomRight = topRight.moveBy(0, dimension.getHeight()); Point bottomLeft = topLeft.moveBy(0, dimension.getHeight()); return new Locations(topLeft, topRight, bottomLeft, bottomRight); }
From source file:org.safs.selenium.webdriver.CFComponent.java
License:Open Source License
/** @return Rectangle, the rectangle relative to the browser. * //from ww w . j a va 2 s .c o m * If this return 'absolute rectangle on screen', we SHOULD remove override method {@link #getRectangleImage(Rectangle)}.<br> * But we need to be careful, Robot will be used to get image, we have to implement RMI to get image if the <br> * browser is running on a remote machine.<br> */ protected Rectangle getComponentRectangle() { String debugmsg = StringUtils.debugmsg(false); WebElement component = (compObject != null ? compObject : winObject); try { //Get component's location relative to the browser Point p = WDLibrary.getLocation(component, false); org.openqa.selenium.Dimension dim = component.getSize(); return new Rectangle(p.x, p.y, dim.getWidth(), dim.getHeight()); } catch (Exception e) { IndependantLog.warn(debugmsg + "Fail to get bounds for " + windowName + ":" + compName + " on screen due to " + StringUtils.debugmsg(e)); } return null; }
From source file:org.safs.selenium.webdriver.lib.model.EmbeddedObject.java
License:Open Source License
/** * To check if an Element is visible on page.<br> * If the center of the element is in the container and is shown in the browser's client area, the<br> * element will be considered as visible on page.<br> * @param element Element, the element to check * @return boolean, true if the element is visible on page. * @throws SeleniumPlusException/* w w w. java 2s . co m*/ */ protected boolean isShowOnPage(Element element) throws SeleniumPlusException { WDLibrary.checkNotNull(element); try { WebElement item = element.getWebElement(); org.openqa.selenium.Dimension itemD = item.getSize(); org.openqa.selenium.Point itemCenterOffset = new org.openqa.selenium.Point(itemD.width / 2, itemD.height / 2); org.openqa.selenium.Point itemLoc = item.getLocation(); org.openqa.selenium.Point itemCenterLoc = itemLoc.moveBy(itemCenterOffset.x, itemCenterOffset.y); WebElement container = webelement(); org.openqa.selenium.Dimension containerD = container.getSize(); org.openqa.selenium.Point containerLoc = container.getLocation(); org.openqa.selenium.Point itemCenterLocRelativeToContainer = itemCenterLoc.moveBy(-containerLoc.x, -containerLoc.y); if (item.isDisplayed() //the item is considered displayed by Selenium && WDLibrary.isLocationInBounds(itemCenterLocRelativeToContainer, containerD) //the center of item is shown in the container && WDLibrary.isShowOnPage(item, itemCenterOffset) //the center of the item is shown in browser ) { return true; } } catch (Exception e) { IndependantLog.error(StringUtils.debugmsg(false) + "Met " + StringUtils.debugmsg(e)); } return false; }
From source file:org.safs.selenium.webdriver.lib.WDLibrary.java
License:Open Source License
/** * Check if the point is inside of the boundary of WebElement.<br> * @param element WebElement, The element to get boundary to check with. * @param p Point, The point to check. * @return boolean, true if the point is inside of the boundary of WebElement *///from www. j a va 2s . c om public static boolean inside(WebElement element, Point p) { if (p == null) return true; Dimension dimension = element.getSize(); Rectangle rect = new Rectangle(0, 0, dimension.width, dimension.height); return rect.contains(p); }
From source file:org.safs.selenium.webdriver.lib.WDLibrary.java
License:Open Source License
/** * Enlarge the listening area of DocumentClickCapture, if click offset is outside of the WebElement's boundary.<br> * @param clickable WebElement, The element to click. * @param offset Point, The offset relative to the WebElement to click at. * @param listener DocumentClickCapture, The click listener to capture the click event. *//*www . j av a 2 s.c o m*/ private static void checkOffset(WebElement clickable, Point offset, DocumentClickCapture listener) { String debugmsg = StringUtils.debugmsg(false); if (clickable == null || offset == null || listener == null) { //We have nothing to check, or have no listener to disable return; } if (!inside(clickable, offset)) { IndependantLog.warn(debugmsg + "Enlarge the listening area of DocumentClickCapture, the click point " + offset + " is outside of the WebElement " + clickable.getSize()); listener.setEnlargeListeningArea(true); } }
From source file:org.safs.selenium.webdriver.lib.WDLibrary.java
License:Open Source License
/** * Click the WebElement at a certain coordination with a special key pressed.<br> * Firstly it will try to get webelement's location and use Robot to click. At the same<br> * time, it will listen to a 'javascript mouse down' event to find out if the click really<br> * happened; If not, it will try to use Selenium's API to do the work.<br> * If the click point is outside of the boundary of the WebElement, which means we are going<br> * to click on the sibling component. At this situation, our click-listener will never receive<br> * the click event, we will turn off the click-listener.<br> * * @param clickable WebElement, the WebElement to click on * @param offset Point, the coordination relative to this WebElement to click at.<br> * if the offset is null, then click at the center. * @param specialKey Keys, the special key to press during the click * @param mouseButtonNumber int, the mouse-button-number representing right, middle, or left button. * it can be {@link #MOUSE_BUTTON_LEFT} or {@link #MOUSE_BUTTON_RIGHT}.<br> * {@link #MOUSE_BUTTON_MIDDLE} NOT supported yet. * @param optional String[], the optional parameters * <ul>/*from w w w. jav a 2s. c om*/ * <li> optional[0] autoscroll boolean, if the component will be scrolled into view automatically before clicking. * if not provided, the default value is true. * </ul> * @throws SeleniumPlusException */ public static void click(WebElement clickable, Point offset, Keys specialKey, int mouseButtonNumber, String... optional) throws SeleniumPlusException { String debugmsg = StringUtils.debugmsg(WDLibrary.class, "click"); checkBeforeOperation(clickable, true); WebDriver wd = WDLibrary.getWebDriver(); RemoteDriver rd = (wd instanceof RemoteDriver) ? (RemoteDriver) wd : null; boolean autoscroll = parseAutoScroll(optional); if (autoscroll) { try { new Actions(wd).moveToElement(clickable).perform(); } catch (Throwable t) { IndependantLog .error(debugmsg + "Ignoring Selenium Robot Click 'moveToElement' action failure caused by " + t.getClass().getName()); } } MouseEvent event = null; DocumentClickCapture listener = new DocumentClickCapture(true, clickable); checkOffset(clickable, offset, listener); try { //2. Perform the click action by Robot Point location = getScreenLocation(clickable); if (offset != null) location.translate(offset.x, offset.y); else { Dimension d = clickable.getSize(); location.translate(d.width / 2, d.height / 2); } listener.addListeners(false); RBT.click(rd, location, specialKey, mouseButtonNumber, 1); listener.startListening(); //3. Wait for the 'click' event, check if the 'mousedown' event really happened. // CANAGL -- FIREFOX PROBLEM: A link that takes you to a new page (like the Google SignIn link) will // trigger the default action and apparently will NOT allow us to detect the Click occurred. // So this WILL generate a waitForClick InterruptedException (Timeout) event = listener.waitForClick(timeoutWaitRobotClick); if (event == null) { IndependantLog.resumeLogging(); IndependantLog .warn(debugmsg + " Robot may fail to perform click. Click screen location is " + location); throw new SeleniumPlusException("The Robot click action didn't happen."); } else { IndependantLog.resumeLogging(); IndependantLog.debug(debugmsg + "Robot click successful."); } } catch (Throwable thr) { IndependantLog.resumeLogging(); IndependantLog.warn(debugmsg + "Met Exception " + StringUtils.debugmsg(thr)); // let the failed listeners exit. try { Thread.sleep(DocumentClickCapture.LISTENER_LOOP_DELAY + DocumentClickCapture.delayWaitReady); } catch (Exception x) { IndependantLog.debug(debugmsg + StringUtils.debugmsg(x)); } try { //2. Perform the click action by Selenium IndependantLog.debug(debugmsg + " Try selenium API to click."); //Create a combined actions according to the parameters Actions actions = new Actions(getWebDriver()); if (autoscroll) { if (offset != null) actions.moveToElement(clickable, offset.x, offset.y); else actions.moveToElement(clickable); } if (specialKey != null) actions.keyDown(specialKey); if (isRightMouseButton(mouseButtonNumber)) actions.contextClick(); else if (isLeftMouseButton(mouseButtonNumber)) actions.click(); else if (isMiddleMouseButton(mouseButtonNumber)) { throw new SeleniumPlusException("Click 'mouse middle button' has not been supported yet."); } else { throw new SeleniumPlusException( "Mouse button number '" + mouseButtonNumber + "' cannot be recognized."); } if (specialKey != null) actions.keyUp(specialKey); IndependantLog.debug( debugmsg + "click with key '" + specialKey + "', mousebutton='" + mouseButtonNumber + "'"); //Perform the actions listener.addListeners(false); try { //if the Robot click worked, but was not detected. If we clicked a link, original page has //disappeared, so the link doesn't exist neither, the WebElement is stale. WebDriver will //not throw StaleElementReferenceException until the 'implicit timeout' is reached. //But we don't want to waste that time, so just set 'implicit timeout' to 0 and don't wait. WDTimeOut.setImplicitlyWait(0, TimeUnit.SECONDS); actions.build().perform(); listener.startListening(); // Dharmesh: Not report waitForClick failure due to listener event not capture // if click coordination out of component size or background. // It is hard to find sibling component. try { event = listener.waitForClick(timeoutWaitClick); } catch (Throwable the) { IndependantLog.debug(debugmsg + " waitForClick failed but not reported"); } ; /*if(event != null) IndependantLog.debug(debugmsg+"click has been performed."); else{ throw new SeleniumPlusException("Selenium Action.click failed to return the MouseEvent."); }*/ } catch (StaleElementReferenceException x) { listener.stopListening(); // chrome is NOT stopping! // the click probably was successful because the elements have changed! IndependantLog.debug(debugmsg + "StaleElementException (not found) suggests the click has been performed successfully."); } finally { IndependantLog.debug( debugmsg + "selenium API click finally stopping listener and resetting timeouts."); listener.stopListening(); // chrome is NOT stopping! WDTimeOut.resetImplicitlyWait(Processor.getSecsWaitForComponent(), TimeUnit.SECONDS); } } catch (Throwable th) { listener.stopListening(); // chrome is NOT stopping! if (enableClickListenerFailures) { IndependantLog.error(debugmsg, th); throw new SeleniumPlusException("click action failed: " + StringUtils.debugmsg(th)); } else { IndependantLog.debug(debugmsg + "ignoring selenium API click failure caused by " + th.getClass().getName() + ", " + th.getMessage()); } } } finally { IndependantLog.debug(debugmsg + "FINALLY stopping any ongoing listener, if any."); listener.stopListening(); // chrome is NOT stopping! } }
From source file:org.safs.selenium.webdriver.lib.WDLibrary.java
License:Open Source License
/** * Double-Click the WebElement at a certain coordination with a special key pressed.<br> * Firstly it will try to get webelement's location and use Robot to double click. At the same<br> * time, it will listen to a 'javascript mouse down' event to find out if the double click really<br> * happened; If not, it will try to use Selenium's API to do the work.<br> * If the click point is outside of the boundary of the WebElement, which means we are going<br> * to click on the sibling component. At this situation, our click-listener will never receive<br> * the click event, we will turn off the click-listener.<br> * * @param clickable WebElement, the WebElement to click on * @param offset Point, the coordination relative to this WebElement to click at.<br> * if the offset is null, then click at the center. * @param specialKey Keys, the special key to press during the click * @param mouseButtonNumber int, the mouse-button-number representing right, middle, or left button. * it can be {@link #MOUSE_BUTTON_LEFT}<br> * {@link #MOUSE_BUTTON_MIDDLE} and {@link #MOUSE_BUTTON_RIGHT} NOT supported yet. * @param optional String[], the optional parameters * <ul>//from w w w.ja v a2 s . com * <li> optional[0] autoscroll boolean, if the component will be scrolled into view automatically before clicking. * if not provided, the default value is true. * </ul> * @throws SeleniumPlusException */ public static void doubleClick(WebElement clickable, Point offset, Keys specialKey, int mouseButtonNumber, String... optional) throws SeleniumPlusException { String debugmsg = StringUtils.debugmsg(WDLibrary.class, "doubleClick"); checkBeforeOperation(clickable, true); MouseEvent event = null; DocumentClickCapture listener = new DocumentClickCapture(true, clickable); checkOffset(clickable, offset, listener); boolean autoscroll = parseAutoScroll(optional); try { //2. Perform the click action by Robot Point location = getScreenLocation(clickable); if (offset != null) location.translate(offset.x, offset.y); else location.translate(clickable.getSize().width / 2, clickable.getSize().height / 2); listener.addListeners(false); RBT.click(location, specialKey, mouseButtonNumber, 2); listener.startListening(); //3. Wait for the 'click' event, check if the 'mousedown' event really happened. event = listener.waitForClick(timeoutWaitClick); if (event == null) { IndependantLog.warn( debugmsg + " Robot may fail to perform doubleclick. Click screen location is " + location); throw new SeleniumPlusException("The doubleclick action didn't happen."); } else { IndependantLog.debug(debugmsg + "doubleclick has been peformed."); } } catch (Throwable thr) { IndependantLog.warn(debugmsg + "Met Exception " + StringUtils.debugmsg(thr)); try { //2. Perform the click action by Selenium IndependantLog.debug(debugmsg + " Try selenium API to doubleclick."); //Create a combined actions according to the parameters Actions actions = new Actions(WDLibrary.getWebDriver()); if (autoscroll) { if (offset != null) actions.moveToElement(clickable, offset.x, offset.y); else actions.moveToElement(clickable); } if (specialKey != null) actions.keyDown(specialKey); if (isLeftMouseButton(mouseButtonNumber)) actions.doubleClick(); else if (isMiddleMouseButton(mouseButtonNumber) || isRightMouseButton(mouseButtonNumber)) { throw new SeleniumPlusException( "Double click 'mouse middle/right button' has not been supported yet."); } else throw new SeleniumPlusException( "Mouse button number '" + mouseButtonNumber + "' cannot be recognized."); if (specialKey != null) actions.keyUp(specialKey); IndependantLog.debug(debugmsg + "doubleclick with key '" + specialKey + "', mousebutton='" + mouseButtonNumber + "'"); //Perform the actions listener.addListeners(false); try { //unfortunately, if the Robot click worked, but was not detected, we have to wait the full //WebDriver implied timeout period for the perform() failure to occur. actions.build().perform(); listener.startListening(); event = listener.waitForClick(timeoutWaitClick); if (event != null) IndependantLog.debug(debugmsg + "doubleclick has been peformed."); else { throw new SeleniumPlusException( "Selenium Action.doubleclick failed to detect the MouseEvent."); } } catch (StaleElementReferenceException x) { // the click probably was successful because the elements have changed! IndependantLog.debug(debugmsg + "StaleElementException (not found) suggests the click has been performed successfully."); } } catch (Throwable th) { IndependantLog.error(debugmsg, th); throw new SeleniumPlusException("doubleclick action failed: " + StringUtils.debugmsg(th)); } } finally { listener.stopListening(); } }
From source file:org.safs.selenium.webdriver.lib.WDLibrary.java
License:Open Source License
/** * Get the screen rectangle for a WebElement object. * @param we WebElement, the webelement object to get screen rectangle * @return Rectangle, the screen rectangle of a webelement object * @throws SeleniumPlusException//from w w w .j a va2 s . c om */ public static Rectangle getRectangleOnScreen(WebElement we) throws SeleniumPlusException { Rectangle rectangle = null; try { Point p = getScreenLocation(we); org.openqa.selenium.Dimension dim = we.getSize(); rectangle = new Rectangle(); rectangle.setBounds(p.x, p.y, dim.getWidth(), dim.getHeight()); return rectangle; } catch (Exception e) { throw new SeleniumPlusException( "Fail get screen rectangle for webelement, due to " + StringUtils.debugmsg(e)); } }
From source file:org.safs.selenium.webdriver.lib.WDLibrary.java
License:Open Source License
/** * @param we WebElement, the component to hover mouse * @param point Point, the position relative to the component to hover the mouse * @param millisStay double, the period to hover the mouse, in milliseconds * @throws SeleniumPlusException if the hover fail *//*from w ww . j av a 2 s . c om*/ public static void mouseHover(WebElement we, Point point, int millisStay) throws SeleniumPlusException { try { //Hover mouse on the webelement Actions action = new Actions(lastUsedWD); action.moveToElement(we); if (point != null) { int xOffset = point.x - we.getSize().width / 2; int yOffset = point.y - we.getSize().height / 2; action.moveByOffset(xOffset, yOffset); } action.build().perform(); //Pause a while StringUtilities.sleep(millisStay); //Move out the mouse //action.moveByOffset(-Robot.SCREENSZIE.width, -Robot.SCREENSZIE.height);//This will throw exception //action.build().perform(); Robot.getRobot().mouseMove(-Robot.SCREENSZIE.width, -Robot.SCREENSZIE.height); } catch (Exception e) { IndependantLog.warn(StringUtils.debugmsg(false) + "Failed to hover mouse by Selenium API: " + StringUtils.debugmsg(e)); Point screenPoint = new Point(point.x, point.y); try { translatePoint(we, screenPoint); IndependantLog.warn(StringUtils.debugmsg(false) + "Try SAFS Robot to hover mouse at screen point [" + screenPoint.x + "," + screenPoint.y + "]"); Robot.mouseHover(screenPoint, millisStay); } catch (Exception e2) { throw new SeleniumPlusException( "Failed to hover mouse at point [" + point.x + "," + point.y + "] relative to webelement."); } } }
From source file:org.safs.selenium.webdriver.lib.WDLibrary.java
License:Open Source License
/** * Given the element, and the (offsetX, offsetY) relative to element. * This function will calculate the offset point screen coordination. * /* w w w .j a v a 2s. c o m*/ * @param element WebElement, the element relative to which the coordination will be calculated. * @param offsetX String, the offset on x axis, in pixel or in percentage, for example 15 or 30%. * @param offsetX String, the offset on y axis, in pixel or in percentage, for example 45 or 50%. * * @return Point, the offset point screen coordination; or null if any exception occured. * **/ public static Point getElementOffsetScreenLocation(WebElement element, String offsetX, String offsetY) { String debugmsg = StringUtils.debugmsg(false); try { Point screenLoc = WDLibrary.getScreenLocation(element); Dimension dimemsion = element.getSize(); //calc coords according to the offset and element's location and dimension double dx, dy; dx = ImageUtils.calculateAbsoluteCoordinate(screenLoc.getX(), dimemsion.getWidth(), offsetX); dy = ImageUtils.calculateAbsoluteCoordinate(screenLoc.getY(), dimemsion.getHeight(), offsetY); return new Point((int) dx, (int) dy); } catch (Exception e) { IndependantLog.error(debugmsg + ": Exception", e); return null; } }