Preface
The MonkeyRunner is a tool provided by Google Android Development Team and released with Android SDK. The MonkeyRunner is consisted of a set of command and use Jython script as its interpreting program language. The Jython script is process by Java VM rather than C/C++. We can add some logic statement into the script for more flexible control by Jython script.
Before you start to read this article, you need to know how to set up the environment of monkeyrunner. If you don't know how to do it, you can read my another article at http://android-test-tw.blogspot.com/2012/10/android-automation-test-by-monkeyrunner.html in this blog in advance.
Brief Jython:
Let's brief Jython Script for monkeyrunner below before we go over next section.
The variable for Jython language, it can manipulate several type of variable such as Number, String, and List. The variable in Jython doesn't need to be declared. All you have to do is to assign to value to it.
Let's take a look some simple example and description below. You can copy the script to a text file and save it to a file named example.py (file name can be any valid file name in your system). Open a cmd window and type monkeyrunner.bat example.py and press enter key on your computer.
Assign a number to a variable.
num = 1
Assign a string to a variable.
str = "Hello"
str = 'Hello2'
Assign a list to a variable.
list = ['mail','inbox',100,200]
The logic statement and loop control are described as follows,
The if statement in Jython is as follows,
if_stmt ::= "if" expression ":" suite
( "elif" expression ":" suite )*
["else" ":" suite]
Ex:
if num < 0:
num = 0
print('Negative changed to zero')
elif num == 0:
print('Zero')
elif num == 1:
print('Single')
else:
print('More')
The while statement in Jython is as follows,
while_stmt ::= "while" expression ":" suite
["else" ":" suite]
Ex:
num = 100
go = True
while go:
getNum = int(raw_input('Input a number : '))
if getNum == num:
print 'That is right.'
go = False
else:
print 'Oh No~'
else:
print 'Game over.'
print 'Done'
for_stmt ::= "for" target_list "in" expression_list ":" suite
["else" ":" suite]
Ex:
for i in range(9):
print(i)
else:
print 'done'
A simple example:
First, you need to bring up a cmd window on windows operation system and attach your Android device to computer and check if the computer can detect you ADB interface of Android powered device.
In this example we want to verify the result of run-time image and make decision for next action. At this case, we need to add logic statement in monkeyrunner. The logic script refers Jython Script. However we can refer to Python script tutorial for writing statement because Jython uses the same script with Python. Note, not all the Python script and components/class you can use on monkeyrunner. However you can try to use it.
Following example shows you how to turn on Bluetooth radio on your Android device and check the UI (user interface) if Radio icon has set to Bluetooth On. And turn off Bluetooth radio. The goal is to perform turn on and off Bluetooth Radio repeatedly and check the result by UI.
In this case we need to use a method of MonkeyRunner class that official web site doesn't mention. That's a API - MonkeyRunner.loadImageFromFile(Full_File_Path);
In order to compare the screenshot for the testing result, we need to get a screenshot for pass criteria. We can use MonkeyRunner to get the screenshot by Jython script. The piece of script is as follows.
result = device1.takeSnapshot()
result.getSubImage ((133,85,85,28))
result.writeToFile('./capture1.png','PNG')
The screenshot of Bluetooth On below was captured at settings/wireless screen on Android device and ran the above script. Note, the screen resolution is 240x432 and OS version is 4.1 in emulator.
capture1.png
Ex:from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice, MonkeyImage
#Import class of MonkeyRunner, MonkeyDevice, and MonkeyImage.
device1 = MonkeyRunner.waitForConnection(10000,"emulator-5554")
print 'connected'
#Connect Android device from computer and assign handler to variable -device1.
monkeyimage1 = MonkeyRunner.loadImageFromFile('./capture1.png')
print 'capture1.png loaded'
#Load a existing image file by load image API and assign handler to variable - monkeyimage1;
device1.press('KEYCODE_HOME','DOWN_AND_UP',' ')
print 'home key are triggered'
#Send home key event to bring up Launcher
MonkeyRunner.sleep(3);
#Wait for reaction of Android device
device1.press('KEYCODE_MENU','DOWN_AND_UP',' ')
#Send menu key event
MonkeyRunner.sleep(3);
#Wait for reaction of Android device
device1.touch ( 95, 417, "DOWN_AND_UP")
#Touch system settings in menu list
MonkeyRunner.sleep(3);
#Wait for reaction of Android device
for i in range(1000):
device1.touch ( 197, 96, "DOWN_AND_UP")
#Touch bluetooth On
MonkeyRunner.sleep(2);
monkeyimage2 = device1.takeSnapshot()
#Take a screenshot on Android device and assign handler to variable monkeyimage2
monkeyimage2 = monkeyimage2.getSubImage ((133,85,85,28))
#Get specified rectangle from screenshot got by last step for the result we want to check and assign handler to variable monkeyimage2 again.
if monkeyimage2.sameAs ( monkeyimage1, 1 ):
print str(i+1)+':pass'
else:
print str(i+1)+':fail'
#Compare the result if both images of pre-loaded and run-time screenshot are the same.
device1.touch ( 155, 95, "DOWN_AND_UP")
#Touch bluetooth Off
else:
print 'done'
Reference:
Python Script Language Tutorial, http://docs.python.org/tutorial/Jython Script Lanuage Tutorial, http://www.jython.org/docs/index.html
MonkeyRunner, http://developer.android.com/tools/help/monkeyrunner_concepts.html
Android KeyCode, http://developer.android.com/reference/android/view/KeyEvent.html
Isparta
回覆刪除Tunceli
Yozgat
Çorum
Konya
VGP
Burdur
回覆刪除Giresun
Sakarya
Artvin
Mardin
LUX5R
Antalya
回覆刪除Trabzon
Niğde
Maraş
Antep
T4TS
Afyon
回覆刪除Balıkesir
Kırklareli
Amasya
Bilecik
68D
Muğla
回覆刪除Bitlis
Karaman
SED
İstanbul
回覆刪除Sivas
Kırıkkale
Zonguldak
Iğdır
J8D7E3
Aksaray
回覆刪除Aydın
Kütahya
Rize
Bingöl
UFF2
Uşak
回覆刪除Ankara
Adıyaman
Hatay
Şırnak
J28
sakarya
回覆刪除düzce
çankırı
sivas
elazığ
X47YV
whatsapp görüntülü show
回覆刪除ücretli.show
Z2J
Kocaeli Lojistik
回覆刪除Uşak Lojistik
Osmaniye Lojistik
Çorlu Lojistik
Kocaeli Lojistik
SLH
F76F0
回覆刪除Kocaeli Evden Eve Nakliyat
Aydın Evden Eve Nakliyat
Aksaray Evden Eve Nakliyat
Tunceli Evden Eve Nakliyat
Tekirdağ Boya Ustası
order peptides
sarms for sale
Antalya Evden Eve Nakliyat
steroid cycles
41D1D
回覆刪除Ankara Asansör Tamiri
Malatya Şehirler Arası Nakliyat
Pancakeswap Güvenilir mi
Kripto Para Nedir
Çankırı Evden Eve Nakliyat
Ankara Fayans Ustası
Kırşehir Lojistik
Ankara Boya Ustası
Silivri Fayans Ustası
F8F86
回覆刪除Iğdır Lojistik
Gölbaşı Fayans Ustası
Okex Güvenilir mi
Bilecik Parça Eşya Taşıma
Adıyaman Şehirler Arası Nakliyat
Mardin Evden Eve Nakliyat
Burdur Lojistik
Isparta Şehirler Arası Nakliyat
Çerkezköy Çilingir
F0F55
回覆刪除Kırklareli Evden Eve Nakliyat
Tekirdağ Cam Balkon
Edirne Evden Eve Nakliyat
buy anapolon oxymetholone
Eryaman Alkollü Mekanlar
Eskişehir Evden Eve Nakliyat
Referans Kimliği Nedir
parabolan for sale
clenbuterol
5019E
回覆刪除binance indirim kodu %20
5E006
回覆刪除indirim kodu %20
9AB02
回覆刪除amasya sesli sohbet sesli chat
bingöl bedava sohbet
giresun ücretsiz sohbet odaları
canlı sohbet et
Mardin Kadınlarla Sohbet Et
gümüşhane tamamen ücretsiz sohbet siteleri
Kastamonu Sohbet Odaları
samsun bedava sohbet siteleri
hatay random görüntülü sohbet
A6F28
回覆刪除en iyi görüntülü sohbet uygulamaları
osmaniye sesli sohbet mobil
Elazığ Goruntulu Sohbet
isparta canlı görüntülü sohbet siteleri
kızlarla rastgele sohbet
Kars Bedava Sohbet Uygulamaları
Bayburt Rastgele Canlı Sohbet
kırklareli yabancı sohbet
niğde rastgele görüntülü sohbet
F6748
回覆刪除giresun telefonda kızlarla sohbet
Izmir Parasız Sohbet
yabancı görüntülü sohbet uygulamaları
Elazığ Canli Sohbet
ordu mobil sohbet et
mobil sohbet odaları
canlı görüntülü sohbet
seslı sohbet sıtelerı
edirne telefonda sohbet
20E8A
回覆刪除sesli sohbet siteleri
igdir görüntülü sohbet odaları
Aksaray Sesli Görüntülü Sohbet
van random görüntülü sohbet
bolu görüntülü canlı sohbet
kırklareli ücretsiz sohbet odaları
siirt kızlarla rastgele sohbet
canli sohbet chat
kırklareli tamamen ücretsiz sohbet siteleri
11447
回覆刪除Dlive Takipçi Satın Al
Bitcoin Üretme
Sohbet
Kripto Para Oynama
Görüntülü Sohbet
Snapchat Takipçi Hilesi
Likee App Takipçi Hilesi
Tiktok İzlenme Hilesi
Parasız Görüntülü Sohbet
9349E0E394
回覆刪除tiktok düşmeyen takipçi