How to Fake battery settings/status during a Flutter Integration Test? - android

The (Android) app we're working on is depended on it's plugged-in (AC) status.
Because we want to run the app on a real device connected to our buildserver we need a way to fake this behaviour in order to fully test the app without actually removing the device from power.
Now I've learned about adb shell dumpsys battery ac 0|1 which allowed me to set it.
But I was unable to get this working during the test itself with:
Process.run('adb', [
'shell',
'dumpsys',
'battery',
'set',
'ac',
'1',
]);
This results in the following error:
══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞═════════════════
The following ProcessException was thrown running a test:
Permission denied
Command: adb shell dumpsys battery set ac 1
I also tried it with sudo, but that resulted in the same.

Related

How to run "adb push" command before launching an activity from Android Studio

I have some test data for my program that i want to push to the device before a test activity is run or debug. On the command line this is:
adb -s RF8M40T69JX push --sync -z any test_data /storage/emulated/0/Download/test_data
I know i can run an external tool shell script with the "Before launch" configuration option. But this does not give me any way to know on which device Studio want to start it. I don't want to waste the time and dispatch to every possible device and every Edit-Compile-Run cycle.
I checked that the device signature is unfortunately not passed as environment variable to external tools as it should be. Is there any other way? Can i do this with some Gradle magic?

Use "UiAutomator dump" command while Appium is running

I need to run Automated tests via Appium, while there is another application which is trying to take UiDump and calculate some statistics. However, once Appium session starts, the application encounters error, since it can no longer capture the UiDump. On executing command "uiautomator dump", following error is encountered
137|j7maxlte:/proc/24005 $ uiautomator dump
sh: resetreason: can't execute: Permission denied
Killed
I understand this is a known behavior, but is there any way or work around to execute command : "uiautomator dump" while Appium session is active?

See app background processes in Android Studio

Maybe I'm just missing it but is there a way to view WHAT is running in the background on-device in android studio?
I'm getting a battery usage alert on my phone (galaxy s8 - OS v8.0), indicating my app is doing something in the background and I'd like to see what it is.
Thank you
Yea, via the terminal function. This is one of options at the bottom of the Android studio.
You need to run ADB to connect to the device. Once connected you can use Unix command lines to see the processes running.
From google dev : https://developer.android.com/studio/command-line/adb
Also, the command I use to connect an emulator to run unix commands is:
adb -s <DEVICE> shell
Normally the ADB is stored :
USER\AppData\Local\Android\Sdk\platform-tools
and then once connected you can use the following to see the running processes :
https://www.howtogeek.com/107217/how-to-manage-processes-from-the-linux-terminal-10-commands-you-need-to-know/
Hope this helps.

Starting android emulator and running tests

I'm writing a script that performs a series of operations on an Android emulator. I'd like to start the emulator, and run of suite of tests. So far I have:
(emulator-ARM #emuName) & adb wait-for-device; adb shell am instrument -w com.name.name.android.test/android.test.InstrumentationTestRunner
But All this does is kicks off the emulator, and returns an error that reads:
android.util.AndroidException: Can't connect to activity manager; is the system running?
It seems to me that wait-for-device is simply waiting for a device to be connected, not for a device to be ready, which is what I think I need
Anyone else had this problem, or has a suggestion?
According to the documentation Android Debug Bridge / Commands / Scripting / wait-for-device, it is exactly as you said (It seems to me that wait-for-device is simply waiting for a device to be connected, not for a device to be ready...). Unfortunately, there is no command mentioned which does what you need.

How to send key events to a headless emulator in an instrumentation test?

We are currently working on an instrumentation test suite which runs on our build server, but while the tests pass on a dev machine using a normal Android emulator, the builds fail on the build server since there we only run a headless emulator with the -no-window flag.
The failure occurs when trying to invoke the InstrumentationTestCase.sendKeys() method to programmatically open the options menu. The error is:
Permission denied: injecting key event from pid 646 uid 10026 to window Window{43d55100 paused=false} owned by uid 1000
We then found out that there's a INJECT_EVENTS permission, but setting it in the manifest had no effect. In fact in the log we saw this output:
Not granting permission android.permission.INJECT_EVENTS to package com.qype.radar (protectionLevel=2 flags=0x6644)
Does that mean this permission is useless?
We also tried to let the instrumentation test app and the app under test share the same Linux user ID using android:sharedUserId and run in the same process (android:process -- we weren't sure if that was already the case), but still no luck.
Does this mean it's currently impossible to run instrumentations which contain key events on a headless emulator, or are we missing something?
I run the emulator without -no-window on headless machines by first running an Xvnc instance (i.e. fake X server) then starting the emulator in that DISPLAY.
More accurately, I get the Xvnc and Android Emulator Jenkins plugins to do this for me.
Unfortunately, unlocking the screen is still a concern before injecting UI events, but this is (hackily) resolved by automatically running a command like this (similar to this other answer you've seen):
echo "event send EV_KEY:KEY_MENU:1 EV_KEY:KEY_MENU:0" | nc -q1 localhost 5554
Edit:
I discovered that this method is far more reliable:
adb shell input keyevent 82
Some info about keycode 82.
I had similar problem with my test on the Hudson server. In my case the problem I solved by suggestion from Android SDK:
http://developer.android.com/guide/topics/testing/testing_android.html#UITestTroubleshooting
Important was that I had to enable permissions for main application too.

Categories

Resources