2

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

  • Versions used: appium software version 1.6.2
  • appium java_client version 6.1.0
  • selenium 3.13
4
  • I am unfamiliar with Appium, but I would say that your problem lies within your main method. In Android, activity classes generally have an onCreate(Bundle SavedInstanceState) method that is called when the activity is launched. Inside this method you must also set the view. Your class should extend AppCompatActivity as well. Perhaps you would like to try out Android Studio, which is made by Google specifically for Android app developers. Commented Jul 16, 2018 at 17:24
  • I will intall Android Studio, try the above and let you know if it working or not. Thanks anyways :) Commented Jul 16, 2018 at 17:43
  • Android Studio is user-friendly and offers easy-to-use templates for users. I hope that you find everything well. If you have issues, feel free to contact me. Commented Jul 16, 2018 at 17:59
  • Santosh, it seems you haven't used click() after finding the next button or just use driver.findElement(By.id("com.mediamushroom.copymydata:id/NextButton")).click(); Commented Jul 17, 2018 at 7:28

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

Comments

0

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,

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.