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.testmax.handler.SeleniumBaseHandler.java

License:CDDL license

protected void mouseMove(WebElement elm) {
    Point coordinates = elm.getLocation();
    Robot robot;/*from  w ww.j  a va  2  s  . c o m*/
    try {
        robot = new Robot();
        robot.mouseMove(coordinates.getX(), coordinates.getY() + 120);
    } catch (AWTException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.testmax.runner.SeleniumTestRunner.java

License:CDDL license

private boolean scrollAndClick(WebElement control, int position) {

    try {/*from   w ww  .  j ava 2s  .c  o  m*/
        ((JavascriptExecutor) driver)
                .executeScript("window.scrollTo(" + position + "," + control.getLocation().y + ")");
        printMessage("**** Clicking " + this.controlIdentifier + ": Elapse Time:" + this.timeTaken + " :"
                + this.addMessage());
        control.click();
        printMessage("**** Clicked " + this.controlIdentifier + ":" + this.addMessage());

    } catch (Exception e) {

        printMessage(
                "**** Failed To Click " + this.controlIdentifier + " -" + e.getMessage().substring(0, 150));
        return false;
    }
    return true;
}

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 a  v  a  2 s.  c  o  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;
}

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

License:Open Source License

/**
 * Visualy mark an element on the webPage.
 *
 * @param wd/*from   w w w.jav  a 2 s .  co m*/
 * @param we
 * @param innerHtml - content of message dv
 * @param backrgba - background color string : try 'red' or
 * 'rgba(64,0,0,0.3)'
 * @param frontrgba - front color for border and text
 */
public static void highlightElement(WebDriver wd, WebElement we, String innerHtml, String backrgba,
        String frontrgba) {

    if (wd == null || we == null) {
        return;
    }

    // scrollElementIntoView(wd, we);
    // Get element size and location
    int left = we.getLocation().x;
    int top = we.getLocation().y;
    int width = we.getSize().width;
    int height = we.getSize().height;

    final String css = String.format("color:%s;" + "background-color:%s;"
            + "border-style:dashed;border-width:2px;border-color:%s; " + "position:absolute;"
            + "top:%dpx;left:%dpx;" + "width:%dpx;height:%dpx;" + "z-index:1000;", frontrgba, backrgba,
            frontrgba, top, left, width, height);

    final String script = String.format("elem = document.createElement('div');" + "elem.innerHTML='%s';"
            + "elem.setAttribute('style','%s');" + "document.body.appendChild(elem);", innerHtml, css);

    ((JavascriptExecutor) wd).executeScript(script);
}

From source file:com.twiceagain.rservejavademo.extract.FTree.java

private void initFTree(WebDriver wd, WebElement we, FTree parent) {

    if (parent != null) {
        this.parent = parent;
        depth = parent.getDepth() + 1;// www  .j  a v  a 2  s.  c om

        path.addAll(parent.path);
        path.add(we.getTagName());

    }

    if (we != null) {
        tag = we.getTagName();
        visible = we.isDisplayed();
        if (isVisible()) {
            Dimension d = we.getSize();
            width = d.width;
            height = d.height;
            Point p = we.getLocation();
            x = p.x;
            y = p.y;
            area = getWidth() * getHeight();
            ratio = (getHeight() == 0) ? 0 : getWidth() / getHeight();
            text = we.getText();
        }
        attributes.putAll(BasicDriver.getAttributes(wd, we));
        String cl = attributes.get("class");
        if (cl != null) {
            classList.addAll(Arrays.asList(cl.split("\\s+")));
        }

        List<WebElement> lwe = we.findElements(By.xpath("./*"));
        for (WebElement w : lwe) {
            FTree cc = new FTree(wd, w, this);
            getChildren().add(cc);
        }
    }
}

From source file:com.vaadin.addon.spreadsheet.elements.SpreadsheetElement.java

License:Open Source License

boolean isElementSelected(WebElement element) {
    updateSelectionLocationAndSize();//  ww  w .ja  v a 2 s.  c  om
    Point location = element.getLocation();
    location.x += element.getSize().getWidth() / 2;
    location.y += element.getSize().getHeight() / 2;
    return isInSelection(location) || isNonCoherentlySelected(element);
}

From source file:com.vaadin.framework7.samples.SpringCrudIT.java

License:Apache License

private void checkFormLocation(WebElement form) {
    Point location = form.getLocation();
    int minimalPosition = getDriver().manage().window().getSize().width - 1;
    Assert.assertTrue(//from  ww w.  ja v a  2  s .  c  om
            String.format("Expected form position: >=%d but actual was: %d", minimalPosition, location.getX()),
            location.getX() >= minimalPosition);
}

From source file:com.vaadin.framework8.samples.SpringCrudIT.java

License:Apache License

private void checkFormLocation(WebElement form) {
    Point location = form.getLocation();
    Assert.assertTrue(location.getX() >= getDriver().manage().window().getSize().width - 1);
}

From source file:com.vaadin.testbench.elements.MenuBarElement.java

License:Apache License

private void activateOrOpenSubmenu(WebElement item, boolean alwaysClick) {

    if (lastItemLocationMovedTo == null || !isAnySubmenuVisible()) {
        item.click();/*from   w  w w .j  a  v  a  2s.c o  m*/
        if (hasSubmenu(item)) {
            lastItemLocationMovedTo = item.getLocation();
        }
        return;
    }

    // Assumes mouse is still at position of last clicked element
    Actions action = new Actions(getDriver());
    action.moveToElement(item);
    action.build().perform();

    if (isLeaf(item) || isSelectedTopLevelItem(item)) {
        lastItemLocationMovedTo = null;
    } else {
        lastItemLocationMovedTo = item.getLocation();
    }

    if (alwaysClick || isLeaf(item) || !isAnySubmenuVisible()) {
        action = new Actions(getDriver());
        action.click();
        action.build().perform();
    }
}

From source file:com.vaadin.tests.components.calendar.CalendarMonthViewDndEventTest.java

License:Apache License

@Test
public void dragAndDropEventToNextDay() {
    openTestURL();//  w  w  w  . j  a  v a 2  s  .c  o m

    WebElement calendar = findElement(By.className("v-calendar"));
    int calendarRight = calendar.getLocation().getX() + calendar.getSize().getWidth();

    WebElement event = findElement(By.className("v-calendar-event"));
    int x = event.getLocation().getX();
    int width = event.getSize().getWidth();

    // does calendar have space on the right for one more event (i.e. day
    // cell on the right).
    boolean moveRight = event.getLocation().getX() + 2 * event.getSize().getWidth() <= calendarRight;

    WebElement cell = getParentCell(event, "v-calendar-month-day");

    int cellY = cell.getLocation().getY();
    int cellHeight = cell.getSize().getHeight();

    long origStart = getTime("start");
    long origEnd = getTime("end");

    if (moveRight) {
        dragAndDrop(event, event.getSize().getWidth() + 5, 0);
    } else {
        dragAndDrop(event, -width / 2, 0);
    }

    event = findElement(By.className("v-calendar-event"));
    int newX = event.getLocation().getX();
    int newY = event.getLocation().getY();

    Assert.assertTrue("Moved event has wrong Y position (not the same row), new Y position=" + newY
            + ", cell Y position=" + cellY + ", cell height=" + cellHeight,
            newY >= cellY && newY < cellY + cellHeight);
    if (moveRight) {
        Assert.assertTrue("Moved event has wrong X position (not after original event)", newX >= x + width - 1);
    } else {
        width = event.getSize().getWidth();
        Assert.assertTrue("Moved event has wrong X position (not after original event)", x >= newX + width - 1);
    }

    long start = getTime("start");
    long end = getTime("end");

    int day = 24 * 3600000;
    if (moveRight) {
        Assert.assertEquals("Start date of moved event is not next day, same time", origStart + day, start);
        Assert.assertEquals("End date of moved event is not next day, same time", origEnd + day, end);
    } else {
        Assert.assertEquals("Start date of moved event is not previous day, same time", origStart - day, start);
        Assert.assertEquals("End date of moved event is not previous day, same time", origEnd - day, end);

    }
}