안드로이드에서 MonkeyRunner라는 것을 알고 있는가?


 안드로이드의 어플을 테스트하기 위해서 사용하는 것으로 python을 이용해서 사용하는 테스트 도구이다.


 안드로이드 스튜디오를 설치하면, ADB로 업그레이드 할때, 같이 설치할 수 있는 도구로써, 구글에서 지원해주는 도구이다.


 ADB와 차이나는 것은 ADB에서는 실행 시킬려면 작업이 많이 드는 이미지 매칭 함수가 들어가 있어. 자동화하기에는 매우 좋은 프로그램이라고 할 수 있다.


 MonkeyRunner라는 것은 원래 목적은 Monkey(원숭이)가 폰을 만지는 것과 같이 자동으로 막 만지게 하는 것을 사용 설명서(python으로 만들어져있는 스크립트)를 보고 눌러보는 장치. 라고 보면 될 것이다.


 MonkeyRunner의 설명은 안드로이드 스튜디오자체에서 설명을 해주고 있다.


 링크 : https://developer.android.com/studio/test/monkeyrunner/index.html


 원래 취지는 매우 좋은 용도로 사용한 것이다.

 참고로 소스로 그냥 넘겨도 되는 기능이 있는데, 이 부분은 다른 프로그래머가 유튜브에 언급했듯. 나중에 생기는 오작동을 잡기가 매우 어려워 지게 된다.

 그걸 방지하고, 귀찮으면 컴퓨터에게 시키게 만들자.

 테스터를 구하기 힘들면, 컴퓨터에게 넘기자.

 라는 뜻으로 만들어진 도구이다.[이것을 악용하면, 안 좋은 작업으로 사용할 수 있게 되겠지만...]


 컴퓨터에게 시키는 장치. 즉, 매크로인 것이다.


 링크에 들어가면, 알 수 있지만, 기본적으로는 python 언어를 이용해서 만들어지게 되어 있다.


 그리고 해당 소스를 사용해서 디바이스를 자동으로 동작 시키는 것이다.


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Imports the monkeyrunner modules used by this program
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
 
# Connects to the current device, returning a MonkeyDevice object
device = MonkeyRunner.waitForConnection()
 
# Installs the Android package. Notice that this method returns a boolean, so you can test
# to see if the installation worked.
device.installPackage('myproject/bin/MyApplication.apk')
 
# sets a variable with the package's internal name
package = 'com.example.android.myapplication'
 
# sets a variable with the name of an Activity in the package
activity = 'com.example.android.myapplication.MainActivity'
 
# sets the name of the component to start
runComponent = package + '/' + activity
 
# Runs the component
device.startActivity(component=runComponent)
 
# Presses the Menu button
device.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP)
 
# Takes a screenshot
result = device.takeSnapshot()
 
# Writes the screenshot to a file
result.writeToFile('myproject/shot1.png','png')
cs


 윗 소스는 링크에 있는 예제 소스이다.


 device = MonkeyRunner.waitForConnection()


 이것은 MonkeyRunner라는 클래스(jar로 라이브러리가 있다. 나중에 이 라이브러리를 가지고와서 Java로 다시 구성할 수 있게된다.)에 있는 정적 메소드 waitForConnection()을 이용해서 device에 MonkeyDevice라는 클래스로 만들어져 있는 모바일 디바이스의 정보를 받아들인다.(연결 될 때까지 계속 기다리는 동기화된 함수이다.)


 이렇게  구해진 dveice라는 변수에게 계속 명령을 주는 형태이다.


 device.installPackage라는 것은 해당 APK을 해당 모바일에 설치하도록 명령을 넣는 것이다.


 device.startActivity라는 것은 설정되어 있는 어플을 실행시키는 것이며, press/touch등 동작까지 명령으로 설정할 수 있게 된다.


 takeSnapshot()이라는 것으로 이미지를 메모리에 불러올 수 있게 되며, writeToFile을 이용해서 파일을 입력하도록 할 수 있고, 파일을 읽어 들여서 매칭 시킬 수도 있다.

Posted by JunkMam
,