Thursday, November 03, 2011

Android Application Testing Guide: Q&A

Q:  Hi Diego, I wanted to ask that can i write a monkey runner script which controls a web based apk ?
Eg; I install youtube.apk which is nothing but a browser with hardcoded youtube url.
Now my monkeyrunner script shall install this apk and then pass events such as a search string etc on this web based application.
All this i want to do and control externally through the monkey runner script. Is this possible? If yes, then could you please guide me by some pseudo code? 


Comment on Using Android monkeyrunner from Eclipse


Posted by latha



A:  This is an interesting question and a good monkeyrunner example, so here we go. monkeyrunner has the ability of installing APKs after obtaining the connection with the device. Then we start Youtube main activity, sleep for a bit to let things settle down.
Once we have the activity running is time to start our search. To do it, we touch the Search icon, enter the desired search string, 'android' in this particular case and the we touch the Search button again to actually start the action.
Following, is the script that translates our plan to monkeyrunner:


#! /usr/bin/env monkeyrunner

import sys
import os
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice

YOUTUBE = 'com.google.android.youtube-2.1.6.apk'
prog = os.path.basename(sys.argv[0])

def usage():
        print >>sys.stderr, "usage: %s" % prog
        sys.exit(1)

def main():
        if len(sys.argv) != 1:
                usage()

        print "waiting for connection..."
        device = MonkeyRunner.waitForConnection()

        print "installing youtube"
        device.installPackage(YOUTUBE)

        device.startActivity(component="com.google.android.youtube/.HomeActivity")
        MonkeyRunner.sleep(3)
        # search
        device.touch(450, 80, MonkeyDevice.DOWN_AND_UP)
        MonkeyRunner.sleep(5)
        device.type('android')
        # done
        device.touch(450, 740, MonkeyDevice.DOWN_AND_UP)



if __name__ == '__main__':
    main()


This script covers the case described in the question but it could be easily adapted for other cases and application.

I hope this is the answer you were looking for.

17 comments:

Mike said...

Hi Diego

when I try to use the full path name of my apk i.e
HOMERUN = 'C:/monkeyrunner/apk/com.com2us.HB3DLITE-19-1.4.1.apk'
device.installPackage(HOMERUN)

I get msg "error during Sync: Local path doesn't exist"

Any idea why this is happening?
Thanks

Diego Torres Milano said...

I don't know how the C:// is treated. Try to avoid it by moving the APK to the same directory where you are running the script.

Mike said...

...kind of confusing, cause when I do
device.installPackage('C:/monkeyrunner/apk/com.com2us.HB3DLITE-19-1.4.1.apk')
it works.

Also I'm trying to import my module into my monkeyrunner script. When I do: 'import myLib' it gives 'error: No module named myLib'
BTW I put the module myLib.py in the same directory as my script.

Diego Torres Milano said...

You should have no problems importing a module which is in the same directory of your script as the default path includes the current directory. To verify this pint sys.path and if necessary append '.' or the specific directory.

Android app development said...

This is one of the good post.I like your blog advantages. This is one of the useful post.
Android app developers

Android developer said...

Very advantageous cavalcade you access here.. Nicely presented admonition in this post, I accept to apprehend this affectionate of stuff. The amore of adequate is able and the abeyance is good. Acknowledgment for the post.

Android developers

mithun said...

Wonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep Blogging!



Android Application Development

Joseh672 said...

Hi Diego


is there a way to call an app then minimize it so it can run in the background.


Thanks,

Jose

Joseh672 said...

Hi

Diego

Is there a way to call an app
then minimize it so that it can run in the background


Thanks

Diego Torres Milano said...

@Joseh672,
What do you mean by minimize it ?
You can start another Activity (i.e. Home) and the previous Activity will be moved to the background.




Background activity: (an activity that is not visible to the user and has been paused) is no longer critical, so the system may safely kill its process to reclaim memory for other foreground or visible processes. If its process needs to be killed, when the user navigates back to the activity (making it visible on the screen again), its onCreate(Bundle) method will be called with the savedInstanceState it had previously supplied in onSaveInstanceState(Bundle) so that it can restart itself in the same state as the user last left it.

Joseh672 said...

Diego

how can I run monkeyrunner to open
application exp: File expert
then select the files

Thanks

Diego Torres Milano said...

@Joseh672,
What's an "application exp: File expert" ?

Joseh672 said...

I was using file expert as an example


Diego Torres Milano said...

@Joseh672,
Help me help you.

If you don't describe your problem with enough detail and giving examples is almost impossible to guess what you are trying to accomplish.

Joseh672 said...

I am trying to use monkeyrunner
to
1st step open file expert
2nd then click on select My Apps
3rd select All
4th click on Backup

any suggestions?

Thanks


m0llym0nster said...

@Mike
did you ever get monkeyrunner to install with the variable name?

device.installpackage(HOMERUN)
versus
device.installpackage('C:/path/to/app.apk') ?

That's the issue I am running into now, with your same error.

@Joseh672 you can try sikuli.org and using their sikuli project to simulate clicking on the mobile device.

Diego Torres Milano said...

@Molly Brinkley,
This is a Windows related problem. It works correctly on Linux and OS X.