Example usage for org.openqa.selenium WebElement getSize

List of usage examples for org.openqa.selenium WebElement getSize

Introduction

In this page you can find the example usage for org.openqa.selenium WebElement getSize.

Prototype

Dimension getSize();

Source Link

Document

What is the width and height of the rendered element?

Usage

From source file:org.safs.selenium.webdriver.lib.WDLibrary.java

License:Open Source License

/**
 * Capture component image//www  .java 2 s .c  o m
 * @param we - WebElement object.
 * @param imgName - Image full path with name
 * @throws Exception
 */
public static void captureScreen(WebElement we, String imgName, String fileformat)
        throws SeleniumPlusException {
    try {
        Rectangle rect = new Rectangle(we.getLocation().x, we.getLocation().y, we.getSize().width,
                we.getSize().height);
        BufferedImage img = captureBrowserArea(rect);
        File outputfile = new File(imgName + "." + fileformat);
        ImageIO.write(img, fileformat, outputfile);
    } catch (Exception e) {
        throw new SeleniumPlusException("Failed to capture screenshot " + e.getMessage());
    }
}

From source file:org.safs.selenium.webdriver.lib.WDLibrary.java

License:Open Source License

/**
 * To check if a certain point of web-element is shown on the page.<br>
 * If the point is not given or it is out of boundary of web-element, method will<br>
 * check if the web-element is fully shown on the page.<br>
 *
 * @param element WebElement, to check if it is shown on the page
 * @param offset Point, the offset from Left-Top of web-element; the point to check. can be null
 * @return boolean if the offest-point or the full-web-element is shown on the page.
 *///from   www.j  a  va  2  s.c  om
public static boolean isShowOnPage(WebElement element, org.openqa.selenium.Point offset) {
    String debugmsg = StringUtils.debugmsg(false);
    boolean isShowOnPage = false;

    try {
        WDLibrary.checkNotNull(element);

        //The location(Left-Top) is relative to the viewport
        org.openqa.selenium.Point elementLTLoc = element.getLocation();
        Dimension elementD = element.getSize();

        int browserXOffset = (int) WDLibrary.lastBrowserWindow.getPageXOffset();
        int browserYOffset = (int) WDLibrary.lastBrowserWindow.getPageYOffset();
        //move the location according to the page-offset, get the location relative to the page
        elementLTLoc = elementLTLoc.moveBy(-browserXOffset, -browserYOffset);
        org.openqa.selenium.Point elementBRLoc = elementLTLoc.moveBy(elementD.width, elementD.height);

        //Get the bounds of the browser's page
        int browserClientW = (int) WDLibrary.lastBrowserWindow.getClientWidth();
        int browserClientH = (int) WDLibrary.lastBrowserWindow.getClientHeight();
        Dimension browserPageBounds = new Dimension(browserClientW, browserClientH);

        IndependantLog.debug(debugmsg + " offset=" + offset + "; element dimension=" + elementD + "; ");

        if (offset != null && isLocationInBounds(offset, elementD)) {
            //check the offset point is shown browser's page
            elementLTLoc = elementLTLoc.moveBy(offset.x, offset.y);
            IndependantLog.debug(debugmsg + "check if 'element (Top Left+offset) Corner'=" + elementLTLoc
                    + " is in 'browser window dimension'=" + browserPageBounds + "; ");
            isShowOnPage = isLocationInBounds(elementLTLoc, browserPageBounds);
        } else {
            //if Left-Top and Bottom-Right is in the browser, the element is fully shown
            IndependantLog.debug(debugmsg + "check if 'element Top Left Corner'=" + elementLTLoc
                    + " and 'Bottom Right Corner'=" + elementBRLoc + " are both in 'browser window dimension'="
                    + browserPageBounds + "; ");
            isShowOnPage = isLocationInBounds(elementLTLoc, browserPageBounds)
                    && isPointInBounds(elementBRLoc, browserPageBounds);
        }

    } catch (Exception e) {
        IndependantLog.error(debugmsg + "Fail due to " + StringUtils.debugmsg(e));
    }

    IndependantLog.debug(debugmsg + " return " + isShowOnPage);
    return isShowOnPage;
}

From source file:org.safs.selenium.webdriver.lib.WDLibrary.java

License:Open Source License

/**
 * Click a component with an offset. This API will not verify that the click does happen.<br>
 * If you want to make sure of that, please call {@link #click(WebElement, Point)} instead.<br>
 * <br>/*  www.j  a v a2 s .c  o  m*/
 * Sometimes we want to click without verification, for example, to show an Alert.<br> 
 * With presence of Alert, any call to Selenium API will throw out UnhandledAlertException<br>
 * and close the Alert automatically. Our API {@link #click(WebElement, Point)} will call<br>
 * Selenium API for verification, so it is unable to open the Alert successfully.<br>  
 * 
 * @param component WebElement, the component to click
 * @param offset Point, the offset (relative to component) to click at
 * @param optional String[], the optional parameters
 * <ul>
 * <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>
 * @return boolean true if succeed.
 * @see #click(WebElement, Point)
 */
public static boolean clickUnverified(WebElement component, Point offset, String... optional) {
    String debugmsg = StringUtils.debugmsg(false);

    try {
        IndependantLog.debug(debugmsg + " click with parameter componet:" + component + ", offset:" + offset);
        //Create a combined actions according to the parameters
        Actions actions = new Actions(getWebDriver());

        boolean autoscroll = parseAutoScroll(optional);
        if (autoscroll) {
            if (offset != null)
                actions.moveToElement(component, offset.x, offset.y);
            else
                actions.moveToElement(component);
        }
        IndependantLog.debug(debugmsg + " Try Selenium API to click.");
        actions.click().perform();

        return true;
    } catch (Exception e) {
        IndependantLog.warn(
                debugmsg + " Failed with Selenium API, met " + StringUtils.debugmsg(e) + ". Try Robot click.");

        try {
            Point p = WDLibrary.getScreenLocation(component);

            if (offset != null)
                p.translate(offset.x, offset.y);
            else
                p.translate(component.getSize().width / 2, component.getSize().height / 2);

            RBT.click(p, null, WDLibrary.MOUSE_BUTTON_LEFT, 1);

            return true;
        } catch (Exception e1) {
            IndependantLog.error(debugmsg + " Failed with Robot click!");
        }
        return false;
    }
}

From source file:org.sonarqube.report.extendedpdf.OverviewPDFReporter.java

License:Open Source License

private void captureScreenshot(WebDriver driver, String selector) throws IOException {
    File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    driver.get(dashboardUrl);/*w  ww.j  a  v  a  2s  .  c  om*/
    System.out.println("Screenshot captured...");
    List<WebElement> elements = driver.findElements(By.cssSelector(selector));
    if (!elements.isEmpty()) {
        WebElement element = elements.get(0);
        BufferedImage fullImg = ImageIO.read(screenshot);
        Point point = element.getLocation();
        int eleWidth = element.getSize().getWidth();
        int eleHeight = element.getSize().getHeight();
        BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);
        ImageIO.write(eleScreenshot, "png", screenshot);
        System.out.println("Cropping to get widget: " + selector.substring(1));
        FileUtils.copyFile(screenshot,
                new File(screenshotsDir + File.separator + selector.substring(1) + ".png"));
    }
}

From source file:org.sugarcrm.voodoodriver.VDDMouse.java

License:Apache License

public void DnD(WebElement src, WebElement dst) {
    int src_x, src_y, dst_x, dst_y;
    Dimension srcDim = null;/*w  w  w.j a  v a  2  s. c  om*/
    Dimension dstDim = null;
    String msg = "";
    this.reporter.Log("Starting DnD.");

    if (this.robo == null) {
        return;
    }

    Thread.currentThread();
    Point srcPoint = ((Locatable) src).getCoordinates().getLocationOnScreen();
    Point dstPoint = ((Locatable) src).getCoordinates().getLocationOnScreen();
    srcDim = src.getSize();
    dstDim = dst.getSize();
    src_x = srcPoint.getX() + (srcDim.getWidth() / 2);
    src_y = srcPoint.getY() + (srcDim.getHeight() / 2);
    dst_x = dstPoint.getX() + (dstDim.getWidth() / 2);
    dst_y = dstPoint.getY() + (dstDim.getHeight() / 2);

    msg = String.format("DnD Source Screen Coordinates: X => '%d' Y => '%d'", src_x, src_y);
    this.reporter.Log(msg);
    msg = String.format("DnD Dest Screen Coordinates: X => '%d' Y => '%d'", dst_x, dst_y);
    this.reporter.Log(msg);

    this.robo.mouseMove(srcPoint.x / 2, srcPoint.y / 2);
    this.robo.delay(800);
    this.robo.mousePress(InputEvent.BUTTON1_MASK);
    this.robo.delay(800);
    this.Move(src_x, src_y, dst_x, dst_y);
    this.robo.delay(800);
    this.robo.mouseRelease(InputEvent.BUTTON1_MASK);
    this.robo.delay(500);
    msg = "DnD Finished.";
    this.reporter.Log(msg);
}

From source file:org.suren.autotest.web.framework.log.TargetElementMark.java

License:Apache License

@Override
public void mark(WebElement ele, File file) throws IOException {
    BufferedImage bufImg = ImageIO.read(file);

    try {//from  w w w. j  a  v a  2  s .co m
        WebElement webEle = (WebElement) ele;
        Point loc = webEle.getLocation();
        Dimension size = webEle.getSize();

        Graphics2D g = bufImg.createGraphics();
        g.setColor(Color.red);
        g.drawRect(loc.getX(), loc.getY(), size.getWidth(), size.getHeight());
    } catch (StaleElementReferenceException se) {
    }
}

From source file:org.suren.autotest.web.framework.selenium.action.SeleniumHover.java

License:Apache License

@Override
public void hover(Element ele) {
    WebElement webEle = searchStrategyUtils.findStrategy(WebElement.class, ele).search(ele);
    if (webEle == null) {
        logger.warn("can not found element.");
        return;/*from www.j  a va  2  s.  co m*/
    }

    if (!(ele instanceof FileUpload)) {
        Dimension size = webEle.getSize();
        Point loc = webEle.getLocation();
        int toolbarHeight = engine.getToolbarHeight();
        int x = size.getWidth() / 2 + loc.getX();
        int y = size.getHeight() / 2 + loc.getY() + toolbarHeight;

        try {
            new Robot().mouseMove(x, y);
        } catch (AWTException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

From source file:org.xframium.page.element.SeleniumElement.java

License:Open Source License

@Override
public Image _getImage(Resolution imageResolution) {
    WebElement imageElement = getElement();

    if (imageElement != null) {
        if (imageElement.getLocation() != null && imageElement.getSize() != null
                && imageElement.getSize().getWidth() > 0 && imageElement.getSize().getHeight() > 0) {
            String fileKey = "PRIVATE:" + getDeviceName() + ".png";
            PerfectoMobile.instance().imaging().screenShot(getExecutionId(), getDeviceName(), fileKey,
                    Screen.primary, ImageFormat.png, imageResolution);
            byte[] imageData = PerfectoMobile.instance().repositories().download(RepositoryType.MEDIA, fileKey);
            if (imageData != null && imageData.length > 0) {
                try {
                    BufferedImage fullImage = ImageIO.read(new ByteArrayInputStream(imageData));
                    return fullImage.getSubimage(imageElement.getLocation().getX(),
                            imageElement.getLocation().getY(), imageElement.getSize().getWidth(),
                            imageElement.getSize().getHeight());
                } catch (Exception e) {
                    log.error(Thread.currentThread().getName() + ": Error extracting image data", e);
                }/*from ww w  . j  ava2  s  .  c  o m*/
            } else
                log.warn(Thread.currentThread().getName() + ": No image data could be retrieved for [" + fileKey
                        + "]");

        } else
            log.warn(Thread.currentThread().getName() + ": The element returned via " + getKey()
                    + " did not contain a location or size");
    }

    return null;
}

From source file:org.xframium.page.element.SeleniumElement.java

License:Open Source License

public boolean _moveTo() {
    WebElement webElement = getElement();
    if (webElement != null && webElement.getSize().getHeight() > 0 && webElement.getSize().getWidth() > 0) {
        if (webDriver instanceof HasInputDevices) {
            new Actions(webDriver).moveToElement(webElement).build().perform();
            return true;
        }/*from  w  ww.  j ava  2  s. c o m*/
    }

    return false;
}

From source file:org.xframium.page.element.SeleniumElement.java

License:Open Source License

public boolean _press() {
    WebElement webElement = getElement();
    if (webElement != null && webElement.getSize().getHeight() > 0 && webElement.getSize().getWidth() > 0) {
        if (webDriver instanceof HasInputDevices) {
            new Actions(webDriver).clickAndHold(webElement).build().perform();
            return true;
        }/*from   www.jav a  2 s  .co m*/
    }

    return false;
}