Skip to main content

Unlocking Your Android Test Devices' Potential. Literally.

"Education is the key to unlock the golden door of freedom."
~George Washington Carver
 The successful creation and management of a device lab at my company has required many hours of work from an autodidactic polymath. Unfortunately that responsibility fell to rest on me instead (with an audible *THUMP*). This is another piece of the narrative of my education.

Waking and Unlocking Jelly Bean Devices Automatically

One of the common traits to all Android devices we use in house is that they're all provisioned with a Google Apps account for remote management purposes. This includes "personal" devices (BYOD is alive and well) and lab devices. Under the auspices of Google's MDM scheme, there is no concept of segregating collections of devices and applying rules regarding PIN strength, screen timeouts, etc (at least according the the domain admin whose words I have no need to distrust). This is frustrating to me since my first experience with MDM was back in 2007 with Microsoft's first attempts at it in System Center Configuration Manager and Windows Mobile 6.5 and 7. At least with SCCM, we could create collections and manage them with unique and overlapping rulesets. Come on, Google. You're getting beaten by SCCM 2007. Okay, I digress.

There is a lot of room for investigation here but suffice it to say newer devices are not able to use the old "keep screen awake while charging" feature on devices in my lab. I am not the Google Apps domain admin and do not have access to the admin console to investigate these settings or work arounds. At least in the case of Jelly Bean, there is a fairly handy work-around based on UIAutomator's access to the lock screen.

Broken down it looks like this:

Problem - Unblock UI automation at the lock screen automatically.
Solution - Use UIAutomator on JellyBean devices to wake the screen, check for lock, enter the pin, and unlock the device.
Bonus - Leave this in place to be repeatable by any arbitrary test on the CI server.

Pseudo code:

FakeTestCase(){
    if(device screen isn't on){
        getUiDevice().wakeUp()
    } catch (exception e) {
        handle exception()
    }
   
    UiWatcher LockScreenPinHandler = new UiWatcher() {
    boolean checkForCondition(){
        UiObject SomethingUnique = get something unique about the lock screen
        if(SomethingUnique.exists()){
            get PinEntryField
            set Pin in PinEntryField
            hit Enter
        }
    
    }

    RegisterWatcher
    RunWatcher

    UiObject HomeScreenAppsButton = get home screen apps button;
    assert(HomeScreenAppsButton.isShown/Visible/Clickable/Whatever())

}

In normal usage:

By creating a java project and building a JAR that contains the UIAutomator test logic above, I can simply upload that jar to /data/local/tmp on the affected devices and run the "test" prior to running any other UI automation, be it Cucumber, JUnit, Monkey Runner, or even UIAutomator-based. In Jenkins it would look like the following (where $DEVICE_SERIAL is an environment variable corresponding to your Android device):

#run DeviceUnlocker.jar in order to wake/unlock device
adb -s $DEVICE_SERIAL shell uiautomator runtest DeviceUnlocker.jar -c com.dummmycompany.DeviceUnlocker.DeviceUnlockingPseudoTest

SAMPLE CODE: 


Comments

  1. Can u please check the option "stay awake" from USB debugging menu list .. so when u r running your automated test device will be always ON .. BTW your solution is nice .. thanks for your post ..

    ReplyDelete
  2. HI Sandip,
    I've tried that but because my devices are under Google Apps management, the domain admin has dictated that they all have a screen timeout and that policy is enforced even on devices in my lab. That's right. This whole thing only exists because of a stupid scenario most people should be able to completely avoid.

    ReplyDelete

Post a Comment

Popular posts from this blog

UiAutomator and Watchers: Adding Async Robustness to UI Automation

"I'm looking over your shoulder... only because I've got your back." ~ Stephen Colbert After my recent UiAutomator review a user brought up an important question about the use of UiWatcher. The watchers serve as async guardians of the test flow, making sure the odd dialog window doesn't completely frustrate your tests. Having a tool that automatically watches your back when you're focused on the functional flow of your tests is awesome. 100% pure awesomesauce. Since the API documentation on watchers is scant and the UI Testing tutorial on the Android dev guide doesn't cover their use in depth, I figured I should add a post here that goes over a simple scenario demonstrating how to use this fundamentally important UI automation tool. In my example code below, I'm using uiautomator to launch the API Demo app (meaning run this against an Emulator built in API level 17 - I used the Galaxy Nexus image included in the latest ADT and platform tools). ...

UiAutomator.jar: What happened when Android's JUnit and MonkeyRunner got drunk and hooked up

"Drunkenness does not create vice; it merely brings it into view" ~Seneca So Jelly Bean 4.2 landed with much fanfare and tucked in amongst the neat new OS and SDK features (hello, multi-user tablets!) was this little gem for testers: UiAutomator.jar. I have it on good authority that it snuck in amongst the updates in the preview tools and OS updates sometime around 4.1 with r3 of the platform. As a code-monkey of a tester, I was intrigued. One of the best ways Google can support developers struggling with platform fragmentation is to make their OS more testable so I hold high hopes with every release to see effort spent in that area. I have spent a couple days testing out the new UiAutomator API  and the best way I can think of describing it is that Android's JUnit and MonkeyRunner got drunk and had a code baby. Let me explain what I mean before that phrase sinks down into "mental image" territory. JUnit, for all its power and access to every interface, e...

Why Developers Shouldn't Perform Software Testing - A Rebuttal

Take a minute and read the following 2-pager entitled " Guest View: Why developers shouldn’t perform software testing ". Man, I tried to like this article because the author, Martin Mudge, clearly knows that businesses who undervalue quality by trying to eliminate testing through simply shuffling the traditional testing task to developers are making a huge mistake. Unfortunately he begins by making an assertion that testing overburdens a developer which at face value is complete nonsense. If you feel overburdened, it is a timeline issue and that is true no matter WHAT the nature of the tasking is.  So his assertion of “one the most important” contributing factors being overburdening the developers is massively flawed. It gets immediately worse from there because his second point is about time constraints.  Mr Mudge just gets so much wrong. What he really should be shooting down is the idea that testing, as a cost-center not as a task, can be eliminated by having your p...