Example usage for org.openqa.selenium WebElement getLocation

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

Introduction

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

Prototype

Point getLocation();

Source Link

Document

Where on the page is the top left-hand corner 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 {//from w w  w. j a  v a2 s . com
        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/*  ww  w  . ja  va 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.seleniumtests.uipage.htmlelements.CachedHtmlElement.java

License:Apache License

public CachedHtmlElement(WebElement elementToCache) {
    try {//from   w  ww .  ja  va2  s.c om
        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.HtmlElement.java

License:Apache License

/**
* Make element visible. Sometimes useful when real elements are backed by an image element
*//*  w ww .  jav a 2 s  . com*/
protected void makeWebElementVisible(WebElement element) {
    if (SeleniumTestsContextManager.isWebTest()) {
        if (element.isDisplayed()) {
            return;
        }
        try {

            if (element.getLocation().x < 0) {
                Long viewportHeight = (Long) ((JavascriptExecutor) driver)
                        .executeScript("return document.documentElement.clientHeight");
                Integer heightPosition = element.getLocation().y > viewportHeight
                        ? element.getLocation().y - viewportHeight.intValue()
                        : element.getLocation().y;
                changeCssAttribute(element, "left", "20px");
                changeCssAttribute(element, "top", heightPosition + "px");
                changeCssAttribute(element, "position", "inherit");
            }
            if (element.getAttribute("style").toLowerCase().replace(" ", "").contains("display:none")) {
                changeCssAttribute(element, "display", "block");
            }
            //            changeCssAttribute(element, "clip", "auto");
            changeCssAttribute(element, "zIndex", "100000");
        } catch (Exception e) {
            return;
        }

        // wait for element to be displayed
        try {
            new WebDriverWait(driver, 1).until(ExpectedConditions.visibilityOf(element));
        } catch (ElementNotVisibleException e) {
            TestLogging.info(String.format("element %s not visible", element));
        } catch (Exception e) {
            logger.warn("Could not make element visible", e);
        }

    }
}

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

License:Apache License

/**
 * Get a table cell at row,column coordinates
 * @param row      the row index//ww  w. j  a  v a2  s.  c  om
 * @param column   the column index
 * @param focus      if true, scroll to this cell
 * @return
 */
public WebElement getCell(final int row, final int column, boolean focus) {
    WebElement cell = getCell(row, column);
    if (focus) {
        Point loc = cell.getLocation();
        ((CustomEventFiringWebDriver) driver).scrollTo(loc.x - 50, loc.y - 50);
    }
    return cell;
}

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()));//from  ww w  . ja  v a  2  s. com

    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();//from  w ww . j  av  a 2s.c  o 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.sugarcrm.candybean.examples.mobile.AppiumAndroidTest.java

License:Open Source License

@Test
public void testLocation() throws Exception {
    WebElement button = driver.findElement(By.xpath("//button[1]"));

    Point location = button.getLocation();

    assertEquals(location.getX(), 157);/*  www  .j  av a2s . c  o m*/
    assertEquals(location.getY(), 182);
}

From source file:com.sugarcrm.candybean.examples.mobile.AppiumIosTest.java

License:Open Source License

@Test
public void testLocation() throws Exception {
    WebElement button = driver.findElement(By.xpath("//button[1]"));

    Point location = button.getLocation();

    assertEquals(location.getX(), 94);//w  ww  . j a va  2 s. co m
    assertEquals(location.getY(), 122);
}