org.openqa.selenium.interactions.Action Java Examples

The following examples show how to use org.openqa.selenium.interactions.Action. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TouchFlickTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@NeedsFreshDriver
@Test
public void testCanFlickHorizontally() {
  driver.get(pages.clicksPage);
  driver.get(pages.longContentPage);

  WebElement link = driver.findElement(By.id("link1"));
  int x = link.getLocation().x;
  // The element is located at the right of the page,
  // so it is not initially visible on the screen.
  assertThat(x).isGreaterThan(1500);

  Action flick = getBuilder(driver).flick(1000, 0).build();
  flick.perform();
  x = link.getLocation().x;
  // After flicking, the element should now be visible on the screen.
  assertThat(x).isLessThan(1500);
}
 
Example #2
Source File: TouchScrollTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@NeedsFreshDriver
@Test
public void testCanScrollHorizontally() {
  driver.get(pages.longContentPage);

  WebElement link = driver.findElement(By.id("link1"));
  int x = link.getLocation().x;
  // The element is located at the right of the page,
  // so it is not initially visible on the screen.
  assertThat(x).isGreaterThan(1500);

  Action scrollDown = getBuilder(driver).scroll(400, 0).build();
  scrollDown.perform();

  x = link.getLocation().y;
  // After scrolling, the location of the element should change accordingly.
  assertThat(x).isLessThan(1500);
}
 
Example #3
Source File: TouchScrollTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@NeedsFreshDriver
@Test
public void testCanScrollVertically() {
  driver.get(pages.longContentPage);

  WebElement link = driver.findElement(By.id("link3"));
  int y = link.getLocation().y;
  // The element is located at the right of the page,
  // so it is not initially visible on the screen.
  assertThat(y).isGreaterThan(4200);

  Action scrollDown = getBuilder(driver).scroll(0, 800).build();
  scrollDown.perform();

  y = link.getLocation().y;
  // After scrolling, the location of the element should change accordingly.
  assertThat(y).isLessThan(3500);
}
 
Example #4
Source File: TouchScrollTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@NeedsFreshDriver
@Test
public void testCanScrollHorizontallyFromWebElement() {
  driver.get(pages.longContentPage);

  WebElement link = driver.findElement(By.id("link1"));
  int x = link.getLocation().x;
  // The element is located at the right of the page,
  // so it is not initially visible on the screen.
  assertThat(x).isGreaterThan(1500);

  WebElement toScroll = driver.findElement(By.id("imagestart"));
  Action scroll = getBuilder(driver).scroll(toScroll, -1000, 0).build();
  scroll.perform();

  x = link.getLocation().x;
  // After scrolling, the location of the element should change accordingly.
  assertThat(x).isLessThan(1500);
}
 
Example #5
Source File: TouchScrollTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@NeedsFreshDriver
@Test
public void testCanScrollVerticallyFromWebElement() {
  driver.get(pages.longContentPage);

  WebElement link = driver.findElement(By.id("link3"));
  int y = link.getLocation().y;
  // The element is located at the right of the page,
  // so it is not initially visible on the screen.
  assertThat(y).isGreaterThan(4200);

  WebElement toScroll = driver.findElement(By.id("imagestart"));
  Action scroll = getBuilder(driver).scroll(toScroll, 0, -800).build();
  scroll.perform();

  y = link.getLocation().y;
  // After scrolling, the location of the element should change accordingly.
  assertThat(y).isLessThan(3500);
}
 
Example #6
Source File: TouchDoubleTapTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@Test
public void testCanDoubleTapOnAnImageAndAlterLocationOfElementsInScreen() {
  driver.get(pages.longContentPage);

  WebElement image = driver.findElement(By.id("imagestart"));
  int y = ((Locatable) image).getCoordinates().inViewPort().y;
  // The element is located at a certain point, after double tapping,
  // the y coordinate must change.
  assertThat(y).isGreaterThan(100);

  Action doubleTap = getBuilder(driver).doubleTap(image).build();
  doubleTap.perform();

  y = ((Locatable) image).getCoordinates().inViewPort().y;
  assertThat(y).isLessThan(50);
}
 
Example #7
Source File: TouchFlickTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@NeedsFreshDriver
@Test
public void testCanFlickVerticallyFast() {
  driver.get(pages.longContentPage);

  WebElement link = driver.findElement(By.id("link4"));
  int y = link.getLocation().y;
  // The element is located at the bottom of the page,
  // so it is not initially visible on the screen.
  assertThat(y).isGreaterThan(8700);

  Action flick = getBuilder(driver).flick(0, 1500).build();
  flick.perform();
  y = link.getLocation().y;
  // After flicking, the element should now be visible on the screen.
  assertThat(y).isLessThan(4000);
}
 
Example #8
Source File: TouchFlickTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@NeedsFreshDriver
@Test
public void testCanFlickVertically() {
  driver.get(pages.longContentPage);

  WebElement link = driver.findElement(By.id("link3"));
  int y = link.getLocation().y;
  // The element is located at the bottom of the page,
  // so it is not initially visible on the screen.
  assertThat(y).isGreaterThan(4200);

  Action flick = getBuilder(driver).flick(0, 750).build();
  flick.perform();
  y = link.getLocation().y;

  // After flicking, the element should now be visible on the screen.
  assertThat(y).isLessThan(4200);
}
 
Example #9
Source File: TouchFlickTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@NeedsFreshDriver
@Test
public void testCanFlickVerticallyFastFromWebElement() {
  driver.get(pages.longContentPage);

  WebElement link = driver.findElement(By.id("link4"));
  int y = link.getLocation().y;
  // The element is located at the bottom of the page,
  // so it is not initially visible on the screen.
  assertThat(y).isGreaterThan(8700);

  WebElement toFlick = driver.findElement(By.id("imagestart"));
  Action flick = getBuilder(driver).flick(toFlick, 0, -600, FlickAction.SPEED_FAST).build();
  flick.perform();
  y = link.getLocation().y;
  // After flicking, the element should now be visible on the screen.
  assertThat(y).isLessThan(8700);
}
 
Example #10
Source File: TouchFlickTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@NeedsFreshDriver
@Test
public void testCanFlickVerticallyFromWebElement() {
  driver.get(pages.longContentPage);

  WebElement link = driver.findElement(By.id("link3"));
  int y = link.getLocation().y;
  // The element is located at the bottom of the page,
  // so it is not initially visible on the screen.
  assertThat(y).isGreaterThan(4200);

  WebElement toFlick = driver.findElement(By.id("imagestart"));
  Action flick = getBuilder(driver).flick(toFlick, 0, -600, FlickAction.SPEED_NORMAL).build();
  flick.perform();
  y = link.getLocation().y;
  // After flicking, the element should now be visible on the screen.
  assertThat(y).isLessThan(4000);
}
 
Example #11
Source File: TouchFlickTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@NeedsFreshDriver
@Test
public void testCanFlickHorizontallyFast() {
  driver.get(pages.longContentPage);

  WebElement link = driver.findElement(By.id("link2"));
  int x = link.getLocation().x;
  // The element is located at the right of the page,
  // so it is not initially visible on the screen.
  assertThat(x).isGreaterThan(3500);

  Action flick = getBuilder(driver).flick(1500, 0).build();
  flick.perform();
  x = link.getLocation().x;
  // After flicking, the element should now be visible on the screen.
  assertThat(x).isLessThan(3000);
}
 
Example #12
Source File: ActionsTest.java    From Selenium-WebDriver-3-Practical-Guide-Second-Edition with MIT License 6 votes vote down vote up
@Test
public void shouldPerformCompositeAction() {

    driver.get("http://guidebook.seleniumacademy.com/Selectable.html");

    WebElement one = driver.findElement(By.name("one"));
    WebElement three = driver.findElement(By.name("three"));
    WebElement five = driver.findElement(By.name("five"));

    // Add all the actions into the Actions actions.
    Actions actions = new Actions(driver);
    actions.keyDown(Keys.CONTROL)
            .click(one)
            .click(three)
            .click(five)
            .keyUp(Keys.CONTROL);

    // Generate the composite action.
    Action compositeAction = actions.build();

    // Perform the composite action.
    compositeAction.perform();
}
 
Example #13
Source File: TouchFlickTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@NeedsFreshDriver
@Test
public void testCanFlickHorizontallyFastFromWebElement() {
  driver.get(pages.longContentPage);

  WebElement toFlick = driver.findElement(By.id("imagestart"));
  WebElement link = driver.findElement(By.id("link2"));
  int x = link.getLocation().x;
  // The element is located at the right of the page,
  // so it is not initially visible on the screen.
  assertThat(x).isGreaterThan(3500);

  Action flick = getBuilder(driver).flick(toFlick, -400, 0, FlickAction.SPEED_FAST).build();
  flick.perform();
  x = link.getLocation().x;
  // After flicking, the element should now be visible on the screen.
  assertThat(x).isLessThan(3500);
}
 
Example #14
Source File: TouchFlickTest.java    From selenium with Apache License 2.0 6 votes vote down vote up
@NeedsFreshDriver
@Test
public void testCanFlickHorizontallyFromWebElement() {
  driver.get(pages.longContentPage);

  WebElement toFlick = driver.findElement(By.id("imagestart"));
  WebElement link = driver.findElement(By.id("link1"));
  int x = link.getLocation().x;
  // The element is located at the right of the page,
  // so it is not initially visible on the screen.
  assertThat(x).isGreaterThan(1500);

  Action flick = getBuilder(driver).flick(toFlick, -1000, 0, FlickAction.SPEED_NORMAL).build();
  flick.perform();

  x = link.getLocation().x;
  // After flicking, the element should now be visible on the screen.
  assertThat(x).isLessThan(1500);
}
 
Example #15
Source File: SelenideAddons.java    From neodymium-library with MIT License 6 votes vote down vote up
/**
 * Drag and drop an element to a given position. The position will be set by the user. It drags the element and
 * moves it to a specific position of the respective slider.
 * 
 * @param elementToMove
 *            The selector of the slider to drag and drop
 * @param elementToCheck
 *            The locator of the slider value
 * @param horizontalMovement
 *            The offset for the horizontal movement
 * @param verticalMovement
 *            The offset for the vertical movement
 * @param pauseBetweenMovements
 *            Time to pass after the slider do the next movement step
 * @param retryMovements
 *            Amount of retries the slider will be moved
 * @param condition
 *            The condition for the slider to verify the movement
 */

public static void dragAndDropUntilCondition(SelenideElement elementToMove, SelenideElement elementToCheck, int horizontalMovement, int verticalMovement,
                                             int pauseBetweenMovements, int retryMovements, Condition condition)
{
    Actions moveSlider = new Actions(Neodymium.getDriver());

    int counter = 0;
    while (!elementToCheck.has(condition))
    {
        if (counter > retryMovements)
        {
            SelenideAddons.wrapAssertionError(() -> {
                Assert.assertTrue("CircutBreaker: Was not able to move the element and to reach the condition. Tried: " + retryMovements
                                  + " times to move the element.", false);
            });
        }
        Action action = moveSlider.dragAndDropBy(elementToMove.getWrappedElement(), horizontalMovement, verticalMovement).build();
        action.perform();
        Selenide.sleep(pauseBetweenMovements);
        counter++;
    }
}
 
Example #16
Source File: RealHtmlElement.java    From ats-framework with Apache License 2.0 6 votes vote down vote up
/**
 * Drag and drop an element on top of other element
 * @param targetElement the target element
 */
@Override
@PublicAtsApi
public void dragAndDropTo(
                           HtmlElement targetElement ) {

    new RealHtmlElementState(this).waitToBecomeExisting();

    WebElement source = RealHtmlElementLocator.findElement(this);
    WebElement target = RealHtmlElementLocator.findElement(targetElement);

    Actions actionBuilder = new Actions(webDriver);
    Action dragAndDropAction = actionBuilder.clickAndHold(source)
                                            .moveToElement(target, 1, 1)
                                            .release(target)
                                            .build();
    dragAndDropAction.perform();

    // drops the source element in the middle of the target, which in some cases is not doing drop on the right place
    // new Actions( webDriver ).dragAndDrop( source, target ).perform();
}
 
Example #17
Source File: HiddenHtmlElement.java    From ats-framework with Apache License 2.0 6 votes vote down vote up
/**
 * Drag and drop an element on top of other element
 * @param targetElement the target element
 */
@Override
@PublicAtsApi
public void dragAndDropTo(
                           HtmlElement targetElement ) {

    new HiddenHtmlElementState(this).waitToBecomeExisting();

    WebElement source = HiddenHtmlElementLocator.findElement(this);
    WebElement target = HiddenHtmlElementLocator.findElement(targetElement);

    Actions actionBuilder = new Actions(htmlUnitDriver);
    Action dragAndDropAction = actionBuilder.clickAndHold(source)
                                            .moveToElement(target, 1, 1)
                                            .release(target)
                                            .build();
    dragAndDropAction.perform();

    // drops the source element in the middle of the target, which in some cases is not doing drop on the right place
    // new Actions( htmlUnitDriver ).dragAndDrop( source, target ).perform();
}
 
Example #18
Source File: AllPostsPage.java    From Selenium-WebDriver-3-Practical-Guide-Second-Edition with MIT License 6 votes vote down vote up
private void goToParticularPostPage(String title) {
    List<WebElement> allPosts
            = postsContainer.findElements(By.className("title"));
    for (WebElement ele : allPosts) {
        if (ele.getText().equals(title)) {
            Actions builder = new Actions(driver);
            builder.moveToElement(ele);
            builder.click(driver.findElement(
                    By.cssSelector(".edit>a")));
            // Generate the composite action.
            Action compositeAction = builder.build();
            // Perform the composite action.
            compositeAction.perform();
            break;
        }
    }
}
 
Example #19
Source File: CanvasTest.java    From webdrivermanager-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void test() throws InterruptedException {
    driver.get("http://szimek.github.io/signature_pad/");

    WebElement canvas = driver.findElement(By.tagName("canvas"));
    Action action = new Actions(driver).click(canvas)
            .moveToElement(canvas, 8, 8).clickAndHold(canvas)
            .moveByOffset(120, 120).moveByOffset(-120, 120)
            .moveByOffset(-120, -120).moveByOffset(8, 8).release(canvas)
            .build();
    action.perform();

    Thread.sleep(5000);
}
 
Example #20
Source File: HtmlSelect2Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @exception Exception If the test fails
 */
@Test
@Alerts({"false", "true", "false", "true"})
@BuggyWebDriver(IE = {"false", "false", "true", "false"})
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();

    assertEquals(Boolean.parseBoolean(getExpectedAlerts()[0]), options.get(0).isSelected());
    assertEquals(Boolean.parseBoolean(getExpectedAlerts()[1]), options.get(1).isSelected());
    assertEquals(Boolean.parseBoolean(getExpectedAlerts()[2]), options.get(2).isSelected());
    assertEquals(Boolean.parseBoolean(getExpectedAlerts()[3]), options.get(3).isSelected());
}
 
Example #21
Source File: HtmlSelect2Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @exception Exception If the test fails
 */
@Test
@Alerts({"false", "true", "true", "true"})
@BuggyWebDriver(IE = {"false", "false", "true", "false"})
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();

    assertEquals(Boolean.parseBoolean(getExpectedAlerts()[0]), options.get(0).isSelected());
    assertEquals(Boolean.parseBoolean(getExpectedAlerts()[1]), options.get(1).isSelected());
    assertEquals(Boolean.parseBoolean(getExpectedAlerts()[2]), options.get(2).isSelected());
    assertEquals(Boolean.parseBoolean(getExpectedAlerts()[3]), options.get(3).isSelected());
}
 
Example #22
Source File: HtmlSelect2Test.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * @exception Exception If the test fails
 */
@Test
@Alerts({"false", "false", "false", "true"})
@BuggyWebDriver(IE = {"false", "false", "true", "false"})
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();

    assertEquals(Boolean.parseBoolean(getExpectedAlerts()[0]), options.get(0).isSelected());
    assertEquals(Boolean.parseBoolean(getExpectedAlerts()[1]), options.get(1).isSelected());
    assertEquals(Boolean.parseBoolean(getExpectedAlerts()[2]), options.get(2).isSelected());
    assertEquals(Boolean.parseBoolean(getExpectedAlerts()[3]), options.get(3).isSelected());
}
 
Example #23
Source File: ConnectedCupDeviceInterface.java    From product-iots with Apache License 2.0 4 votes vote down vote up
/**
 * This method performs the slider change action of the web interface.
 * @param slider : The element of the slider to be changed.
 * @param val : Value to be set.
 */
private void moveSlider(WebElement slider, int val) {
    Actions move = new Actions(driver);
    Action action = move.dragAndDropBy(slider, 0, val).build();
    action.perform();
}
 
Example #24
Source File: TouchSingleTapTest.java    From selenium with Apache License 2.0 4 votes vote down vote up
private void singleTapOnElement(String elementId) {
  WebElement toSingleTap = driver.findElement(By.id(elementId));
  Action singleTap = getBuilder(driver).singleTap(toSingleTap).build();
  singleTap.perform();
}