appium/dotnet-client

Cant seem to tap using new code

aaronportier opened this issue · 14 comments

I am use to using the Touch Action but now that it has gone away I am trying to use the new way. There are few examples but I am trying using this and I do not see anything happen: Is this the correct way? I have asked in Appium Discussion, Stack Overflow and ChatGPT and all of them do not give anything.
Actions actions = new Actions(_IOSdriver);
actions.MoveToLocation(23, 315);
actions.Click();
actions.Release();
actions.Perform();

Dor-bl commented

Have you checked those examples?
https://github.com/appium/dotnet-client/blob/main/test%2Fintegration%2FIOS%2FTouchActionTest.cs

Keep in mind that TouchAction is deprecated and will be removed in future release.
From the TouchAction class :

[Obsolete("TouchAction is deprecated, please use W3C actions instead: http://appium.io/docs/en/commands/interactions/actions/")]

Dor-bl commented

This code is taken from python :

actions = ActionChains(driver)

actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch"))
actions.w3c_actions.pointer_action.move_to_location(start_x, start_y)
actions.w3c_actions.pointer_action.pointer_down()
actions.w3c_actions.pointer_action.pause(2)
actions.w3c_actions.pointer_action.move_to_location(end_x, end_y)
actions.w3c_actions.pointer_action.release()
actions.perform()

You can ask GPT to convert it to C# and see if it works

This is what I got with ChaptGPT below which does not work however I think what I originally sent you should work it just does not appear to. Hopefully there will be an example with a working solution prior to the touchaction being obsolete.

using OpenQA.Selenium;
using OpenQA.Selenium.Interactions;

// Assuming you have an initialized 'driver' object
IActionChains actions = new Actions(driver);

// Create a PointerInput for touch interactions
PointerInput pointerInput = new PointerInput(Interaction.PointerTouch, "touch");

// Set up the pointer action
var moveAction = pointerInput.CreatePointerMove(CoordinateOrigin.Viewport, start_x, start_y, TimeSpan.Zero);
var downAction = pointerInput.CreatePointerDown(PointerButton.Left);
var pauseAction = pointerInput.CreatePointerPause(TimeSpan.FromSeconds(2));
var moveEndAction = pointerInput.CreatePointerMove(CoordinateOrigin.Viewport, end_x, end_y);
var releaseAction = pointerInput.CreatePointerUp(PointerButton.Left);

// Add the actions to the sequence
actions.AddAction(moveAction);
actions.AddAction(downAction);
actions.AddAction(pauseAction);
actions.AddAction(moveEndAction);
actions.AddAction(releaseAction);

// Perform the actions
actions.Perform();

Dor-bl commented

I totally agree, but I didn't had the time to have an example ready, if in the end you are able to get it to work I would appreciate if you could send a PR with the example for our docs or even better a PR with the Unit test.

Dor-bl commented

@aaronportier, I created a short example(Not the cleanest one probably) of how to use the W3C actions :
https://gist.github.com/Dor-bl/1b7dde735f4d7f44e320298343dbda18
let me know if you still encounter issues.

From what I see CreatePointerMove which is inherited from the Selenium client, and can receive either an element or CoordinateOrigin as a starting point, seems like no way from their end to set an (X, Y) as you seek.
Maybe @titusfortner has a suggestion.

Would something like this work?

var tapPoint = new Point(${x}, ${y});
var move = touch.CreatePointerMove(CoordinateOrigin.Viewport, tapPoint.X, tapPoint.Y, TimeSpan.Zero);

@eglitise where is the touch coming from is that using TouchAction? If so we are trying to get rid of that as its not W3C compliant.

@eglitise where is the touch coming from is that using TouchAction? If so we are trying to get rid of that as its not W3C compliant.

This is where the touch is coming from:
var touch = new PointerInputDevice(PointerKind.Touch, "finger");

you can take a look at this PR for an example:
#721

Thanks guys for helping me out and showing me this. If I could can I ask one more question about this? In some tests I use touchAction.LongPress(349, 452).MoveTo(353, 750).Release().Perform(); This is mostly to refresh the page is this in a w3c example I could see?

It should be the same as a standard coordinate-based swipe, just split the LongPress into CreatePointerDown + CreatePause:

var initialMove = touch.CreatePointerMove(CoordinateOrigin.Viewport, point1.X, point1.Y, TimeSpan.Zero);
var actionPress = touch.CreatePointerDown(PointerButton.TouchContact);
var pause = touch.CreatePause(TimeSpan.FromMilliseconds(800));
var move = touch.CreatePointerMove(CoordinateOrigin.Viewport, point2.X, point2.Y, TimeSpan.Zero);
var actionRelease = touch.CreatePointerUp(PointerButton.TouchContact);

You can then change the pause length to whatever fits your usecase. Alternatively, you can try changing the TimeSpan.Zero in the second CreatePointerMove.

@aaronportier I'm closing this "issue".
For further questions please use https://discuss.appium.io/