driver.startActivity fails to wait for app to start up, leads to test execution hanging

Hi!

I’m trying to build an Appium test suite for an Android application but I ran into a problem with the Android M’s permission model, specifically since the app asks for a permission straight at launch before the application itself starts up.

Now with the help of willosser’s reply in here, I am able to install the application so that it has all the permissions it needs, but for some reason after calling driver.startActivity() Appium will fail somewhat silently if I try to search for any elements and the Appium server log will print a following error message:

info: [debug] executing cmd: C:\Users\Tomi\Android_SDK\platform-tools\adb.exe shell “am start -S -n com.amaze.filemanager/.activities.MainActivity”
info: [debug] Responding to client with success: {“status”:0,“value”:“Successfully launched the app.”,“sessionId”:“8cdbf4b7-2ebc-4008-936e-dd8ba5acd883”}
info: ← POST /wd/hub/session/8cdbf4b7-2ebc-4008-936e-dd8ba5acd883/appium/device/start_activity 200 2615.435 ms - 104 {“status”:0,“value”:“Successfully launched the app.”,“sessionId”:“8cdbf4b7-2ebc-4008-936e-dd8ba5acd883”}
info: → POST /wd/hub/session/8cdbf4b7-2ebc-4008-936e-dd8ba5acd883/element {“using”:“xpath”,“value”:“//[@text=‘Alarms’]“}
info: [debug] Waiting up to 0ms for condition
info: [debug] Pushing command to appium work queue: [“find”,{“strategy”:“xpath”,“selector”:”//
[@text=‘Alarms’]”,“context”:“”,“multiple”:false}]
error: Unhandled error: TypeError: Cannot read property ‘sendAction’ of null
at [object Object]. (C:\Program Files (x86)\Appium\node_modules\appium\lib\devices\android\android.js:504:23)
at Immediate.q.process [as _onImmediate] (C:\Program Files (x86)\Appium\node_modules\appium\node_modules\async\lib\async.js:806:21)
at processImmediate [as _immediateCallback] (timers.js:367:17) context: [POST /wd/hub/session/8cdbf4b7-2ebc-4008-936e-dd8ba5acd883/element {“using”:“xpath”,“value”:“//*[@text=‘Alarms’]”}]

It seems that the client is not sent an error message that the element could not be located, and overall seems the element is not waited for at all.

I tried to start the activity by just running driver.startActivity(package-name, activity-name) and by driver.startActivity(package-name, activity-name, package-name, activity-name) so Appium would wait for the activity to be actually started before starting the test set, but this seemed to not change anything.

My setUp method is following:

DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(MobileCapabilityType.PLATFORM_NAME,"Android");
cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Nexus 5x 1");
cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "4000");
cap.setCapability(MobileCapabilityType.PLATFORM_VERSION, "6.0.1");
cap.setCapability("appPackage", "com.amaze.filemanager");
cap.setCapability("noReset", false);
cap.setCapability(MobileCapabilityType.FULL_RESET, false);
cap.setCapability("autoLaunch", false);
Runtime.getRuntime()
            .exec("adb install -g C:\\Users\\Tomi\\Projects\\amazeFileManager\\AmazeFileManager\\build\\outputs\\apk\\AmazeFileManager-play-debug.apk");
driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
driver.startActivity("com.amaze.filemanager", "activities.MainActivity",
            "com.amaze.filemanager", "activities.MainActivity");

and after that I attempted to put up a following wait:

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(
            By.xpath("//*[@text='Alarms']"))
    );

But that does not seem to do anything but get the error log on the server, after which everything stops. I am stumped on what to do.

Just to cover the basic questions, can you please confirm or answer the following:

Before Android M, this call worked, right?

visibilityOfElementLocated(By.xpath(“//*[@text=‘Alarms’]”)

Is it still working?

What version of Appium are you using?

Can you dump the page source before trying to locate the element? I have some problems with Appium not being able to read the current page on M, and more on N. I don’t think this is the problem given the error, but want to explore that angle

I don’t know if or how PLATFORM_VERSION is used in Appium. I looked and noticed we are still using 4.4.

I’m not certain if the call worked before M - I only recently started building the test suite and so far the only version on which I’ve worked with is M.

The command

visibilityOfElementLocated(By.xpath("//*[@text='Alarms']")

seems to be working fine when used in different places. I am running version 1.4.16.1 of Appium.

Here is xml dump of the page I get when inspecting the startup page with uiautomatorviewer from Android platform tools.

I’m not sure if this problem is related to searching the element, but rather that Appium for some reason does not wait after startActivity is called. This seems to be a bit weird since I put the wait in question there just so that Appium would actually wait for a moment for the activity to properly start up before it starts to do its’ thing.

Also, this problem of mine only shows up when I set autoLaunch = false, run the adb command to install the application with the -g flag and manually call startActivity(), if I leave autoLaunch on everything works fine.

And yeah, seems that the PLATFORM_VERSION does not do very much, if left out of the capabilities it does not seem to change anything that I can see.