On longPress in Android device(Google Pixel) - InvalidElementStateException: Unable to perform W3C actions. Check logcat output

@Aleksei I’ m getting this error: org.openqa.selenium.InvalidStateException: Unable to perform W3C actions. Check the logcat output for possible error reports and make sure your input actions chain is valid.

I’m using appium 2.0 and performing a longPress action on an element where I’m getting this issue intermittently. The device being used in Google Pixel on Android version 12.

Any idea of what could be going wrong??

No idea without your code including 1-2 actions before.
0 ios or android ?
1 how you find element ?
2 how you make long tap ?

@Aleksei

0: Its an Android device - Google Pixel and Android version 12

1: I use @AndroidFindBy annotation implementing the pagefactory model to find the element:-
@AndroidFindBy(uiAutomator = “new UiSelector().descriptionContains("App Info")”)
WebElement appInfoBtn;

2: Use the below longPress() method to implement the same with below code snippet:
public void longPress(AppiumDriver driver, WebElement ele) {
Point location = ele.getLocation();
Dimension size = ele.getSize();
Point centreofElem = getCenterofElem(location, size);

    PointerInput pressFinger = new PointerInput(PointerInput.Kind.TOUCH,"finger");
    Sequence seq = new Sequence(pressFinger, 1)
            .addAction(pressFinger.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), centreofElem))
            .addAction(pressFinger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()))
            .addAction(new Pause(pressFinger, Duration.ofSeconds(1)))
            .addAction(pressFinger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));

             driver.perform(Collections.singletonList(seq));
}

and you get exception on below line?

@Aleksei
Yes, exactly it fails on this very line: driver.perform(Collections.singletonList(seq));

Also, this is the below code to getCenterofElement:

private Point getCenterofElem(Point locn, Dimension size) {
int leftX = locn.getX();
int midX = leftX + (size.getWidth()/2);

int upperY = locn.getY();
int midY = upperY + (size.getHeight()/2);

return new Point(midX, midY);
}

Did you try add simple sleep for e.g. 5 sec before LongTap on element BUT after needed screen loaded?
Just to check…

@Aleksei So what are u suggesting in this case?? Should I add some sleep before performing the longPress?? In case so should I add it before calling the longPress() method or before this line- driver.perform(Collections.singletonList(seq))

Post this longPress method, I wait for the next screen 1st element visibility (which is basically a textField) by using a fluentwait and Expectedconditions.visibilityofelemntLocated() so I guess that should take care of screenLoad check.

You did not answer: does simple sleep helps you?

I still hardly believe that InvalidStateException happens inside driver.perform(Collections.singletonList(seq));. As far as NO element here and long tap happens by coordinates.

I haven’t added any sleep as of now and that’s what I asked u previously…Do I need to add sleep and if so then at which step??

Should I add it before longPress?? And if so then at which point?? after getting the co-ordinates or before getting it??

see question above.

Also you can try other solution → appium-uiautomator2-driver/docs/android-mobile-gestures.md at master · appium/appium-uiautomator2-driver · GitHub

@Aleksei
I did try with sleep and that isn’t working out…But with the other suggestion of yours related to mobile gesture seems to work fine for now…Let me try with that for couple of runs on Jenkins…I hope so that should be stable and work smoothly !!

Thanks much for your suggestion !! Will update on this thread if the mobile gesture is working seamlessly for this issue !!

Hi,
I got into similar issue. I am using following versions of dependencies:

  • Appium server 2.4.1
  • Appium UiAutomator2 driver 2.45.1
  • Appium Java Client 9.2.3

I had a cutom long press code implemented via W3C Gesture that moves to center of element, press down on element, hold for 5 seconds, and then move up the pointer. Something like this:

PointerInput finger1 = new PointerInput(PointerInput.Kind.TOUCH, "finger1");

        Sequence sequence = new Sequence(finger1, 1)
                .addAction(finger1.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), point.x, point.y))
                .addAction(finger1.createPointerDown(PointerInput.MouseButton.LEFT.asArg()))
                .addAction(new Pause(finger1, touchDownDuration))
                .addAction(finger1.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));

driver.perform(Collections.singletonList(sequence));

It was all working fine both locally on my real device Samsung Galaxy S20 Android 13, and remotely on BrowserStack real device Samsung Galaxy S24 Ultra Android 14.

I started facing this issue on remote execution which was throwing following error:

org.openqa.selenium.InvalidElementStateException: Unable to perform W3C actions. Check the logcat output for possible error reports and make sure your input actions chain is valid.
Build info: version: '4.19.0', revision: '5f9cec8963'
System info: os.name: 'Mac OS X', os.arch: 'aarch64', os.version: '14.5', java.version: '17.0.2'
Driver info: io.appium.java_client.android.AndroidDriver
Command: [1286270c040ee4f6560f951c4d1a2393e79a13d9, actions {actions=[org.openqa.selenium.interactions.Sequence@626b639e]}]
at [email protected]/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at [email protected]/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77)
	at [email protected]/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at [email protected]/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
	at [email protected]/java.lang.reflect.Constructor.newInstance(Constructor.java:480)
	at app//org.openqa.selenium.remote.ErrorCodec.decode(ErrorCodec.java:167)
	at app//org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:138)
	at app//org.openqa.selenium.remote.codec.w3c.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:50)
	at app//org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:190)
	at app//io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:237)
	at app//org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:518)
	at app//org.openqa.selenium.remote.RemoteWebDriver.perform(RemoteWebDriver.java:604)
	at app//com.dcs.appium.core.utils.GestureUtils.tap(GestureUtils.java:112)

I have a test case where I needed to long press multiple elements on screen to retrieve the copied text from clipboard. It works fine on long pressing the first element, however when it attempts to execute long press on second element then I get above error.
In my opinion, this is not related to element since I tried interchanging or trying with different elements and it always worked fine for first long press action, and on subsequent long presses I got this error.
My hypothesis for this error is that when an element is long pressed, then Android displays a system toaster message indicating text is copied to clipboard which stays on screen for sometime, and if we attempt to long press another element while this toaster is being displayed, then we get this error. However I doubt this is the case, because it still works fine locally and toaster is not obstructing the view of element to be clicked.

I inspected the device logs from logcat but i could not find anything related. The error message also does not point to what exactly to look for in device logs.

Nevertheless, I tried the long press from appium-uiautomator2-driver/docs/android-mobile-gestures.md at master · appium/appium-uiautomator2-driver · GitHub which solved the issue.

I am still curious to understand what could possibly have gone wrong all of a sudden as the code was running just fine with same libraries and device under test previously. The only change that I can think of is extra capability that I added androidOptions.setDisableSuppressAccessibilityService(false); because it is false by default however BrowserStack sets it to true so i am passing it explicitly to make it false.

Tried disable animation capability with android? It may help to reduce state element issue