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:com.digi.selenium.util.BillNReload.Bill_n_Reload_Prepaid_Broadband_Util.java

/**
 * Read the 30Days Graph data/* w  w  w. j a  v  a2 s .  co m*/
 */
protected void targetGraphData_30Days() {
    JavascriptExecutor jse = (JavascriptExecutor) getDriver();

    Wait<WebDriver> wait = new WebDriverWait(getDriver(), 15);

    Map<String, String> mapbill = new HashMap<String, String>();
    jse.executeScript("window.scrollBy(0,-500)", "");
    jse.executeScript("window.scrollBy(0,225)", "");
    wait.until(ExpectedConditions
            .visibilityOfElementLocated(By.xpath("//*[name()='svg']/*[name()='g'][13]/*[name()='g'][1]")));
    handleLink_ByText(AppConstants.DAYS_30);

    boolean breakIt = true;

    //Get the BillCycle Elements
    String billcyclerootNodeXpath = "//*[name()='svg']/*[name()='g'][13]/*[name()='g'][1]";
    WebElement billcyclerootElement = getDriver().findElement(By.xpath(billcyclerootNodeXpath));

    String billcyclechildXpath = "./*[name()='text']";

    List<WebElement> listbillcyclechild = billcyclerootElement.findElements(By.xpath(billcyclechildXpath));

    int eleListSize = listbillcyclechild.size();
    System.out.println("listChildElements.size() : " + listbillcyclechild.size());

    //Get the BillCycle Graph , tooltip Elements
    String bcGraphRootNodeXpath = "//*[name()='svg']/*[name()='g'][6]/*[name()='g']";
    WebElement bcGraphRootElement = getDriver().findElement(By.xpath(bcGraphRootNodeXpath));

    String bcGraphChildXpath = "./*[name()='g']";

    List<WebElement> listbcGraphChild = bcGraphRootElement.findElements(By.xpath(bcGraphChildXpath));
    int bcGraphListSize = listbcGraphChild.size();
    if (bcGraphListSize > 0) {
        targetBCGraphData_30Days = new HashMap();
    }
    // System.out.println("bcGraphListSize.size() : " + listbcGraphChild.size());

    for (int j = 0; j < eleListSize; j++) {
        int n = j + 1;
        String billCycleDate = null;
        String bcAtrribute_color = null;
        while (true) {
            breakIt = true;
            try {

                //read the billcycle data from nodes 
                String billchildNodePath_startDate = billcyclerootNodeXpath.concat("/*[name()='text']")
                        .concat("[").concat(String.valueOf(n).concat("]").concat("/*[name()='tspan']"));
                WebElement billelement_Date = getDriver().findElement(By.xpath(billchildNodePath_startDate));

                billCycleDate = billelement_Date.getText();

                if ((billCycleDate.trim().length() > 0)
                        && (!(graphBillCycleDate_30Days.contains(billCycleDate)))) {
                    graphBillCycleDate_30Days.add(billCycleDate);
                    System.out.println("Bill Cycle : '" + n + "' -->" + billCycleDate);
                }

                //read the billcycle graph data - color  from nodes
                String billchildNodePath_color = bcGraphRootNodeXpath.concat("/*[name()='g']").concat("[")
                        .concat(String.valueOf(n).concat("]").concat("/*[name()='path']"));
                //System.out.println("Node billchildNodePath_color : " + billchildNodePath_color);
                WebElement billelement_color = getDriver().findElement(By.xpath(billchildNodePath_color));

                bcAtrribute_color = billelement_color.getAttribute("stroke");

                Actions builder = new Actions(getDriver());

                String childMouseHouverNode = bcGraphRootNodeXpath.concat("/*[name()='g']").concat("[")
                        .concat(String.valueOf(n).concat("]"));
                builder.moveToElement(getDriver().findElement(By.xpath(childMouseHouverNode))).perform();

                WebElement mouseHoverNodeElement = getDriver()
                        .findElement(By.xpath("//div[9]/div[1]/div[1]/div[2]/div/div/div"));

                if (mouseHoverNodeElement != null) {
                    String toolTipText = mouseHoverNodeElement.getText();

                    System.out.println("toolTipText : '" + n + "' " + toolTipText);
                    String toolTipTextMap = AppConstants.TOOL_TIP_TEXT.concat(AppConstants.BEGIN_BRACKET)
                            .concat(Integer.toString(n)).concat(AppConstants.CLOSE_BRACKET);
                    if ((toolTipText.trim().length() > 0)
                            && (!(graphToolTipData_30Days.contains(toolTipText)))) {
                        graphToolTipData_30Days.add(toolTipText);
                        builder.moveToElement(getDriver().findElement(By.xpath(billcyclerootNodeXpath))).click()
                                .build().perform();
                        Thread.sleep(3000);
                    } //end if - tooltip text
                } //end if - mouseHoverNodeElement

            } catch (Exception e) {
                if (e.getMessage().contains("element is not attached")) {
                    breakIt = false;
                }
            }
            if (breakIt) {
                break;
            }

        } //while loop ends
        targetBCGraphData_30Days.put(Integer.toString(n), bcAtrribute_color);
    } //for loop ends
}

From source file:com.digi.selenium.util.BillNReload.Bill_n_Reload_Prepaid_Util.java

/**
 * Read the 90Days Graph data/*from w  w  w .j  a  v  a 2  s.  c  om*/
 */
protected void targetGraphData_90Days() {
    JavascriptExecutor jse = (JavascriptExecutor) getDriver();

    //scroll up the page 
    jse.executeScript("window.scrollBy(0,-500)", "");
    handleLink_ByText(AppConstants.DAYS_90);
    Wait<WebDriver> wait = new WebDriverWait(getDriver(), 30);
    Map<String, String> mapbill = new HashMap<String, String>();
    jse.executeScript("window.scrollBy(0,225)", "");
    wait.until(ExpectedConditions
            .visibilityOfElementLocated(By.xpath("//*[name()='svg']/*[name()='g'][13]/*[name()='g'][1]")));
    //Working :get the stroke attribute to get the color value of graph
    getDriver().manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    boolean breakIt = true;

    //Get the BillCycle Elements
    String billcyclerootNodeXpath = "//*[name()='svg']/*[name()='g'][13]/*[name()='g'][1]";
    WebElement billcyclerootElement = getDriver().findElement(By.xpath(billcyclerootNodeXpath));
    String billcyclechildXpath = "./*[name()='text']";
    List<WebElement> listbillcyclechild = billcyclerootElement.findElements(By.xpath(billcyclechildXpath));
    int eleListSize = listbillcyclechild.size();
    // System.out.println("listChildElements.size() : " + listbillcyclechild.size());

    //Get the BillCycle Graph , tooltip Elements
    String bcGraphRootNodeXpath = "//*[name()='svg']/*[name()='g'][6]/*[name()='g']";
    WebElement bcGraphRootElement = getDriver().findElement(By.xpath(bcGraphRootNodeXpath));

    String bcGraphChildXpath = "./*[name()='g']";

    List<WebElement> listbcGraphChild = bcGraphRootElement.findElements(By.xpath(bcGraphChildXpath));
    int bcGraphListSize = listbcGraphChild.size();
    if (bcGraphListSize > 0) {
        targetBCGraphData_90Days = new HashMap();
    }
    // System.out.println("bcGraphListSize.size() : " + listbcGraphChild.size());

    for (int j = 0; j < eleListSize; j++) {
        int n = j + 1;
        Map<String, String> tempBCGData = new HashMap();
        String billCycleDate = null;
        String bcAtrribute_color = null;
        while (true) {
            breakIt = true;
            try {

                //read the billcycle data from nodes 
                String billchildNodePath_startDate = billcyclerootNodeXpath.concat("/*[name()='text']")
                        .concat("[").concat(String.valueOf(n).concat("]").concat("/*[name()='tspan'][1]"));
                WebElement billelement_startDate = getDriver()
                        .findElement(By.xpath(billchildNodePath_startDate));
                //Thread.sleep(2000);
                String billAtrribute_startDate = billelement_startDate.getText();

                String billchildNodePath_endDate = billcyclerootNodeXpath.concat("/*[name()='text']")
                        .concat("[").concat(String.valueOf(n).concat("]").concat("/*[name()='tspan'][2]"));
                WebElement billelement_endDate = getDriver().findElement(By.xpath(billchildNodePath_endDate));

                String billAtrribute_endDate = billelement_endDate.getText();

                billCycleDate = billAtrribute_startDate.concat("_").concat(billAtrribute_endDate);
                //System.out.println("Bill Cycle : '" + n+"' -->"+  billCycleDate);

                //read the billcycle graph data - color, tooltip from nodes
                String billchildNodePath_color = bcGraphRootNodeXpath.concat("/*[name()='g']").concat("[")
                        .concat(String.valueOf(n).concat("]").concat("/*[name()='path']"));
                //System.out.println("Node billchildNodePath_color : " + billchildNodePath_color);
                WebElement billelement_color = getDriver().findElement(By.xpath(billchildNodePath_color));

                bcAtrribute_color = billelement_color.getAttribute("stroke");
                String colorMap = AppConstants.COLOR.concat(AppConstants.BEGIN_BRACKET).concat(billCycleDate)
                        .concat(AppConstants.CLOSE_BRACKET);
                tempBCGData.put(colorMap, bcAtrribute_color);
                Actions builder = new Actions(getDriver());

                String childMouseHouverNode = bcGraphRootNodeXpath.concat("/*[name()='g']").concat("[")
                        .concat(String.valueOf(n).concat("]"));
                builder.moveToElement(getDriver().findElement(By.xpath(childMouseHouverNode))).perform();

                WebElement mouseHoverNodeElement = getDriver()
                        .findElement(By.xpath("//div[9]/div[1]/div[1]/div[2]/div/div/div"));

                if (mouseHoverNodeElement != null) {
                    String toolTipText = mouseHoverNodeElement.getText();
                    System.out.println("toolTipText : '" + n + "' " + toolTipText);
                    String toolTipTextMap = AppConstants.TOOL_TIP_TEXT.concat(AppConstants.BEGIN_BRACKET)
                            .concat(billCycleDate).concat(AppConstants.CLOSE_BRACKET);
                    tempBCGData.put(toolTipTextMap, toolTipText.replaceAll("\n", ""));
                    builder.moveToElement(getDriver().findElement(By.xpath(billchildNodePath_startDate)))
                            .click().perform();
                }

            } catch (Exception e) {
                if (e.getMessage().contains("element is not attached")) {
                    //System.out.println("Error: Element 4 ");
                    breakIt = false;
                }
            }
            if (breakIt) {
                break;
            }

        } //while loop ends
        targetBCGraphData_90Days.put(billCycleDate, tempBCGData);
    } //for loop ends
}

From source file:com.digi.selenium.util.BillNReload.Bill_n_Reload_Prepaid_Util.java

/**
 * Read the 60Days Graph data//from  w  ww. j  a v  a 2s .c o m
 */
protected void targetGraphData_60Days() {
    JavascriptExecutor jse = (JavascriptExecutor) getDriver();

    handleLink_ByText(AppConstants.DAYS_60);
    Wait<WebDriver> wait = new WebDriverWait(getDriver(), 60);
    Map<String, String> mapbill = new HashMap<String, String>();
    jse.executeScript("window.scrollBy(0,-500)", "");
    jse.executeScript("window.scrollBy(0,225)", "");
    wait.until(ExpectedConditions
            .visibilityOfElementLocated(By.xpath("//*[name()='svg']/*[name()='g'][13]/*[name()='g'][1]")));
    //Working :get the stroke attribute to get the color value of graph
    getDriver().manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    boolean breakIt = true;

    //Get the BillCycle Elements
    String billcyclerootNodeXpath = "//*[name()='svg']/*[name()='g'][13]/*[name()='g'][1]";
    WebElement billcyclerootElement = getDriver().findElement(By.xpath(billcyclerootNodeXpath));

    String billcyclechildXpath = "./*[name()='text']";

    List<WebElement> listbillcyclechild = billcyclerootElement.findElements(By.xpath(billcyclechildXpath));

    int eleListSize = listbillcyclechild.size();
    // System.out.println("listChildElements.size() : " + listbillcyclechild.size());

    //Get the BillCycle Graph , tooltip Elements
    String bcGraphRootNodeXpath = "//*[name()='svg']/*[name()='g'][6]/*[name()='g']";
    WebElement bcGraphRootElement = getDriver().findElement(By.xpath(bcGraphRootNodeXpath));

    String bcGraphChildXpath = "./*[name()='g']";

    List<WebElement> listbcGraphChild = bcGraphRootElement.findElements(By.xpath(bcGraphChildXpath));
    int bcGraphListSize = listbcGraphChild.size();
    if (bcGraphListSize > 0) {
        targetBCGraphData_60Days = new HashMap();
    }
    // System.out.println("bcGraphListSize.size() : " + listbcGraphChild.size());

    for (int j = 0; j < eleListSize; j++) {
        int n = j + 1;
        Map<String, String> tempBCGData = new HashMap();
        String billCycleDate = null;
        String bcAtrribute_color = null;
        while (true) {
            breakIt = true;
            try {

                //read the billcycle data from nodes 
                String billchildNodePath_startDate = billcyclerootNodeXpath.concat("/*[name()='text']")
                        .concat("[").concat(String.valueOf(n).concat("]").concat("/*[name()='tspan'][1]"));
                WebElement billelement_startDate = getDriver()
                        .findElement(By.xpath(billchildNodePath_startDate));
                //Thread.sleep(2000);
                String billAtrribute_startDate = billelement_startDate.getText();

                String billchildNodePath_endDate = billcyclerootNodeXpath.concat("/*[name()='text']")
                        .concat("[").concat(String.valueOf(n).concat("]").concat("/*[name()='tspan'][2]"));
                WebElement billelement_endDate = getDriver().findElement(By.xpath(billchildNodePath_endDate));

                String billAtrribute_endDate = billelement_endDate.getText();

                billCycleDate = billAtrribute_startDate.concat("_").concat(billAtrribute_endDate);
                //System.out.println("Bill Cycle : '" + n+"' -->"+  billCycleDate);
                //read the billcycle graph data - color, tooltip from nodes
                String billchildNodePath_color = bcGraphRootNodeXpath.concat("/*[name()='g']").concat("[")
                        .concat(String.valueOf(n).concat("]").concat("/*[name()='path']"));
                //System.out.println("Node billchildNodePath_color : " + billchildNodePath_color);
                WebElement billelement_color = getDriver().findElement(By.xpath(billchildNodePath_color));

                bcAtrribute_color = billelement_color.getAttribute("stroke");
                String colorMap = AppConstants.COLOR.concat(AppConstants.BEGIN_BRACKET).concat(billCycleDate)
                        .concat(AppConstants.CLOSE_BRACKET);
                tempBCGData.put(colorMap, bcAtrribute_color);
                Actions builder = new Actions(getDriver());

                String childMouseHouverNode = bcGraphRootNodeXpath.concat("/*[name()='g']").concat("[")
                        .concat(String.valueOf(n).concat("]"));
                builder.moveToElement(getDriver().findElement(By.xpath(childMouseHouverNode))).perform();

                //System.out.println("childMouseHouverNode : '"+n +"' " +childMouseHouverNode);

                WebElement mouseHoverNodeElement = null;
                mouseHoverNodeElement = getDriver()
                        .findElement(By.xpath("//div[9]/div[1]/div[1]/div[2]/div/div/div"));
                //wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[9]/div[1]/div[1]/div[2]/div/div/div")));

                if (mouseHoverNodeElement != null) {
                    String toolTipText = mouseHoverNodeElement.getText();
                    System.out.println("toolTipText : '" + n + "' " + toolTipText);
                    String toolTipTextMap = AppConstants.TOOL_TIP_TEXT.concat(AppConstants.BEGIN_BRACKET)
                            .concat(billCycleDate).concat(AppConstants.CLOSE_BRACKET);
                    tempBCGData.put(toolTipTextMap, toolTipText);
                    builder.moveToElement(getDriver().findElement(By.xpath(billchildNodePath_startDate)))
                            .click().perform();
                }

            } catch (Exception e) {
                if (e.getMessage().contains("element is not attached")) {
                    //System.out.println("Error: Element 4 ");
                    breakIt = false;
                }
            }
            if (breakIt) {
                break;
            }

        } //while loop ends
        targetBCGraphData_60Days.put(billCycleDate, tempBCGData);
    } //for loop ends
}

From source file:com.digi.selenium.util.common.PageNavigation.java

protected void clickOnPay() {

    WebElement pay = tryToClickElementByXPath("//*[@id='payBtn']", 5);
    Actions move_action = new Actions(getDriver());
    move_action.moveToElement(pay).click().perform();
    waitForPageLoad(30);//from  w ww.j  a  v  a 2s  .  c o  m
    log.info("Success : Pay Button is clicked");
    //*[@id="payBtn"]   
    /*try {
       //click on PAY Button
       //*[@id="payBtn"]
       // WebElement elementpay=getDriver().findElement(By.xpath("//*[@id='payBtn']"));
       WebElement pay=getDriver().findElement(By.xpath("//*[@id='payBtn']"));
       Actions move_action=new Actions(getDriver());
       move_action.moveToElement(pay).click().perform();
       //System.out.println(pay.isEnabled());
       pay.click();
       waitForPageLoad(100);
    }catch(Exception e) {
       System.out.println(e);
       e.printStackTrace();
       log.error("Fail : Failed to Reload amount.");
       System.exit(0);
    }*/

}

From source file:com.epam.jdi.uitests.mobile.appium.elements.base.Element.java

License:Open Source License

public void clickWithKeys(Keys... keys) {
    invoker.doJAction("Ctrl click on Element", () -> {
        Actions action = new Actions(getDriver());
        for (Keys key : keys)
            action = action.keyDown(key);
        action = action.moveToElement(getWebElement()).click();
        for (Keys key : keys)
            action = action.keyUp(key);//from  ww  w  .  j a v  a2 s.c o m
        action.perform();
    });
}

From source file:com.epam.jdi.uitests.mobile.appium.elements.base.Element.java

License:Open Source License

public void mouseOver() {
    invoker.doJAction("Move mouse over Element", () -> {
        Actions builder = new Actions(getDriver());
        builder.moveToElement(getWebElement()).build().perform();
    });//from w  w  w. j  a v  a2s .  c  o  m
}

From source file:com.epam.jdi.uitests.mobile.appium.elements.complex.Menu.java

License:Open Source License

protected void hoverAction(String... name) {
    chooseItemAction(name, el -> {//from w w w  .ja  v a 2s .  co  m
        Actions action = new Actions(getDriver());
        action.moveToElement(el).clickAndHold().build().perform();
    });
}

From source file:com.epam.jdi.uitests.web.selenium.elements.base.Element.java

License:Open Source License

public void clickWithKeys(Keys... keys) {
    invoker.doJAction("Ctrl click on Element", () -> {
        Actions action = new Actions(getDriver());
        for (Keys key : keys) {
            action = action.keyDown(key);
        }/*from   w w  w.  j  a  va 2 s . c om*/
        action = action.moveToElement(getWebElement()).click();
        for (Keys key : keys) {
            action = action.keyUp(key);
        }
        action.perform();
    });
}

From source file:com.fmb.common.BrowserEmulator.java

License:Apache License

/**
 * Hover on the page element//from   ww w .  j av a 2 s  .  co m
 * 
 * @param xpath
 *            the element's xpath
 */
public void mouseOver(String xpath) {
    pause(stepInterval);
    expectElementExistOrNot(true, xpath, timeout);
    // First make mouse out of browser
    Robot rb = null;
    try {
        rb = new Robot();
    } catch (AWTException e) {
        e.printStackTrace();
    }
    rb.mouseMove(0, 0);

    // Then hover
    WebElement we = browserCore.findElement(By.xpath(xpath));

    if (GlobalSettings.browserCoreType == 2) {
        try {
            Actions builder = new Actions(browserCore);
            builder.moveToElement(we).build().perform();
        } catch (Exception e) {
            e.printStackTrace();
            handleFailure("Failed to mouseover " + xpath);
        }

        logger.info("Mouseover " + xpath);
        return;
    }

    // Firefox and IE require multiple cycles, more than twice, to cause a
    // hovering effect
    if (GlobalSettings.browserCoreType == 1 || GlobalSettings.browserCoreType == 3) {
        for (int i = 0; i < 5; i++) {
            Actions builder = new Actions(browserCore);
            builder.moveToElement(we).build().perform();
        }
        logger.info("Mouseover " + xpath);
        return;
    }

    // Selenium doesn't support the Safari browser
    if (GlobalSettings.browserCoreType == 4) {
        Assert.fail("Mouseover is not supported for Safari now");
    }
    Assert.fail("Incorrect browser type");
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.html.HTMLButtonElementTest.java

License:Apache License

/**
 * @throws Exception if an error occurs//from w  w w  .j  av a 2 s  . c  o  m
 */
@Test
@Alerts("mouse over [btn]")
public void mouseOver() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html>\n" + "  <head>\n"
            + "    <title>Test</title>\n" + "    <script>\n" + "    function dumpEvent(event) {\n"
            + "      // target\n" + "      var eTarget;\n" + "      if (event.target) {\n"
            + "        eTarget = event.target;\n" + "      } else if (event.srcElement) {\n"
            + "        eTarget = event.srcElement;\n" + "      }\n" + "      // defeat Safari bug\n"
            + "      if (eTarget.nodeType == 3) {\n" + "        eTarget = eTarget.parentNode;\n" + "      }\n"
            + "      var msg = 'mouse over';\n" + "      if (eTarget.name) {\n"
            + "        msg = msg + ' [' + eTarget.name + ']';\n" + "      } else {\n"
            + "        msg = msg + ' [' + eTarget.id + ']';\n" + "      }\n" + "      alert(msg);\n" + "    }\n"
            + "    </script>\n" + "  </head>\n" + "<body>\n" + "  <form id='form1'>\n"
            + "    <button id='btn' onmouseover='dumpEvent(event);'>button</button><br>\n" + "  </form>\n"
            + "</body></html>";

    final WebDriver driver = loadPage2(html);

    final Actions actions = new Actions(driver);
    actions.moveToElement(driver.findElement(By.id("btn")));
    actions.perform();

    verifyAlerts(driver, getExpectedAlerts());
}