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:com.redskyit.scriptDriver.RunTests.java

License:MIT License

private void info(WebElement element, String selector, boolean verify) throws Exception {
    do {//w w  w.j  ava2s. c o m
        try {
            Point loc = element.getLocation();
            Dimension size = element.getSize();
            String tag = element.getTagName();
            System.out
                    .print(null == selector ? "test-id \"" + element.getAttribute("test-id") + "\"" : selector);
            System.out.print(" info");
            System.out.print(" tag " + tag);
            System.out.print((element.isDisplayed() ? "" : " not") + " displayed");
            System.out.print(" at " + loc.x + "," + loc.y);
            System.out.print(" size " + size.width + "," + size.height);
            System.out.print((element.isEnabled() ? "" : " not") + " enabled");
            System.out.print((element.isSelected() ? "" : " not") + " selected");
            if (tag.equals("input") || tag.equals("select")) {
                System.out.print(" check \"" + element.getAttribute("value") + "\"");
            } else {
                String text = tag.equals("textarea") ? element.getAttribute("value") : element.getText();
                if (text.indexOf('\n') != -1) {
                    CRC32 crc = new CRC32();
                    crc.update(text.getBytes());
                    System.out.print(" checksum \"crc32:" + crc.getValue() + "\"");
                } else {
                    System.out.print(" check \"" + element.getText() + "\"");
                }
            }
            System.out.println();
            return;
        } catch (StaleElementReferenceException e) {
            // If element has gone stale during a dump, ignore it
            if (!verify)
                return;
            // element has gone stale, re-select it
            System.out.println("// EXCEPTION : StaleElementReference");
        } catch (Exception e) {
            if (verify)
                throw e;
            return;
        }
        sleepAndReselect(100);
    } while (_waitFor > 0 && (new Date()).getTime() < _waitFor);
}

From source file:com.sat.dbds.vcs.login.LoginStepDef.java

License:Open Source License

/**
 * Start scenario/*  w w  w . ja v a 2s  .  c om*/
 * <pre>
 * <b> Gherkin </b>
 *   <code> @Before(order = 1)</code>
 * </pre>.
 *
 * @param by the by
 * @param loc the loc
 * @throws IOException Signals that an I/O exception has occurred.
 */

void DownloadImage(By by, String loc) throws IOException {

    WebElement Image = SeleniumUtilities.getDriver().findElement(by);
    File screen = ((TakesScreenshot) SeleniumUtilities.getDriver()).getScreenshotAs(OutputType.FILE);
    int width = Image.getSize().getWidth();
    int height = Image.getSize().getHeight();
    BufferedImage img = ImageIO.read(screen);
    BufferedImage dest = img.getSubimage(Image.getLocation().getX(), Image.getLocation().getY(), width, height);
    ImageIO.write(dest, "png", screen);
    File file = new File(loc);
    FileUtils.copyFile(screen, file);

}

From source file:com.screenslicer.core.scrape.QueryKeyword.java

License:Open Source License

private static void handleIframe(RemoteWebDriver driver) throws ActionFailed {
    List<WebElement> iframes = null;
    try {/*from   w w w  . j a  va 2 s.  c o  m*/
        iframes = driver.findElementsByTagName("iframe");
    } catch (Throwable t) {
        throw new ActionFailed(t);
    }
    try {
        for (WebElement iframe : iframes) {
            try {
                Dimension dim = iframe.getSize();
                if (iframe.isDisplayed() && (dim.getHeight() * dim.getWidth()) > MIN_IFRAME_AREA) {
                    String src = iframe.getAttribute("src");
                    if (!CommonUtil.isEmpty(src) && src.indexOf("://") > -1 && src.indexOf("?") > -1) {
                        String origHandle = null;
                        String origUrl = null;
                        String newHandle = null;
                        try {
                            origHandle = driver.getWindowHandle();
                            origUrl = driver.getCurrentUrl();
                            newHandle = Util.newWindow(driver);
                        } catch (Throwable t) {
                            Log.exception(t);
                            throw new ActionFailed(t);
                        }
                        boolean undo = false;
                        try {
                            Util.get(driver, src, true);
                            driver.executeScript(
                                    "document.getElementsByTagName('html')[0].style.overflow='scroll';");
                            Util.driverSleepShort();
                            if (driver.findElementByTagName("body").getText().length() < MIN_SOURCE_DIFF) {
                                undo = true;
                            }
                        } catch (Throwable t) {
                            Log.exception(t);
                            undo = true;
                        }
                        try {
                            if (undo) {
                                if (origHandle.equals(newHandle)) {
                                    if (!driver.getCurrentUrl().equals(origUrl)) {
                                        try {
                                            driver.navigate().back();
                                        } catch (Throwable t) {
                                            Log.exception(t);
                                        }
                                    }
                                    if (!driver.getCurrentUrl().equals(origUrl)) {
                                        driver.get(origUrl);
                                    }
                                } else {
                                    Util.cleanUpNewWindows(driver, origHandle);
                                }
                            } else {
                                Util.cleanUpNewWindows(driver, newHandle);
                                break;
                            }
                        } catch (Throwable t) {
                            Log.exception(t);
                            throw new ActionFailed(t);
                        }
                    }
                }
            } catch (Throwable t) {
                Log.exception(t);
                continue;
            }
        }
    } catch (Throwable t) {
        Log.exception(t);
        throw new ActionFailed(t);
    }
}

From source file:com.seleniumtests.uipage.htmlelements.CachedHtmlElement.java

License:Apache License

public CachedHtmlElement(WebElement elementToCache) {
    try {//from   w  ww .  j  a va2s . c o  m
        rectangle = elementToCache.getRect();
        location = new Point(rectangle.x, rectangle.y);
        size = new Dimension(rectangle.width, rectangle.height);
    } catch (WebDriverException e) {
        location = elementToCache.getLocation();
        size = elementToCache.getSize();
        rectangle = new Rectangle(location, size);
    }

    cachedElement = Jsoup.parseBodyFragment(elementToCache.getAttribute("outerHTML")).body().child(0);
    if ("option".equals(cachedElement.tagName())
            || ("input".equals(cachedElement.tagName())
                    && "checkbox".equals(cachedElement.attributes().getIgnoreCase("type")))
            || ("input".equals(cachedElement.tagName())
                    && "radio".equals(cachedElement.attributes().getIgnoreCase("type")))) {
        selected = elementToCache.isSelected();
    } else {
        selected = false;
    }
    realElement = elementToCache;

}

From source file:com.seleniumtests.uipage.htmlelements.PictureElement.java

License:Apache License

/**
 * Move to the center of the picture/* w w w  .  j av  a2s  .  c o  m*/
 * @param element   The element to move to
 * @param coordX   x offset from the center of the element
 * @param coordY   y offset from the center of the element
 * @return
 */
private Actions move(WebElement element, int coordX, int coordY) {

    if (SeleniumTestsContextManager.isWebTest()) {
        // issue #133: handle new actions specific case
        // more browsers will be added to this conditions once they are migrated to new composite actions
        if (SeleniumTestsContextManager.getThreadContext().getBrowser() == BrowserType.FIREFOX) {
            // issue #133: firefox moves to center of element in page
            coordX -= element.getSize().width / 2;
            coordY -= element.getSize().height / 2;
        } else if (SeleniumTestsContextManager.getThreadContext()
                .getBrowser() == BrowserType.INTERNET_EXPLORER) {
            // issue #180: internet explorer moves to center of element in viewport
            Dimension viewportDim = ((CustomEventFiringWebDriver) (WebUIDriver.getWebDriver()))
                    .getViewPortDimensionWithoutScrollbar();
            coordX -= Math.min(element.getSize().width, viewportDim.width) / 2;
            coordY -= Math.min(element.getSize().height, viewportDim.height) / 2;
        }
    }

    return new Actions(WebUIDriver.getWebDriver()).moveToElement(element, coordX, coordY);
}

From source file:com.smash.revolance.ui.materials.mock.webdriver.PageTest.java

License:Open Source License

@Test
public void pageShouldHandleHtml() throws IOException {
    List<WebElement> elements = browser.findElements(By.xpath("//body//*"));
    assertThat(elements.size(), is(85));

    WebElement element = elements.get(0);

    Point location = element.getLocation();
    assertThat(location.getX(), is(255));
    assertThat(location.getY(), is(35));

    Dimension dimension = element.getSize();
    assertThat(dimension.getWidth(), is(879));
    assertThat(dimension.getHeight(), is(918));

    String tag = element.getTagName();
    assertThat(tag, is("div"));

    String txt = element.getText();
    assertThat(txt, is(nullValue()));/*w ww . j  ava 2  s  .  c  om*/

    String cssClass = element.getAttribute("class");
    assertThat(cssClass, is("page"));
}

From source file:com.smash.revolance.ui.model.element.api.Element.java

License:Open Source License

public Element(Page page, WebElement element) {
    this();/* ww  w  .ja  va2s  . co  m*/
    setPage(page);
    setTag(element.getTagName());
    setDim(element.getSize());
    setPos(element.getLocation());
    setClz(element.getAttribute("class"));
    setId(element.getAttribute("id"));
}

From source file:com.smash.revolance.ui.model.element.api.Element.java

License:Open Source License

public static boolean isIncluded(WebElement element, Rectangle rectangle) {
    Point topleft = element.getLocation();
    Dimension dim = element.getSize();
    Rectangle rectangleRef = new Rectangle(topleft.getX(), topleft.getY(), dim.getWidth(), dim.getHeight());

    return rectangleRef.contains(rectangle);
}

From source file:com.stratio.qa.specs.CommonG.java

License:Apache License

private Integer getDocumentHeight(WebDriver driver) {
    WebElement body = driver.findElement(By.tagName("html"));
    return body.getSize().getHeight();
}

From source file:com.twiceagain.mywebdriver.driver.web.Drivers.java

License:Open Source License

/**
 * Screen shot of selected element only.
 *
 * @param wd//from  w w w  .  j ava2  s .co  m
 * @param ele
 * @return
 * @throws IOException
 */
public static File screenshot2TempFile(WebDriver wd, WebElement ele) throws IOException {
    Drivers.scrollElementIntoView(wd, ele);
    File fi = ((TakesScreenshot) wd).getScreenshotAs(OutputType.FILE);
    BufferedImage img = ImageIO.read(fi);
    // Get the location of element on the page
    Point point = ele.getLocation();

    // Get width and height of the element
    int eleWidth = ele.getSize().getWidth();
    int eleHeight = ele.getSize().getHeight();

    // Crop the entire page screenshot to get only element screenshot
    BufferedImage eleScreenshot = img.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);
    ImageIO.write(eleScreenshot, "png", fi);
    return fi;
}