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

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

Why do you have this capability:
dc.setCapability(“BROWSER_NAME”, “android”);?
deviceName, platformName, platformVersion would be suitable.
And try to use my example provided above

im taking the same prob, using AndroidDriver or RemoteWebDriver, i cant use TouchAction…

I see method

tap(WebElement el) has been removed from Appium Java Client 6.x onwards.

Are you aware of any specific reasons ? Certainly this method is more useful and readable than newer methods using TapOptions and PointOptions. :thinking:

Could you please share us, LongPress on coordinates with duration? I have tried but I can’t. What I have tried

//LongPress on coordinates with duration:
new TouchAction((AndroidDriver)driver).longPress(longPressOptions().withElement((ElementOption) point(xPoint, yPoint)).withDuration(Duration.ofMillis(duration))).release().perform();

1 Like

dude,
I searched for every fck place on the internet and 3 days looking I find your code !! thx man !!! help me a lot

hi,

Swipe not working with co ordinates and press pointoption.
Using java-client-7.0.0

Can you please help me.

Please find the code and log below.


public class Unit3 {

public static void main(String[] args) throws MalformedURLException {
	// TODO Auto-generated method stub
	
	File appDir = new File("src");
	File appName = new File(appDir, "ApiDemos-debug.apk");
	
	DesiredCapabilities capability = new DesiredCapabilities();
	capability.setCapability(MobileCapabilityType.DEVICE_NAME, "Nexus6");
	capability.setCapability(MobileCapabilityType.PLATFORM_NAME, Platform.ANDROID);
	capability.setCapability(MobileCapabilityType.APP, appName.getAbsolutePath());
	
	AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"),capability);
	driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);
	
	driver.findElementByXPath("//android.widget.TextView[@text='Views']").click();
	
    boolean flag = true;
	
	do
    {			
        TouchAction touch = new TouchAction(driver);                   
              
		
		Dimension size = driver.manage().window().getSize();
		int startx = size.getWidth()/2;
		int endx = size.getWidth()/2;
		int starty = (int)(size.getHeight()* 0.80);
		int endy = (int)(size.getHeight()* 0.10);	
		touch.press(PointOption.point(startx, starty)).waitAction().moveTo(PointOption.point(endx, endy)).release().perform();
		
        List<AndroidElement> ls = driver.findElementsByClassName("android.widget.TextView");
        for(int i=0;i<ls.size();i++)
        {
        	 if(ls.get(i).getText().contains("Seek")){
        		 touch.tap(tapOptions().withElement(element(ls.get(i)))).perform();
        		 flag=false;
  			     break; 		
                	 }
        }   			
						
    }while(flag=true);     	
}


}

[HTTP] <-- GET /wd/hub/session/353eb1f7-d777-4c51-b723-a7685d8eec19/window/rect 200 37 ms - 50

[HTTP]

[HTTP] --> POST /wd/hub/session/353eb1f7-d777-4c51-b723-a7685d8eec19/touch/perform

[HTTP] {“actions”:[{“action”:“press”,“options”:{“x”:720,“y”:1913}},{“action”:“wait”,“options”:{}},{“action”:“moveTo”,“options”:{“x”:720,“y”:239}},{“action”:“release”,“options”:{}}]}

[debug] [W3C (353eb1f7)] Calling AppiumDriver.performTouch() with args: [[{“action”:“press”,“options”:{“x”:720,“y”:1913}},{“action”:“wait”,“options”:{}},{“action”:“moveTo”,“options”:{“x”:720,“y”:239}},{“action”:“release”,“options”:{}}],“353eb1f7-d777-4c51-b723-a7685d8eec19”]

[debug] [AndroidBootstrap] Sending command to android: {“cmd”:“action”,“action”:“swipe”,“params”:{“startX”:720,“startY”:1913,“endX”:720,“endY”:239,“steps”:22}}

[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION

[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: getDeviceSize

[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {“status”:0,“value”:{“height”:2392,“width”:1440}}

[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {“cmd”:“action”,“action”:“swipe”,“params”:{“startX”:720,“startY”:1913,“endX”:720,“endY”:239,“steps”:22}}

[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type ACTION

[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command action: swipe

[debug] [AndroidBootstrap] Received command result from bootstrap

[debug] [MJSONWP] Matched JSONWP error code 13 to UnknownError

[debug] [W3C (353eb1f7)] Encountered internal error running command: UnknownError: An unknown server-side error occurred while processing the command. Original error: The swipe did not complete successfully

[debug] [W3C (353eb1f7)] at errorFromMJSONWPStatusCode (C:\Users\sures\AppData\Roaming\npm\node_modules\appium\node_modules\appium-base-driver\lib\protocol\errors.js:789:10)

[debug] [W3C (353eb1f7)] at Socket.socketClient.on.data (C:\Users\sures\AppData\Roaming\npm\node_modules\appium\node_modules\appium-android-driver\lib\bootstrap.js:139:18)

[debug] [W3C (353eb1f7)] at Socket.emit (events.js:189:13)

[debug] [W3C (353eb1f7)] at addChunk (_stream_readable.js:284:12)

[debug] [W3C (353eb1f7)] at readableAddChunk (_stream_readable.js:261:13)

[debug] [W3C (353eb1f7)] at Socket.Readable.push (_stream_readable.js:220:10)

[debug] [W3C (353eb1f7)] at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)

[HTTP] <-- POST /wd/hub/session/353eb1f7-d777-4c51-b723-a7685d8eec19/touch/perform 500 76 ms - 927

[HTTP]

[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Display bounds: [0,0][1440,2392]

[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Display bounds: [0,0][1440,2392]

[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Swiping from [x=720.0, y=1913.0] to [x=720.0, y=239.0] with steps: 22[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {“status”:13,“value”:“The swipe did not complete successfully”}

[debug] [AndroidBootstrap] Emitting alert message…

[debug] [AndroidBootstrap] [BOOTSTRAP LOG] [debug] Emitting system alert message

@Telmo_Cardoso can you tell me how to find out coordinate or scroll horizontal and click element.
i’m using intellij and i’m not getting getElement.

sky if possible.

skype id : chetan thummar

Hi @anu_suryey,
you can try the below mentioned method

public void StoAndroidMobile(By strobj1,By strobj2)

{

try {

boolean isFoundTheElement_1 = driver.findElements(strobj1).size() > 0;

boolean isFoundTheElement_2 = driver.findElements(strobj2).size() > 0;

while (isFoundTheElement_1 == true || isFoundTheElement_2 == true){

Dimension sizes_1 = driver.findElement(strobj1).getSize();

Dimension sizes_2 = driver.findElement(strobj2).getSize();

int obj1_height = sizes_1.getHeight();

int obj1_width = sizes_1.getWidth();

int obj2_height = sizes_2.getHeight();

int obj2_width = sizes_2.getWidth();

System.out.println(sizes_2);

new TouchAction(AndroidDriver).press(PointOption.point(obj1_width, obj1_height).waitAction().moveTo(PointOption.point(obj2_width, obj2_height)).release().perform();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

-Peace out !
Thiru

Hello

My code don’t click with coordinates.

package br.alex.appium.page;

import static br.alex.appium.core.DriverFactory.getDriver;

import org.openqa.selenium.By;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.touch.TouchActions;

import br.alex.appium.core.BasePage;
import br.alex.appium.core.DriverFactory;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.touch.TapOptions;
import io.appium.java_client.touch.offset.ElementOption;

public void clicarBotaoMais() {
	MobileElement botao = getDriver().findElement(By.xpath("//*[@text='(+)']"));

	new TouchAction(getDriver())
		.tap(TapOptions.tapOptions()
		.withElement(ElementOption.element(botao, -500, -100)))
		.release()
		.perform();

}

Can we help-me?

Alex Borelli

Hello guys,
trying to excute the following two commands:

new TouchAction(driver).longPress(753, 1483).moveTo(626, 503).release().perform();

new TouchAction(driver).longPress(753, 1483).waitAction(Duration.ofMillis(672)).moveTo(626, 503).release().perform();

but getting the following problems in eclipse:

The method longPress(LongPressOptions) in the type TouchAction is not applicable for the arguments

and following warning

TouchAction is a raw type. References to generic type TouchAction should be parameterized

Could you suggest how to re-write the commmands with Appium 6 please ?

Thanks a lot,
Giuseppe