Santosh Kumar
Santosh Kumar

Reputation: 81

Unable to perform simple click operation using appium code on Android app

I am new to Appium and I was trying to execute a simple program which performs a click operation. But the click operation is not happening. Here is the code:

package com.android.touchactionss;

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

import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.android.AndroidDriver;

public class Sample {
public static void main(String[] args) throws InterruptedException, MalformedURLException {
    DesiredCapabilities cap = new DesiredCapabilities();
    cap.setCapability("platformName", "Android");
    cap.setCapability("deviceName", "xiaomi-2014818-204648717d62");
    cap.setCapability("version", "5.1.1");
    cap.setCapability("appActivity", "com.mediamushroom.copymydata.app.EasyMigrateActivity");
    cap.setCapability("appPackage", "com.mediamushroom.copymydata");
    AndroidDriver<?> driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
    Thread.sleep(5000);
    try{
    System.out.println("STARTED");
    driver.findElementByAndroidUIAutomator(
            "new UiSelector().resourceId(\"com.mediamushroom.copymydata:id/NextButton\")");
    //driver.findElement(By.id("//*[@resource-id='com.mediamushroom.copymydata:id/NextButton']"));
    System.out.println("ENDED");
    }
    catch(Exception exception){
        exception.printStackTrace();
    }
    Thread.sleep(5000);
    driver.quit();
}
}

No exception is thrown but the click operation didn't happen. I tried with both driver.findElement(By.id("")) and driver.findElementByAndroidUIAutomator() method. But none of them worked. I have attached the object properties screen.

enter image description here

Upvotes: 2

Views: 1543

Answers (2)

Kovacic
Kovacic

Reputation: 1481

Hi here is example in next few lines, try to use some testing framework, Junit, TestNg, I've removed main, used TestNG with this example:

Start Appium server:

  1. Appium GUI (https://github.com/appium/appium-desktop/releases/tag/v1.6.2)
  2. Appium via console : appium --address 127.0.0.1 --port 4723

when Appium server is up-an-running, call this code:

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

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

public class TestAppium {

    AndroidDriver<MobileElement> driver;

    @BeforeTest
    public void setup() {
        DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability("platformName", "Android");
        cap.setCapability("deviceName", "emulator-5554");  //used emulator, but should be set devices guid  in Your case "xiaomi-2014818-204648717d62"
        cap.setCapability("version", "5.1.1");
        cap.setCapability("appActivity", "com com.mediamushroom.copymydata.app.EasyMigrateActivity");
        cap.setCapability("appPackage", "com.mediamushroom.copymydata");

        try {
            driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void testAppiumSimulator() {
        MobileElement element = driver.findElement(By.id("NextButton"));
        element.click();

        // do some Assertion

        Assert.assertTrue(//some condition//);

    }


    @AfterTest
    public void tearDown() {
        driver.quit();
    }
}

And this is an simple Appium test...

Hope this helps,

Upvotes: 0

Bill Hileman
Bill Hileman

Reputation: 2838

First, add the following import:

import io.appium.java_client.android.AndroidElement;

Next change your code:

AndroidDriver<?> driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), cap);

to:

AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap);

You might need to change the URL to 0.0.0.0 but it depends on what your Appium Server settings are. They may be correct they way it is now.

Lastly, you need to use the following method to click the element:

driver.findElement(By.id("com.mediamushroom.copymydata:id/NextButton")).click();

Upvotes: 1

Related Questions