Example usage for org.openqa.selenium.interactions Actions moveToElement

List of usage examples for org.openqa.selenium.interactions Actions moveToElement

Introduction

In this page you can find the example usage for org.openqa.selenium.interactions Actions moveToElement.

Prototype

public Actions moveToElement(WebElement target) 

Source Link

Document

Moves the mouse to the middle of the element.

Usage

From source file:org.openmrs.module.mirebalais.smoke.pageobjects.AbstractPageObject.java

License:Open Source License

public void clickOn(WebElement element) {
    Actions actions = new Actions(driver);
    actions.moveToElement(element).click().perform();
}

From source file:org.openmrs.module.mirebalais.smoke.pageobjects.AbstractPageObject.java

License:Open Source License

public void hoverOn(By elementId) {
    Actions builder = new Actions(driver);
    Actions hover = builder.moveToElement(driver.findElement(elementId));
    hover.perform();/*from w w  w . j a  v  a  2 s.c om*/
}

From source file:org.richfaces.tests.metamer.ftest.richDragIndicator.TestDragIndicator.java

License:Open Source License

@Test
public void testAccepting() {

    dragIndicatorAttributes.set(draggingClass, DRAGGING_CLASS);
    dragIndicatorAttributes.set(acceptClass, ACCEPT_CLASS);

    Actions actionQueue = new Actions(driver);

    Action dragging = actionQueue.clickAndHold(page.drag1).build();
    dragging.perform();//from  www.j a  va  2s .co  m
    elementNotPresent.element(page.indicator);

    // just small move to display indicator
    dragging = actionQueue.moveByOffset(1, 1).build();
    dragging.perform();
    assertTrue(page.indicator.isDisplayed());
    assertTrue(page.indicator.getAttribute("class").contains(DRAGGING_CLASS));

    dragging = actionQueue.moveToElement(page.drop1).build();
    dragging.perform();
    assertTrue(page.indicator.isDisplayed());
    assertTrue(page.indicator.getAttribute("class").contains(ACCEPT_CLASS));

    // then release
    dragging = actionQueue.release().build();
    dragging.perform();
    elementNotPresent.element(page.indicator);
}

From source file:org.richfaces.tests.metamer.ftest.richDragIndicator.TestDragIndicator.java

License:Open Source License

@Test
public void testRejecting() {
    dragIndicatorAttributes.set(draggingClass, DRAGGING_CLASS);
    dragIndicatorAttributes.set(rejectClass, REJECT_CLASS);

    Actions actionQueue = new Actions(driver);

    Action dragging = actionQueue.clickAndHold(page.drag1).build();
    dragging.perform();// w  w  w.j  a  va  2  s .  c  o m
    elementNotPresent.element(page.indicator);

    // just small move to display indicator
    dragging = actionQueue.moveByOffset(1, 1).build();
    dragging.perform();
    assertTrue(page.indicator.isDisplayed());
    assertTrue(page.indicator.getAttribute("class").contains(DRAGGING_CLASS));

    dragging = actionQueue.moveToElement(page.drop2).build();
    dragging.perform();
    assertTrue(page.indicator.isDisplayed());
    assertTrue(page.indicator.getAttribute("class").contains(REJECT_CLASS));

    // then release
    dragging = actionQueue.release().build();
    dragging.perform();
    elementNotPresent.element(page.indicator);
}

From source file:org.richfaces.tests.metamer.ftest.richDragIndicator.TestDragIndicator.java

License:Open Source License

@Test
public void testMovingOverDifferentStates() {
    dragIndicatorAttributes.set(draggingClass, DRAGGING_CLASS);
    dragIndicatorAttributes.set(rejectClass, REJECT_CLASS);
    dragIndicatorAttributes.set(acceptClass, ACCEPT_CLASS);

    for (int i = 0; i <= 20; ++i) {
        Actions actionQueue = new Actions(driver);

        Action dragging = actionQueue.clickAndHold(page.drag1).build();
        dragging.perform();/*from w  w w .j  a  v a2s  . c  o  m*/
        elementNotPresent.element(page.indicator);

        // just small move to display indicator
        dragging = actionQueue.moveByOffset(1, 1).build();
        dragging.perform();
        assertTrue(page.indicator.isDisplayed());
        assertTrue(page.indicator.getAttribute("class").contains(DRAGGING_CLASS));

        // then move out of drag area (but not over drop area)
        dragging = actionQueue.moveByOffset(550, 10).build();
        dragging.perform();
        assertTrue(page.indicator.isDisplayed());
        assertTrue(page.indicator.getAttribute("class").contains(DRAGGING_CLASS));

        dragging = actionQueue.moveToElement(page.drop1).build();
        dragging.perform();
        assertTrue(page.indicator.isDisplayed());
        assertTrue(page.indicator.getAttribute("class").contains(ACCEPT_CLASS));

        dragging = actionQueue.moveToElement(page.drop2).build();
        dragging.perform();
        assertTrue(page.indicator.isDisplayed());
        assertTrue(page.indicator.getAttribute("class").contains(REJECT_CLASS));

        // then release
        dragging = actionQueue.release().build();
        dragging.perform();
        elementNotPresent.element(page.indicator);
    }

}

From source file:org.rstudio.studio.selenium.MenuNavigator.java

License:Open Source License

public static WebElement getMenuItem(final WebDriver driver, String level1, String level2, String level3) {
    WebElement popupItem = getMenuItem(driver, level1, level2);
    Actions action = new Actions(driver);
    action.moveToElement(popupItem).build().perform();

    // Wait for there to be two popups open (the level1 and level2 menus)
    (new WebDriverWait(driver, 1)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver d) {
            List<WebElement> elements = driver.findElements(By.className("gwt-MenuBarPopup"));
            return elements.size() > 1;
        }//from   w ww .j ava2 s  .  com
    });

    // Get the second popup menu
    List<WebElement> elements = driver.findElements(By.className("gwt-MenuBarPopup"));
    WebElement menu2popup = elements.get(1);

    return findMenuItemByName(menu2popup, level3);
}

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

License:Open Source License

/**
 * Does NOT clear any existing text in the control, but does attempt to insure the window/control has focus.
 * <br><em>Purpose:</em>
 * <a href="http://safsdev.sourceforge.net/sqabasic2000/SeleniumGenericMasterFunctionsReference.htm#detail_InputKeys" alt="inputKeys Keyword Reference" title="inputKeys Keyword Reference">inputKeys</a>
 * <p>//from w w w.j  a  v  a2 s. c o m
 * Bypasses attempts to use AWT Robot for keystrokes.
 * Attempts to convert SAFS keystrokes to Selenium low-level Actions keystrokes.
 * @param we WebElement to send SAFS keystrokes (or plain text).
 * @param keystrokes SAFS keystrokes or plain text to type.
 * @throws SeleniumPlusException if we are unable to process the keystrokes successfully.
 **/
public static void inputKeysSAFS2Selenium(WebElement we, String keystrokes) throws SeleniumPlusException {
    String debugmsg = StringUtils.debugmsg(false);
    IndependantLog.debug(debugmsg + " processing '" + keystrokes + "' on webelement " + we);
    if (!focus(we))
        IndependantLog.warn(debugmsg + " Fail to set focus to webelement " + we);

    RemoteDriver wd = null;
    try {
        wd = (RemoteDriver) getWebDriver();
    } catch (Exception x) {
    }
    // convert to Selenium low-level Action keystrokes.
    if (keysparser != null) {
        Vector keys = keysparser.parseInput(keystrokes);
        Actions actions = new Actions(wd);

        if (we != null)
            actions = actions.moveToElement(we);

        Iterator events = keys.iterator();
        RobotKeyEvent event;
        Keys k = null;
        CharSequence c = null;
        while (events.hasNext()) {
            try {
                event = (RobotKeyEvent) events.next();
                c = null;
                k = convertToKeys(event);
                if (k == null) {
                    c = convertToCharacter(event);
                } else {

                }
                switch (event.get_event()) {

                case RobotKeyEvent.KEY_PRESS:
                    if (k != null) {
                        IndependantLog.debug(debugmsg + " handling keyDown '" + k.name() + "'");
                        actions = actions.keyDown(k);
                    } else {
                        IndependantLog.debug(debugmsg + " send char '" + c + "'");
                        actions = actions.sendKeys(c);
                    }
                    break;

                case RobotKeyEvent.KEY_RELEASE:
                    if (k != null) {
                        IndependantLog.debug(debugmsg + " handling keyUp '" + k.name() + "'");
                        actions = actions.keyUp(k);
                    } else {
                        IndependantLog.debug(debugmsg + " send char '" + c + "'");
                        actions = actions.sendKeys(c);
                    }
                    break;

                case RobotKeyEvent.KEY_TYPE:
                    if (k != null) {
                        IndependantLog.debug(debugmsg + " send Key '" + k.name() + "'");
                        actions = actions.sendKeys(k);
                    } else {
                        IndependantLog.debug(debugmsg + " send char '" + c + "'");
                        actions = actions.sendKeys(c);
                    }
                    break;
                default:
                }
            } catch (Exception x) {
                IndependantLog.debug(debugmsg + " IGNORING RobotKeyEvent exception:", x);
            }
        }
        try {
            actions.build().perform();
        } catch (StaleElementReferenceException x) {
            // the click probably was successful because the elements have changed!
            IndependantLog.debug(debugmsg + " StaleElementException (not found).");
        } catch (Throwable x) {
            IndependantLog.debug(debugmsg + " " + x.getClass().getName() + ", " + x.getMessage());
        } finally {
            IndependantLog.debug(debugmsg + " selenium actions.build().perform() complete.");
        }
    } else {
        // TODO what if keyparser cannot load a keys converter???
    }
}

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 w  w .ja va2  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.senchalabs.gwt.gwtdriver.gxt.models.TreeDndTest.java

License:Apache License

@Test
public void testDragAndDrop() {
    Tree t = new Tree(driver, driver.findElement(new ByChained(By.xpath("//body/*"),
            new ByWidget(driver, com.sencha.gxt.widget.core.client.tree.Tree.class))));

    Item root = t.findItemWithText("root");
    root.toggleExpand();/*from   w w w.j  a v  a 2 s  .  c  om*/
    Assert.assertEquals(2, root.getChildren().size());

    Item foo = root.getChildren().get(0);
    foo.toggleExpand();

    Item bar = foo.getChildren().get(0);
    foo.getElement();
    Actions a = new Actions(driver);
    a.clickAndHold(bar.getElement());
    a.moveToElement(root.getElement());
    a.release();

    a.perform();

    Assert.assertEquals(3, root.getChildren().size());

}

From source file:org.senchalabs.gwt.gwtdriver.gxt.models.TreeDndTest.java

License:Apache License

@Test
public void testDragExpandAndDrop() throws InterruptedException {
    Tree t = new Tree(driver, driver.findElement(new ByChained(By.xpath("//body/*"),
            new ByWidget(driver, com.sencha.gxt.widget.core.client.tree.Tree.class))));

    Item root = t.findItemWithText("root");
    Item other = t.getRootChildren().get(1);

    //drag, still holding
    Actions a = new Actions(driver);
    a.clickAndHold(other.getElement());//w  ww.  j ava 2  s .co  m
    a.moveToElement(root.getElement());
    a.perform();

    //wait until expanded (800ms by default, wait 1 second)
    Thread.sleep(1000);

    Assert.assertEquals(2, root.getChildren().size());
    Item foo = root.getChildren().get(0);

    a = new Actions(driver);
    a.moveToElement(foo.getElement());
    a.release();
    a.perform();

    Assert.assertEquals(1, t.getRootChildren().size());
    Assert.assertEquals(false, foo.isExpanded());
    Assert.assertEquals(2, root.getChildren().size());

    foo.toggleExpand();
    Assert.assertEquals(2, foo.getChildren().size());
}