Tapping on the screen by using coordinates


#1

I was reading lots of articals on appium and some sayed we can tell appium server to tap on a specific location by giving the coordinates.(X and Y).
But i tried with so many ways but it didnt works for me.
Im using appium with c#.
Is there any one who has done the same thing.?
If so please share ur code with the community.:slight_smile:


#2

Something similar to

TouchAction a2 = new TouchAction();
a2.Tap (100, 100).Perform();

#3

See this is my output when i try that code.

:frowning: :frowning:
:frowning: :frowning:

Test Name: touchcordinates
Test FullName: AppiumDTZ.Test_ServiceRequest.touchcordinates
Test Source: d:\QA automation\appium\AppiumTFS\DTZAppium\AppiumDTZ\AppiumDTZ\Test_ServiceRequest.cs : line 315
Test Outcome: Failed
Test Duration: 0:00:18.4543878

Result Message:
Test method AppiumDTZ.Test_ServiceRequest.touchcordinates threw exception:
System.NullReferenceException: Object reference not set to an instance of an object.
Result StackTrace:
at OpenQA.Selenium.Appium.MultiTouch.TouchAction.Perform()
at AppiumDTZ.ElementsServiceRequest.touchcordinates() in d:\QA automation\appium\AppiumTFS\DTZAppium\AppiumDTZ\AppiumDTZ\ElementsServiceRequest.cs:line 532
at AppiumDTZ.Test_ServiceRequest.touchcordinates() in d:\QA automation\appium\AppiumTFS\DTZAppium\AppiumDTZ\AppiumDTZ\Test_ServiceRequest.cs:line 317


#4

It’s work for my after initialization with driver:

ITouchAction swipe = new TouchAction(driver);

where driver is instance of

AndroidDriver<AppiumWebElement>

#5

Yes, off course :slight_smile: I wrote my code from memory, sorry. The code wont even compile unless you provide the driver!


#6

In the latest Appium (1.4.13) code compiles without any error.


#7

I’m using Java. And when I replied 3 days ago, I didn’t check the original post about using C# :relaxed:


#8

I am getting below error on using tap(x,y):
Command returned error:java.lang.SecurityException: Injecting to another application requires INJECT_EVENTS permission

info: [debug] [BOOTSTRAP] [debug] Returning result: {“status”:13,“value”:“Injecting to another application requires INJECT_EVENTS permission”}
info: [debug] Responding to client with error: {“status”:13,“value”:{“message”:“An unknown server-side error occurred while processing the command.”,“origValue”:“Injecting to another application requires INJECT_EVENTS permission”


#9

Can you tell how to do it in Java?
With full example of code


#10
TouchAction touchAction=new TouchAction(driver);
touchAction.tap(xPoint, yPoint).perform();

#11

Telmo, thx for fast answer. But I still can’t understand how to put it in my code.

My code:

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidKeyCode;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.MalformedURLException;
import java.net.URL;

public class GameTest {
public static void main(String[] args) throws MalformedURLException, InterruptedException {

    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("deviceName","G9AXGY0336745E");
    caps.setCapability("platformName","Android");
    caps.setCapability("app","C:\\Appium\\node_modules\\appium\\build\\unlock_apk\\unlock_apk-debug.apk");

    AndroidDriver driver = new AndroidDriver(new URL("http://0.0.0.0:4723/wd/hub"), caps);

    driver.pressKeyCode(AndroidKeyCode.HOME);
    Thread.sleep(2000);

    driver.findElementByAndroidUIAutomator("new UiSelector().description(\"Apps\")").click();
    driver.findElementByAndroidUIAutomator("new UiSelector().text(\"FunnyFarm\")").click();
    Thread.sleep(5000);

So, after I opened this app (its a game). I need to tap “start” button.
I just need to add
TouchAction touchAction=new TouchAction(driver);
touchAction.tap(xPoint, yPoint).perform();
and instead xPoint and yPoint trying to guess what are coordinates of this button? Or how it should works


#12

Well… first you do not need to open the app “manually” by touching on its icon. You can use capabilities appActivity and appPackage to indicate witch is the package/activity you want to launch.

Now, about the “start” button. Have you opened uiautomatorviewer to see if you can tap on the id, text, content description, etc.? If so, then you can use your findElementByAndroidUIAutomator or findElementByID or any other strategy to tap on it.


#13

Hi, the search key on the android key is not clickable, the enter key of the app was working fine few days ago bt it has stopped working now after some changes made by the developer in the app…I am only left with option of clicking on the coordinates of search(magnifier) on keyboard.Can you tell me how can i get the coordinates of that…Thanks