English 中文(简体)
Is "Monkey Runner" useful for testers to prepare test cases directly in Android applications?
原标题:

I have gone through the "Monkey Runner" for the following link http://developer.android.com/guide/topics/testing/testing_android.html It has so much Java code. I am not able to under stand the code to create test cases. Is it only for developers, or the testers to test the application thoroughly. Is there any other pattern for creating test cases through code? Can any one suggest me about the same.

Thank you.

问题回答

Have a look at my MonkeyRunner code. Should be easier then Java. Change path to save file, and replace phone number. I had only 1 problem. Cant Hangup a call.

#! /usr/bin/env monkeyrunner
       
    Created on Apr 1, 2011

    @author: sj
       

    import sys

    # import the MonkeyRunners modules used by this program
    from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage







    def browse(d):
        d.broadcastIntent("http://www.google.com/", "ACTION_MAIN")
        #d.startActivity(component="com.android.browser/.BrowserActivity")

    def debug(device):
        print" package:%s" % device.getProperty( am.current.package )
        print" action:%s" % device.getProperty( am.current.action )
        print" comp.class:%s" % device.getProperty( am.current.comp.class )
        print" comp.package:%s" % device.getProperty( am.current.comp.package )
        print device.getProperty( display.width ), device.getProperty( display.height )

    def screenshot(d):
        MonkeyRunner.sleep(1.0)
        result = d.takeSnapshot()
        MonkeyRunner.sleep(1.0)
        result.writeToFile( /yourPath/device.png ,  png )  

    def call(d):

        d.startActivity(component="com.android.contacts/.TwelveKeyDialer")
        print "Start Activity"

        MonkeyRunner.sleep(1.0)

        d.type("+XXXXXXXX")

        # Call number.
        print "Call"
        d.touch(190, 800,  DOWN_AND_UP );
        # not working device.press( KEYCODE_CALL ,  DOWN_AND_UP )

        print "Wait 10 sec"

        MonkeyRunner.sleep(10.0)

        # HangUp Call
        #device.press( KEYCODE_ENDCALL ,  DOWN_AND_UP )

        print "Hang Up"
        #x1 = 215
        #x2 = 230
        #y = 700
        #start = (x1,y)
        #end = (x2, y)
        #steps = 2
        #pause = 0.2
        #device.drag(start, end, pause, steps)

        d.startActivity(component="com.android.phone/.InCallScreen")
        #device.touch(230, 700, "DOWN");
        MonkeyRunner.sleep(1.0)
        #device.touch(230, 700, "UP");

        d.touch(230, 700,  DOWN_AND_UP );
        #device.touch(270, 650,  DOWN_AND_UP );


    def main():
        print "Start"

        # Connect to the current device returning the MonkeyDevice object
        device = MonkeyRunner.waitForConnection()

        #MonkeyRunner.alert("Starting Activity", "monkeyrunner", "OK")

        if not device:
            print "Couldn t get connection"
            sys.exit()

        print "Found device"

        #call(device)
        browse(device)
        debug(device)
        screenshot(device)


        device.press( KEYCODE_ENDCALL ,  DOWN_AND_UP )

        MonkeyRunner.sleep(10.0)




    if __name__ ==  __main__ :
        main()

I have learned monkeyrunner via this small guide. http://antoine-merle.com/introduction-to-the-monkey-runner-tool-2/

You do not have to use java, but to start low on python. For the ide you can use pycharm, which will give you a better start when creating classes in python.

As of the code which @Boris_Ivanov showed, this is a good start, but I would delete the "MonkeyImage" - as you are not using it also pushing the test cases into different files would add to speed if required to use.

One thing to talk about:

    Connect to the current device returning the MonkeyDevice object
    device = MonkeyRunner.waitForConnection()
    #MonkeyRunner.alert("Starting Activity", "monkeyrunner", "OK")

I am using something like this and it does work all the way:

    device = MonkeyRunner.waitForConnection(60)
    if not device:
        raise Exception( Can not connect to device )

Best luck.





相关问题
Selenium not working with Firefox 3.x on linux

I am using selenium-server , selenium rc for UI testing in my application . My dev box is Windows with FireFox 3.5 and every thing is running fine and cool. But when i try to run selenium tests on my ...

Best browser for testing under Safari Mobile on Linux?

I have an iPhone web app I m producing on a Linux machine. What s the best browser I can use to most closely mimic the feature-limited version of Safari present on the iPhone? (It s a "slimmed down" ...

Code Coverage Tools & Visual Studio 2008 Pro

Just wondering what people are using for code coverage tools when using MS Visual Studio 2008 Pro. We are using the built-in MS test project and unit testing tool (the one that come pre-installed ...

Is there any error checking web app cralwers out there?

Wondering if there was some sort of crawler we could use to test and re-test everything when changes are made to the web app so we know some new change didn t error out any existing pages. Or maybe a ...

热门标签