How to use touch action in new java client 6.0.0-BETA4

Using latest java client i am not able to use touch action. can anyone help me figure it out.
thanks in advance

having:

MobileElement myElement;
int xPoint, int yPoint;
int duration = XXXX; //in miliseconds

Tap on Element:

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

Tap on Coordinates:

new TouchAction(localdriver).tap(point(xPoint, yPoint)).perform();

Tap on Element using coordinates relative to element:

new TouchAction(localdriver).tap(tapOptions().withElement(element(myElement, xPoint, yPoint))).perform();

LongPress on Element:

new TouchAction(localdriver).longPress(longPressOptions().withElement(element(myElement))).release().perform();

LongPress on Element with Duration:

new TouchAction(localdriver).longPress(longPressOptions().withElement(element(myElement)).withDuration(Duration.ofMillis(duration))).release().perform();

LongPress on coordinates:

new TouchAction(localdriver).longPress(point(xPoint, yPoint)).release().perform();

And so on…

4 Likes

@Telmo_Cardoso i am using below code
new TouchAction<>(getWebDriver()).press(250, 735).waitAction().moveTo(250, -460).release().perform();

using above code it’s showing in eclipse that press and move to method has been Deprecated.

actually main aim to use that code to open Control Center in iOS application, but i am unable to open that using the above swipe method.
So if you can help me “how to open Control Center in iOS using Appium”.

thanks in advance

If you see your methods are not the same as mine, so yes, you are using deprecated methods, although they still work.

About opening Control Center in iOS. On some iPhones I’m not able to, dont know why, but although the swipe is performed, the control panel doesn’t open. But it depends on the iPhone and iOS you are using.

Just tested with iPhone5S - iOS11.2.5 and this works:

gestSwipeVerticalPercentage(1,0.4,0.5,2000);

public void gestSwipeVerticalPercentage(double startPercentage, double finalPercentage, double anchorPercentage, int duration) throws Exception {
    Dimension size = driver.manage().window().getSize();
    int anchor, startPoint, endPoint;
    anchor = (int) (size.width * anchorPercentage);
    startPoint = (int) (size.height * startPercentage);
    endPoint = (int) (size.height * finalPercentage);
    new TouchAction(driver).press(point(anchor, startPoint)).waitAction(waitOptions(Duration.ofMillis(duration)))
            .moveTo(point(anchor, endPoint)).release().perform();
}
1 Like

@Telmo_Cardoso can you plz tell me which java version you are using?

because i am getting error in point and duration, i don’t know why, may be java client issue.
could you plz check.

6.0.0-Beta4, the one you requested :slight_smile:

import those methods:

import java.time.Duration;
import static io.appium.java_client.touch.offset.PointOption.point;
1 Like

@Telmo_Cardoso thanks for the help.
after importing those file i am getting error in WaitOptions :thinking:

Import that too :frowning:

import static io.appium.java_client.touch.WaitOptions.waitOptions;
1 Like

i don’t know why eclipse was not taking that automatically.
thank you @Telmo_Cardoso

Hi Telmo,

Please help me with the below code,

MobileElement elmUserName = (AndroidElement) driver.findElement(By.id(“username”));

ElementOption eoption;
eoption = new ElementOption().withElement(elmUserName);

TouchActions act = new TouchActions(driver);
act.longPress((WebElement) LongPressOptions.longPressOptions().withElement(eoption)).build().perform();

**I am getting below error message


Mar 27, 2018 12:52:57 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
Exception in thread “main” java.lang.ClassCastException: io.appium.java_client.android.AndroidDriver cannot be cast to org.openqa.selenium.interactions.HasTouchScreen
at org.openqa.selenium.interactions.touch.TouchActions.(TouchActions.java:38)
at scripts.TouchActionExample.main(TouchActionExample.java:39)


I am using Java Client 6.0.0. Beta 4 and Selenium-Standalone-server 3.11 jars.

Maybe you wanted to use TouchAction ?

yes right i have resolved it already. I was using touchactions not touchaction. Btw thanks for your help.

Hello,
I just update to java client 6.0.0 and when I’m using TouchAction I get a warning:

The method is working but it’s annoying.

How can I get rid of it?
Thanks

Your driver is instance of what class?

I have a class called Utils were I defined some useful methods, and I have a construction which is passing me the driver( sorry still at the beginning of appium and java and I might get it wrong).
Here is my code:
package resources;
import static io.appium.java_client.touch.TapOptions.tapOptions;
import static io.appium.java_client.touch.WaitOptions.waitOptions;
import static io.appium.java_client.touch.offset.ElementOption.element;
import static io.appium.java_client.touch.offset.PointOption.point;
import static java.time.Duration.ofSeconds;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;

public class Utils {

public static AndroidDriver<AndroidElement> driver;

public Utils(AndroidDriver<AndroidElement> driver) {
	Utils.driver = driver;
}
  public void swipe(int x_start, int y_start, int x_stop, int y_stop, int duration) {
	  new TouchAction(driver).press(point(x_start, y_start)) .waitAction(waitOptions(ofSeconds(duration)))
  .moveTo(point(x_stop, y_stop)) .release() .perform();
  }

The driver is initiating in my TestBase class.

I use java-client 6.1.0 + Desktop 1.6.2, and TouchAction.tap() with WebElement throws UnsupportedCommandException:

org.openqa.selenium.UnsupportedCommandException: Method has not yet been implemented
Build info: version: '3.13.0', revision: '2f0d292'

Apparently Appium server doesn’t support touch yet because the command errors out (404):

[HTTP] --> POST /wd/hub/session/a304c963-e881-4b3c-a0e1-13f32db993b5/touch/perform
[HTTP] {"actions":[{"action":"tap","options":{"element":"0.09312566293240376-11"}}]}
[W3C] Calling AppiumDriver.performTouch() with args: [[{"action":"tap","options":{"element":"0.09312566293240376-11"}}],"a304c963-e881-4b3c-a0e1-13f32db993b5"]
[HTTP] <-- POST /wd/hub/session/a304c963-e881-4b3c-a0e1-13f32db993b5/touch/perform 404 3 ms - 4088

Is there a workaround?

Try to update the appium server to 1.8.1
should work
for me is working:
public void tap(MobileElement el) {
new TouchAction((AndroidDriver) driver).tap(tapOptions().withElement(element(el))).perform();
}

Installed appium server 1.8.1. Same result.

[HTTP] --> POST /wd/hub/session/20d48a51-2a62-43ad-9dda-0acf7f3699d6/touch/perform
[HTTP] {"actions":[{"action":"tap","options":{"element":"0.4547671076436339-11"}}]} [debug] [W3C] Calling AppiumDriver.performTouch() with args:[[{"action":"tap","options":"element":"0.4547671076436339-11"}}],"20d48a51-2a62-43ad-9dda-0acf7f3699d6"]
[HTTP] <-- POST /wd/hub/session/20d48a51-2a62-43ad-9dda-0acf7f3699d6/touch/perform 404 17 ms - 3575

SOLVED – See this post on SO. The author literally saved my week!

What java client do you have?
Make sure you have 6.0.0 or 6.1.0 the stable releases. And check the above examples
I spend like one week until i made it work. I still have the warning but I can like with it.
check this out also

DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(“deviceName”, “emulator-5554”);
dc.setCapability(“BROWSER_NAME”, “android”);
dc.setCapability(“platformName”, “Android”);
dc.setCapability(“appPackage”, “com.poshmark.app”);
dc.setCapability(“appActivity”, “com.poshmark.ui.MainActivity”);
try {
driver = new RemoteWebDriver(new URL(“http://127.0.0.1:4723/wd/hub”), dc);
wait = new WebDriverWait(driver, 10);
} catch (MalformedURLException e1) {
e1.printStackTrace();
}

io.appium.java_client.TouchAction action = new TouchAction((MobileDriver)driver);
action.press(PointOption.point(startx, starty)).moveTo(PointOption.point(startx, endy)).release().perform();

I tried this code, but error occured

Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException: org.openqa.selenium.remote.RemoteWebDriver cannot be cast to io.appium.java_client.MobileDriver
at com.appium.example.FirstAppiumTest.scrollDown(FirstAppiumTest.java:194)
at com.appium.example.FirstAppiumTest.GatherName(FirstAppiumTest.java:206)
at com.appium.example.FirstAppiumTest.actionPerformed(FirstAppiumTest.java:176)
at java.awt.Button.processActionEvent(Unknown Source)
at java.awt.Button.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

Please need help!!!

AddNote: I am using java-client6.1.0.jar