6

I was searching for the "right", or the "latest" way to create Tap/Swipe/Drag etc. events using the latest (at this point) Appium Java-client 6.1.0. I saw different documentations in the Appium site (Tap using TouchActions, Touch using TouchAction), and there is no reference as which should i use (and which is going to be deprecated?).

new TouchAction(driver)
    .tap(tapOptions()
    .withElement(element(myElement)))
    .perform();

new TouchActions(driver)
    .singleTap(myElement)
    .perform();

It seems that TouchActions is a part of the Selenium project and TouchAction is a part of the Appium, but it does not mean that the Appium is the correct way.

p.s I am using at the moment Chrome/Safari browsers for Android/iOS the testing, but that does not mean that i don't need native apps support for the code.

Thank you for your time

Leon Proskurov
  • 135
  • 1
  • 14

2 Answers2

4

You want to use TouchAction (Appium).

Below is a section of my code. The first is the general scroll function which takes coordinates as parameters. You generally would not call that function directly, it was meant to be called by other functions, like the scrollDown function I've included below it, that calculates the coordinates and calls the general scroll function.

Hope this helps.

/**
 * This method scrolls based upon the passed parameters
 * @author Bill Hileman
 * @param int startx - the starting x position
 * @param int starty - the starting y position
 * @param int endx - the ending x position
 * @param int endy - the ending y position
 */
@SuppressWarnings("rawtypes")
public void scroll(int startx, int starty, int endx, int endy) {

    TouchAction touchAction = new TouchAction(driver);

    touchAction.longPress(PointOption.point(startx, starty))
               .moveTo(PointOption.point(endx, endy))
               .release()
               .perform();

}

/**
 * This method does a swipe upwards
 * @author Bill Hileman
 */
public void scrollDown() {

    //The viewing size of the device
    Dimension size = driver.manage().window().getSize();

    //Starting y location set to 80% of the height (near bottom)
    int starty = (int) (size.height * 0.80);
    //Ending y location set to 20% of the height (near top)
    int endy = (int) (size.height * 0.20);
    //x position set to mid-screen horizontally
    int startx = (int) size.width / 2;

    scroll(startx, starty, startx, endy);

}
Bill Hileman
  • 2,798
  • 2
  • 17
  • 24
  • Thank you for your quick answer. Is it possible that you can explain why this is the correct action to use? The implementation is not hard, the right object to use by appium design is the real question. – Leon Proskurov Nov 06 '18 at 16:55
  • 2
    Quite frankly, in my own opinion, it is the correct action to use because it was the ONLY option I could find that would work at all. I, too, noticed the on-line documentation for Appium, and tried the "alternative" methods, including using javascript. None of them worked I believe (but I could be wrong) that the TouchActions class of Selenium was intended more for touch devices like touchscreens, and not necessarily mobile devices. It's also implemented in an entirely different way - Appium using REST services to communicate vs Selenium using browser emulation. – Bill Hileman Nov 06 '18 at 17:57
  • 1
    There is a scroll action in Appium: `TouchActions action = new TouchActions(driver); action.scroll(element, 10, 100); action.perform();` [Appium official usage](http://appium.io/docs/en/commands/interactions/touch/scroll) – Leon Proskurov Nov 07 '18 at 11:45
  • 1
    I seem to recall using that in earlier projects, but when I tried using it with the newer versions of Appium/Selenium it did not work at all. It was a frustrating research until I found this solution. I could be wrong, the alternative method(s) may work again since various updates, I just haven't bothered checking since what I have now works. – Bill Hileman Nov 07 '18 at 12:54
4

The latest approach to using TouchAction class is to use AndroidTouchAction class instead of TouchAction class as the same is now made generic. That's why you see @SuppressWarnings("rawtypes") being used in the last answer.

This is how you will tap on an element in 6.1.0

For Android:

AndroidTouchAction touch = new AndroidTouchAction (driver);
touch.tap (TapOptions.tapOptions ()
    .withElement (ElementOption.element (e)))
  .perform ();

For iOS:

IOSTouchAction touch = new IOSTouchAction (driver);
touch.tap (TapOptions.tapOptions ()
    .withElement (ElementOption.element (e)))
  .perform ();
Wasiq Bhamla
  • 949
  • 1
  • 7
  • 12