Difference between click() and tap()

If I want to tap on a certain iOS button, should I get a WebElement object from its xpath and invoke the click() method on it, or should I use driver.tap() method to tap a specific x/y coordinate?

i.e. Should I do this:

WebElement button = driver.findElement(By.xpath("//UIAApplication[1]/UIAWindow[1]/UIATableView[1]/UIATableCell[1]"));
button.click();

or this:

driver.tap(1,134,526,1);
5 Likes

According to my understanding, it is better to use driver.tap() method as it is more closely binded to the mobile scenario. tap() method belongs to AppiumDriver class while the click() method belongs to the WebDriver class.

It’s better to go for tap() method as they have extracted all mobile native gestures and pushed them to java_client library for better implementation.

By the way, you can also use driver.tap(int x, WebElement we, int y) if you don’t want to go by specific x/y coordinates.

8 Likes

How we can pass x/y coordinates of an element? How we can get that?
Currently, I’m using click() function in my tests which needs only element to click with no coordinates.
I’m new to appium and don’t know how to pass coordinates in tap() function. Please explain me with example.

Thanks.

You can get the x/y coordinates of the element through the inspector provided by appium UI tool.
For general, the tap function is tap(int x, WebElement we, int y). Here, you can pass 1 as x and y and the WebElement object. This would perform a tap on the webElement you are trying to click.

@osamaa, using ruby client, you can get the locations by Element.location_rel.x and Element.location_rel.y

Java and other clients should have something similar.

Does TAP method will work when testing app using emulator? OR it support only real device?

I feel Tap and Click work the same way. It works just like click in emulator.

But prefer using Click as it doesnt require any x,y coordinates. The reason being, if the object location changes, maintenance costs go up.

If you use click method, as it associates to webelement, even the element is located in different locations doesnt have any problem.

Yes I agree with you. But if we do not have any way to identify element and we have only coordinates then we have to use TAP? Because I think wan no specify coordinate with Click () method.

tap and click sometimes suffer from the elements not being valid anymore and returning “not able to tap element”.

With this being said, I normally use tab on buttons that I’m sure are there. Whenever I get any problem with tapping on visible elements (but appium states they aren’t visible, etc.) I user TouchAction.new.press on Element. So no maintenance costs and it always presses on the spot even when tap/click does not work

1 Like

How to Use Tab On Button.
As I was trying the same but it was not possible to me…as I used Keys.TAB and “/t” in Send Keys.

Please let me know how to tab on iOS Element in Appium.
Thanks,
Ajit Jadhav.

Usual click doesn’t work?

yah…its not…if Element is \Hidden and Need to Scroll…
even its avilable in DOM.

@Ravi15, why don’t you start a new topic as this question doesn’t have much to do with this particular topic.

2 Likes

To find the element X and Y, you can do the following:

WebElement el = driver.findElement(“Your element”);
Point p = ((Locatable) el).getCoordinates().onPage();
driver.tap(1,p.getX(),p.getY(),1);

1 Like

Myself, I use a method I’ve wrote called tapOn :

 public static void tapOn(WebElement element){
        new TouchAction((MobileDriver) getDriver()).tap(element).perform();
    }

I just send him a WebElement. To get it, I use this :

getDriver().findElement(By.xpath(elementSelector))

where the elementSelector is the xPath, found with Appium finder for example.

It makes me able to first get the WebElement in the view, and then create the methods to tap on it.

I also experience a element cannot be tapped situation and using press is at least a step forward. Anyone knows if there’s an upcoming fix?

Hi @Julien_Blanc

What is getDriver() Method here.

I am using like this new TouchAction(((MobileDriver)driver).tap(arg0, arg1, arg2)), and here I am not getting tap() method which takes only one arguement, also perform() method is not coming.

Could you please explain your method in more detail.

Thanks

Yes for sure.

First I’ve the method getDriver :

public static WebDriver getDriver(){
        return AppiumTest.driver;
    }

It returns the driver, to make actions afterward.

Then each screen of the app is split in two sides : first I get the elements, then I create the methods (actions).

To get the elements, I use the method findElement i’ve wrote above.
And after, to tap on an element, I’ve created the method tapOn wrote above too. But getDriver() and tapOn() is wrote in another class accessible for each test.

You want an exemple ?

Hi @Julien_Blanc

That would be a great help, if you provide.

Appreciate your help.

Hello,
I have a query regarding tap function. I wanted to test a touch action on the object that we have created on the canvas . I tried using tap and click but it didn’t work me. And I am not able to see the object that I wanted to touch using ui automator. The object was a rectangle created by the 4 lines . Can someone suggest me how to perform touch on this object .

Thanks