7

I'm using the Android monkey test to test my android apps, it's works for my app, and is very cool. but I'd like to test an application activity in specific, how could i do that?

today I'm testing all app with:

$ adb shell monkey -p my.package -c android.intent.category.HOME -c android.intent.category.DEFAULT -v 500 -s "a random number"
ademar111190
  • 14,215
  • 14
  • 85
  • 114
  • http://stackoverflow.com/questions/4659036/using-monkey-in-a-subactivity-only-android-debugging – stinepike Apr 15 '13 at 16:01
  • @StinePike that solutions need "Put your subactivities under one subpackage" but i don't want do it because because it would be too much work organizing the code to test each of my activities. I'm looking for something simple, like monkeytest is simple. :D – ademar111190 Apr 15 '13 at 16:16
  • then I think it is better to write your own activity test cases – stinepike Apr 15 '13 at 16:21
  • @StinePike I found a better way "i think be better", http://developer.android.com/tools/help/monkeyrunner_concepts.html, with this I can do python scripts to simulate events, so i can open a specific activity and with help of random number simulate monkey tests... i go try. – ademar111190 Apr 15 '13 at 18:44
  • @StinePike works with monkey runner :), i answer my question with the script i using – ademar111190 Apr 15 '13 at 19:37

4 Answers4

8

With Android monkey test i cannot test a specific activity, but with Android monkey runner i can do python scripts to simulate a monkey test, so i did a python script open the my activity and init the monkey test :)

#! /usr/bin/env monkeyrunner

from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
from random import randint

print "get device"
device = MonkeyRunner.waitForConnection()
package = 'my.packaget'
activity = 'my.package.activity'
runComponent = package + '/' + activity
device.startActivity(component=runComponent)

#use commands like device.touch and device.drag to simulate a navigation and open my activity

#with your activity opened start your monkey test
print "start monkey test"
for i in range(1, 1000):
    #here i go emulate only simple touchs, but i can emulate swiper keyevents and more... :D
    device.touch(randint(0, 1000), randint(0, 800), 'DOWN_AND_UP')

print "end monkey test"

save teste.py and run

$ monkeyrunner teste.py
ademar111190
  • 14,215
  • 14
  • 85
  • 114
7

This worked for me. Add category in manifest:

<activity android:name="MonkeyActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.MONKEY" />
    </intent-filter>
</activity>

Where MonkeyActivity will perform an initialization setup for testing and from shell:

adb shell monkey -p my.package -c android.intent.category.MONKEY -v 500
einverne
  • 6,454
  • 6
  • 45
  • 91
Telmo Pimentel Mota
  • 4,033
  • 16
  • 22
2

From the docs:

-c If you specify one or more categories this way, the Monkey will only allow the system to visit activities that are listed with one of the specified categories. If you don't specify any categories, the Monkey will select activities listed with the category Intent.CATEGORY_LAUNCHER or Intent.CATEGORY_MONKEY. To specify multiple categories, use the -c option multiple times — one -c option per category.

so you remove the DEFAULT and LAUNCHER category from your command, add the MONKEY one to the activity you want to test in your manifest and the command is now simply:

$ adb shell monkey -p my.package -c -v 500 -s "a random number"
gatnowurry
  • 344
  • 1
  • 7
  • if i no send the -c ... i get the follow error: "** No activities found to run, monkey aborted.". – ademar111190 Apr 15 '13 at 16:05
  • did you add the MONKEY category to the activity in the androidmanifest.xml? You need something like in your activity definition – gatnowurry Apr 15 '13 at 16:15
  • I try it, i did a activity with "android.intent.action.CATEGORY_MONKEY" and "android.intent.action.MONKEY" and run "$ adb shell monkey -p my.package -v 500 -s 4" and complet outpout is: ":Monkey: seed=0 count=500 :AllowPackage: my.package :IncludeCategory: android.intent.category.LAUNCHER :IncludeCategory: android.intent.category.MONKEY ** No activities found to run, monkey aborted." – ademar111190 Apr 15 '13 at 16:19
  • You're nearly there, you just need to set a category as monkey instead of an action – gatnowurry Apr 15 '13 at 16:20
  • no :(, i set " " and try "$ adb shell monkey -p my.package -v 500 -s 4" and returns ":Monkey: seed=0 count=500 :AllowPackage: my.package :IncludeCategory: android.intent.category.LAUNCHER :IncludeCategory: android.intent.category.MONKEY ** No activities found to run, monkey aborted." – ademar111190 Apr 15 '13 at 16:29
  • and try run with "$ adb shell monkey -p br.com.zazcar.tablet -c android.intent.category.MONKEY -v 500 -s 4" and returns ":Monkey: seed=0 count=500 :AllowPackage: my.package :IncludeCategory: android.intent.category.MONKEY ** No activities found to run, monkey aborted." – ademar111190 Apr 15 '13 at 16:29
  • How did you end up doing it ademar111190 ? Thanks! – MaTT Nov 22 '13 at 05:14
1

For me just worked with:

-c android.intent.category.LAUNCHER

according to my manifest standard category entry:

<activity
                android:name="pl.com.infinitysoftware.carassistant.app.CarAssistantMainActivity"
                android:label="@string/app_name"
                android:launchMode="singleTask"
                android:configChanges="orientation|screenSize">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
</activity>
Aleksander Lech
  • 1,105
  • 12
  • 16