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

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

Introduction

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

Prototype

public Actions(WebDriver driver) 

Source Link

Usage

From source file:com.gargoylesoftware.htmlunit.html.HtmlSelect2Test.java

License:Apache License

/**
 * @exception Exception If the test fails
 *//* w  ww. j  a va  2s  .c  om*/
@Test
@BuggyWebDriver({ FF, IE })
public void select() throws Exception {
    final String html = "<html><head><title>foo</title></head><body>\n"
            + "<form id='form1'><select name='select1' multiple>\n"
            + "  <option value='option1'>Option1</option>\n" + "  <option value='option2'>Option2</option>\n"
            + "  <option value='option3' selected='selected'>Option3</option>\n"
            + "  <option value='option4'>Option4</option>\n" + "</select>\n"
            + "<input type='submit' name='button' value='foo'/>\n" + "</form></body></html>";

    final WebDriver driver = loadPage2(html);

    final List<WebElement> options = driver.findElements(By.tagName("option"));

    final Actions actions = new Actions(driver);
    final Action selectThreeOptions = actions.click(options.get(1)).click(options.get(2)).click(options.get(3))
            .build();

    selectThreeOptions.perform();

    assertFalse(options.get(0).isSelected());
    assertFalse(options.get(1).isSelected());
    assertFalse(options.get(2).isSelected());
    assertTrue(options.get(3).isSelected());
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlSelect2Test.java

License:Apache License

/**
 * @exception Exception If the test fails
 *///from   w  w w  . j av  a 2s  .  c om
@Test
@BuggyWebDriver(IE)
public void shiftClick() throws Exception {
    final String html = "<html><head><title>foo</title></head><body>\n"
            + "<form id='form1'><select name='select1' multiple>\n"
            + "  <option value='option1'>Option1</option>\n" + "  <option value='option2'>Option2</option>\n"
            + "  <option value='option3' selected='selected'>Option3</option>\n"
            + "  <option value='option4'>Option4</option>\n" + "</select>\n"
            + "<input type='submit' name='button' value='foo'/>\n" + "</form></body></html>";

    final WebDriver driver = loadPage2(html);

    final List<WebElement> options = driver.findElements(By.tagName("option"));

    final Actions actions = new Actions(driver);
    final Action selectThreeOptions = actions.click(options.get(1)).keyDown(Keys.SHIFT).click(options.get(3))
            .keyUp(Keys.SHIFT).build();

    selectThreeOptions.perform();

    assertFalse(options.get(0).isSelected());
    assertTrue(options.get(1).isSelected());
    assertTrue(options.get(2).isSelected());
    assertTrue(options.get(3).isSelected());
}

From source file:com.gargoylesoftware.htmlunit.html.HtmlSelect2Test.java

License:Apache License

/**
 * @exception Exception If the test fails
 *///from  w  w w.  j  a v  a 2s  . c om
@Test
@BuggyWebDriver({ FF, IE })
public void controlClick() throws Exception {
    final String html = "<html><head><title>foo</title></head><body>\n"
            + "<form id='form1'><select name='select1' multiple>\n"
            + "  <option value='option1'>Option1</option>\n" + "  <option value='option2'>Option2</option>\n"
            + "  <option value='option3' selected='selected'>Option3</option>\n"
            + "  <option value='option4'>Option4</option>\n" + "</select>\n"
            + "<input type='submit' name='button' value='foo'/>\n" + "</form></body></html>";

    final WebDriver driver = loadPage2(html);

    final List<WebElement> options = driver.findElements(By.tagName("option"));

    final Actions actions = new Actions(driver);
    final Action selectThreeOptions = actions.click(options.get(1)).keyDown(Keys.CONTROL).click(options.get(3))
            .keyUp(Keys.CONTROL).build();

    selectThreeOptions.perform();

    assertFalse(options.get(0).isSelected());
    assertTrue(options.get(1).isSelected());
    assertFalse(options.get(2).isSelected());
    assertTrue(options.get(3).isSelected());
}

From source file:com.gargoylesoftware.htmlunit.javascript.host.event.UIEventTest.java

License:Apache License

/**
 * @throws Exception if an error occurs/*from   w  ww.  j  ava  2s. co m*/
 */
@Test
@Alerts(DEFAULT = { "[object Event]", "undefined", "[object MouseEvent]", "1", "[object MouseEvent]",
        "2" }, IE = { "[object Event]", "undefined", "[object PointerEvent]", "0", "[object PointerEvent]",
                "0" })
public void detail() throws Exception {
    final String html = "<html><head><script>\n" + "  function alertDetail(e) {\n" + "    alert(e);\n"
            + "    alert(e.detail);\n" + "  }\n" + "</script></head>\n" + "<body onload='alertDetail(event)'>\n"
            + "  <div id='a' onclick='alertDetail(event)'>abc</div>\n"
            + "  <div id='b' ondblclick='alertDetail(event)'>xyz</div>\n" + "</body></html>";

    final String[] alerts = getExpectedAlerts();
    int i = 0;

    final WebDriver driver = loadPage2(html);
    verifyAlerts(driver, alerts[i++], alerts[i++]);

    driver.findElement(By.id("a")).click();
    verifyAlerts(driver, alerts[i++], alerts[i++]);

    final Actions action = new Actions(driver);
    action.doubleClick(driver.findElement(By.id("b")));
    action.perform();
    verifyAlerts(driver, alerts[i++], alerts[i++]);
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/* ww w  .j  a v a2s.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());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//  w w w .  j a va 2  s  . c o m
 */
@Test
@Alerts(FF = "mouse over [disabledBtn]")
public void mouseOverDiabled() 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='disabledBtn' onmouseover='dumpEvent(event);' disabled>disabled 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("disabledBtn")));
    actions.perform();

    verifyAlerts(driver, getExpectedAlerts());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/*from  ww w  .j  av  a2s  .c  o m*/
 */
@Test
@Alerts({ "clicked", "fireEvent not available" })
public void fireEvent_WithoutTemplate() throws Exception {
    final String html = "<html>\n" + "  <head>\n" + "    <title>Test</title>\n" + "    <script>\n"
            + "    function doTest() {\n" + "      var elem = document.getElementById('a');\n"
            + "      if (!elem.fireEvent) { alert('fireEvent not available'); return }\n"
            + "      elem.fireEvent('onclick');\n" + "    }\n" + "    </script>\n" + "  </head>\n" + "<body>\n"
            + "  <div id='a' onclick='alert(\"clicked\")'>foo</div>\n"
            + "  <div id='b' onmouseover='doTest()'>bar</div>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    driver.findElement(By.id("a")).click();
    verifyAlerts(driver, getExpectedAlerts()[0]);

    final Actions actions = new Actions(driver);
    actions.moveToElement(driver.findElement(By.id("b")));
    actions.perform();
    verifyAlerts(driver, getExpectedAlerts()[1]);
}

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

License:Apache License

private void mouseOver(final String element) 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" + "    " + element + "\n"
            + "  </form>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);

    final Actions actions = new Actions(driver);
    actions.moveToElement(driver.findElement(By.id("tester")));
    actions.perform();/*from   ww w  .  j  ava2s . c  o  m*/

    verifyAlerts(driver, getExpectedAlerts());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs/*  w  w w . java  2s .co  m*/
 */
@Test
@Alerts(DEFAULT = { "o-mouse over [option1]", "s-mouse over [option1]" }, IE = {})
public void mouseOver() throws Exception {
    shutDownRealIE();

    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html>\n" + "  <head>\n"
            + "    <title>Test</title>\n" + "    <script>\n" + "    function dumpEvent(event, pre) {\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 = pre + '-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"
            + "    <select name='select1' id='select1' size='2' onmouseover='dumpEvent(event, \"s\");' >\n"
            + "      <option value='option1' id='option1' onmouseover='dumpEvent(event, \"o\");' >Option1</option>\n"
            + "      <option value='option2' id='option2'>Option2</option>\n" + "    </select>\n"
            + "  </form>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    final Actions actions = new Actions(driver);
    actions.moveToElement(driver.findElement(By.id("option1")));
    actions.perform();

    verifyAlerts(driver, getExpectedAlerts());
}

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

License:Apache License

/**
 * @throws Exception if an error occurs//from   ww w  .j a  va  2  s. c om
 */
@Test
@Alerts(DEFAULT = "o-mouse over [option1]", FF = { "o-mouse over [option1]",
        "s-mouse over [option1]" }, IE = {})
public void mouseOverDisabledSelect() throws Exception {
    final String html = HtmlPageTest.STANDARDS_MODE_PREFIX_ + "<html>\n" + "  <head>\n"
            + "    <title>Test</title>\n" + "    <script>\n" + "    function dumpEvent(event, pre) {\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 = pre + '-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"
            + "    <select name='select1' id='select1' size='2' disabled='disabled' "
            + "onmouseover='dumpEvent(event, \"s\");' >\n"
            + "      <option value='option1' id='option1' onmouseover='dumpEvent(event, \"o\");'>Option1</option>\n"
            + "      <option value='option2' id='option2'>Option2</option>\n" + "    </select>\n"
            + "  </form>\n" + "</body></html>";

    final WebDriver driver = loadPage2(html);
    final Actions actions = new Actions(driver);
    actions.moveToElement(driver.findElement(By.id("option1")));
    actions.perform();

    verifyAlerts(driver, getExpectedAlerts());
}