{"id":629,"date":"2013-08-02T16:16:36","date_gmt":"2013-08-02T08:16:36","guid":{"rendered":"http:\/\/www.yeetrack.com\/?p=629"},"modified":"2023-11-23T09:56:05","modified_gmt":"2023-11-23T01:56:05","slug":"629","status":"publish","type":"post","link":"https:\/\/www.yeetrack.com\/?p=629","title":{"rendered":"Android\u81ea\u52a8\u5316\u6d4b\u8bd5\u4e4bMonkeyRunner"},"content":{"rendered":"<p>MonkeyRunner\u662fGoogle Android SDK\u4e0b\u7684\u4e00\u4e2a\u5de5\u5177\uff0c\u7528\u4e8e\u81ea\u52a8\u5316\u6d4b\u8bd5Android\u7a0b\u5e8f\u3002<br \/>\nMonkeyrunner\u5de5\u5177\u63d0\u4f9b\u4e86\u4e00\u5957API\uff0c\u4f7f\u7528\u8fd9\u4e9bapi\u5199\u51fa\u7684Python\u7a0b\u5e8f\u53ef\u4ee5\u5728\u9ed1\u76d2\u5730\u63a7\u5236Android\u8bbe\u7f6e\u548c\u6a21\u62df\u5668\u3002\u6709\u4e86Monkeyrunner\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7python\u8bed\u53e5\u63a7\u5236apk\u5305\u7684\u5b89\u88c5\u548c\u5378\u8f7d\u3001\u542f\u52a8app\u3001\u5411app\u53d1\u9001\u5404\u79cd\u52a8\u4f5c\u6307\u4ee4\u3001\u622a\u53d6\u56fe\u7247\u5e76\u4fdd\u5b58\u3002<!--more--><\/p>\n<p>Monkeyrunner\u548cMonkey\u4e0d\u540c\uff0cMonkey\u662f\u76f4\u63a5\u8fd0\u884c\u5728adb shell\u4e2d\u7684\u547d\u4ee4\uff0c\u5b83\u968f\u673a\u7684\u751f\u6210\u7528\u6237\u6216\u8005\u7cfb\u7edf\u7684\u5404\u79cd\u968f\u673a\u4e8b\u4ef6\uff0c\u800cMonkeyRunner\u5219\u53ef\u4ee5\u901a\u8fc7api\u6765\u4ea7\u751f\u7279\u5b9a\u547d\u4ee4\u548c\u4e8b\u4ef6\u6765\u63a7\u5236\u8bbe\u5907\u548c\u6a21\u62df\u5668\u3002<\/p>\n<p>MonkeyRunner\u5728android\u6d4b\u8bd5\u4e2d\u6709\u4e0b\u9762\u7279\u70b9\uff1a<\/p>\n<ul>\n<li>\u652f\u6301\u591a\u8bbe\u5907\uff1a API\u53ef\u4ee5\u8de8\u591a\u4e2a\u8bbe\u5907\u6216\u6a21\u62df\u5668\u5b9e\u65bd\u6d4b\u8bd5\u5957\u4ef6\u3002\u60a8\u53ef\u4ee5\u5728\u540c\u4e00\u65f6\u95f4\u63a5\u4e0a\u6240\u6709\u7684\u8bbe\u5907\u6216\u4e00\u6b21\u542f\u52a8\u5168\u90e8\u6a21\u62df\u5668\uff08\u6216\u7edf\u7edf\u4e00\u8d77\uff09\uff0c\u4f9d\u636e\u7a0b\u5e8f\u4f9d\u6b21\u8fde\u63a5\u5230\u6bcf\u4e00\u4e2a\uff0c\u7136\u540e\u8fd0\u884c\u4e00\u4e2a\u6216\u591a\u4e2a\u6d4b\u8bd5\u3002\u60a8\u4e5f\u53ef\u4ee5\u7528\u7a0b\u5e8f\u542f\u52a8\u4e00\u4e2a\u914d\u7f6e\u597d\u7684\u6a21\u62df\u5668\uff0c\u8fd0\u884c\u4e00\u4e2a\u6216\u591a\u4e2a\u6d4b\u8bd5\uff0c\u7136\u540e\u5173\u95ed\u6a21\u62df\u5668\u3002<\/li>\n<li>\u529f\u80fd\u6d4b\u8bd5\uff1a monkeyrunner\u53ef\u4ee5\u4e3a\u4e00\u4e2a\u5e94\u7528\u81ea\u52a8\u8d2f\u5f7b\u4e00\u6b21\u529f\u80fd\u6d4b\u8bd5\u3002\u60a8\u63d0\u4f9b\u6309\u952e\u6216\u89e6\u6478\u4e8b\u4ef6\u7684\u8f93\u5165\u6570\u503c\uff0c\u7136\u540e\u89c2\u5bdf\u8f93\u51fa\u7ed3\u679c\u7684\u622a\u5c4f\u3002<\/li>\n<li>\u56de\u5f52\u6d4b\u8bd5\uff1amonkeyrunner\u53ef\u4ee5\u8fd0\u884c\u67d0\u4e2a\u5e94\u7528\uff0c\u5e76\u5c06\u5176\u7ed3\u679c\u622a\u5c4f\u4e0e\u65e2\u5b9a\u5df2\u77e5\u6b63\u786e\u7684\u7ed3\u679c\u622a\u5c4f\u76f8\u6bd4\u8f83\uff0c\u4ee5\u6b64\u6d4b\u8bd5\u5e94\u7528\u7684\u7a33\u5b9a\u6027\u3002<\/li>\n<li>\u53ef\u6269\u5c55\u7684\u81ea\u52a8\u5316\uff1a\u7531\u4e8emonkeyrunner\u662f\u4e00\u4e2aAPI\u5de5\u5177\u5305\uff0c\u60a8\u53ef\u4ee5\u57fa\u4e8ePython\u6a21\u5757\u548c\u7a0b\u5e8f\u5f00\u53d1\u4e00\u6574\u5957\u7cfb\u7edf\uff0c\u4ee5\u6b64\u6765\u63a7\u5236Android\u8bbe\u5907\u3002\u9664\u4e86\u4f7f\u7528monkeyrunner API\u4e4b\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528\u6807\u51c6\u7684Python os\u548csubprocess\u6a21\u5757\u6765\u8c03\u7528\u5982adb\u8fd9\u6837\u7684Android\u5de5\u5177\u3002<br \/>\n\u60a8\u8fd8\u53ef\u4ee5\u5411monkeyrunner API\u4e2d\u6dfb\u52a0\u60a8\u81ea\u5df1\u7684\u7c7b\u3002\u6211\u4eec\u5c06\u5728<a href=\"http:\/\/developer.android.com\/guide\/developing\/tools\/monkeyrunner_concepts.html#Plugins\">\u4f7f\u7528\u63d2\u4ef6\u6269\u5c55monkeyrunner<\/a>\u4e00\u8282\u4e2d\u5bf9\u6b64\u8fdb\u884c\u8be6\u7ec6\u8ba8\u8bba\u3002<\/li>\n<\/ul>\n<p>monkeyrunner\u5de5\u5177\u4f7f\u7528Jython\uff08\u4f7f\u7528Java\u7f16\u7a0b\u8bed\u8a00\u7684\u4e00\u79cdPython\u5b9e\u73b0\uff09\u3002Jython\u5141\u8bb8monkeyrunnerAPI\u4e0eAndroid\u6846\u67b6\u8f7b\u677e\u7684\u8fdb\u884c\u4ea4\u4e92\u3002\u4f7f\u7528Jython\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Python\u8bed\u6cd5\u6765\u83b7\u53d6API\u4e2d\u7684\u5e38\u91cf\u3001\u7c7b\u4ee5\u53ca\u65b9\u6cd5\u3002<\/p>\n<h3>\u4e00\u4e2a\u7b80\u5355\u7684MonkeyRunner\u7684\u4f8b\u5b50<\/h3>\n<p>\u4e0b\u9762\u662f\u4e2amonkeyrunner\u7684\u4ee3\u7801\u6837\u4f8b\uff0c\u4e0b\u9762\u4ee3\u7801\u5305\u542b\u4e86\uff0c\u8fde\u63a5\u8bbe\u5907\u3001\u5b89\u88c5apk\u5305\u3001\u542f\u52a8\u7a0b\u5e8f\u3001\u53d1\u9001\u952e\u76d8\u6307\u4ee4\u3001\u622a\u56fe\u3001\u4fdd\u5b58\u622a\u56fe\u3002<\/p>\n<pre><code># python\u5f15\u5165monkeyRunner\u6a21\u5757\nfrom com.android.monkeyrunner import MonkeyRunner, MonkeyDevice\n\n# \u8fde\u63a5\u8bbe\u5907, \u83b7\u5f97\u4e00\u4e2aMonkeyDevice\u5bf9\u8c61\ndevice = MonkeyRunner.waitForConnection()\n\n# \u5b89\u88c5apk\u5305. \u8fd4\u56de\u503c\u662fboolean\uff0c\u53ef\u4ee5\u5224\u65ad\u662f\u5426\u5b89\u88c5\u6210\u529f\ndevice.installPackage('myproject\/bin\/MyApplication.apk')\n\n# sets a variable with the package's internal name\npackage = 'com.example.android.myapplication'\n\n# sets a variable with the name of an Activity in the package\nactivity = 'com.example.android.myapplication.MainActivity'\n\n# sets the name of the component to start\nrunComponent = package + '\/' + activity\n\n# \u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\ndevice.startActivity(component=runComponent)\n\n# \u6309\u4e0b\u624b\u673a\u7684\u83dc\u5355\u952e\ndevice.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP)\n\n# \u622a\u56fe\nresult = device.takeSnapshot()\n\n# \u4fdd\u5b58\u622a\u56fe\nresult.writeToFile('myproject\/shot1.png','png')  \n<\/code><\/pre>\n<h3>\u5e38\u7528API<\/h3>\n<pre><code>#\u9700\u8981\u5f15\u5165\u7684\u6a21\u5757\nfrom com.android.monkeyrunner import MonkeyRunner as mr\nfrom com.android.monkeyrunner import MonkeyDevice as md\nfrom com.android.monkeyrunner import MonkeyImage as mi\n\n#\u7b49\u5f85\u8bbe\u5907\u8fde\u63a5\uff0c30\u79d2\u8d85\u65f6\uff0c\u540e\u9762\u662f\u8bbe\u5907\u540d\u79f0\uff0c\u8be5\u540d\u79f0\u53ef\u4ee5\u901a\u8fc7\u6267\u884c\u547d\u4ee4\u884c<\/code><code>adb devices<\/code>\u83b7\u5f97\ndevice = mr.waitForConnection(30,'123123135002735')\n\n#\u5b89\u88c5apk\u5305\ndevice.installPackage('d:\/\u6709\u9053\u8bcd\u5178V4.0.3.apk'.decode('utf-8'))\n\n#\u5378\u8f7d\u5e94\u7528\u7a0b\u5e8f\ndevice.removePackage('com.youdao.dict')\n\n#\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\ndevice.startActivity(component='com.youdao.dict\/.activity.DictSplashActivity')\n\n#\u7b49\u5f85\u7a0b\u5e8f\u52a0\u8f7d,5\u79d2\nmr.sleep(5)\n\n#\u62d6\u52a8\u64cd\u4f5c\uff0c\u56db\u4e2a\u53c2\u6570\uff0c\u524d\u4e24\u4e2a\u662f\u521d\u59cb\u70b9\u3001\u7ed3\u675f\u70b9\u5750\u6807\uff0c0.5\u662f\u6301\u7eed\u65f6\u95f4\uff0c1\u662f\u6b65\u6570\ndevice.drag((550,500),(100,500), 0.5, 1)\n\n#\u89e6\u6478\u64cd\u4f5c,\u4e09\u4e2a\u53c2\u6570\uff0cX\u5750\u6807\u3001Y\u5750\u6807\uff0c\u89e6\u6478\u7c7b\u578b\ndevice.touch(80, 1050, \"DOWN_AND_UP\")\n\n#\u622a\u56fe\u5e76\u4fdd\u5b58,\u6ce8\u610f\u5982\u679c\u540d\u5b57\u4e2d\u6709\u4e2d\u6587\uff0c\u9700\u8981\u8fdb\u884cutf-8\u7f16\u7801\uff0c\u5426\u5219\u4e71\u7801\nnow = time.strftime(\"%Y-%m-%d-%H-%M-%S\")\nmainPageImage = device.takeSnapshot()\nmainPageImage.writeToFile(\"d:\/\"+\"\u4e3b\u9875\u9762\u622a\u56fe\".decode(\"utf-8\")+now+\".png\", \"png\")\n\n#\u70b9\u51fb\u540e\u9000\u952e,\u952e\u76d8\u7801\u8be6\u60c5\u53ef\u4ee5\u53bb\u67e5sdk\u5e2e\u52a9\u6587\u6863\uff0c\u8def\u5f84\uff1aandroid-sdk-windows\/docs\/reference\/android\/view\/KeyEvent.html\ndevice.press(\"KEYCODE_BACK\", \"DOWN_AND_UP\")\n\n#\u5c06\u65e5\u5fd7\u8f93\u51fa\u5230\u5916\u90e8\u6587\u4ef6,\u5728python\u4e2d\u4f7f\u7528\u4e2d\u6587\uff0c\u9700\u8981\u5728\u6587\u4ef6\u5f00\u5934\u5c06\u7f16\u7801\u8bbe\u7f6e\u4e3autf-8,\u5426\u5219\u4e71\u7801\nlog = open('d:\/monkenyLog.txt', 'w')\nlog.write(\"\u7b49\u5f85\u624b\u673a\u8fde\u63a5...\u3001\\n\")\nlog.close()\n\n#\u622a\u56fe\u6bd4\u8f83\uff0csameAs()\u7b2c\u4e8c\u4e2a\u53c2\u6570\u8868\u793a\u76f8\u4f3c\u5ea6\uff0c0\u8868\u793a\u5b8c\u5168\u4e0d\u76f8\u4f3c\uff0c1\u8868\u793a\u5b8c\u5168\u76f8\u540c\nimageTrue = mr.loadImageFromFile('d:\/shot\/true.png')\nif(imageTrue.sameAs(mainPageImage, 0.75)):\n    log.write('\u622a\u56fe\u6bd4\u8f83\u6210\u529f\\n')\nelse:\n    log.write('\u622a\u56fe\u6bd4\u8f83\u5931\u8d25\\n')\n<\/pre>\n<p>\u5176\u4ed6\u8be6\u7ec6\u4fe1\u606f\u53ef\u53c2\u52a0Android sdk\u6587\u6863\uff0c\u8def\u5f84<code>android-sdk-windows\/docs\/tools\/help\/monkeyrunner_concepts.html<\/code><\/p>\n<h3>\u5f55\u5236\u811a\u672c<\/h3>\n<p>MonkeyRunner\u81ea\u8eab\u63d0\u4f9b\u811a\u672c\u5f55\u5236\u529f\u80fd\uff0c\u5373MonkeyRecorder\u3002<\/p>\n<p>\u5c06\u4e0b\u9762\u4ee3\u7801\u4fdd\u5b58\u6210recorder.py<\/p>\n<pre><code>from com.android.monkeyrunner import MonkeyRunner as mr \nfrom com.android.monkeyrunner.recorder import MonkeyRecorder as recorder \ndevice = mr.waitForConnection() \nrecorder.start(device)\n<\/code><\/pre>\n<p><a href=\"https:\/\/yeetrack-space.bj.bcebos.com\/blog\/201308\/recorder.png\"><img decoding=\"async\" class=\"alignnone size-medium wp-image-630\" alt=\"recorder\" src=\"https:\/\/yeetrack-space.bj.bcebos.com\/blog\/201308\/recorder.png\" \/><\/a><\/p>\n<p>\u5f55\u5236\u4ea7\u751f\u7684\u7ed3\u679c\uff0cmyRecorder.mr:<\/p>\n<pre><code>TOUCH|{'x':92,'y':936,'type':'downAndUp',}\nTOUCH|{'x':357,'y':688,'type':'downAndUp',}\nTOUCH|{'x':285,'y':82,'type':'downAndUp',}\nTYPE|{'message':'hello',}\nTOUCH|{'x':679,'y':82,'type':'downAndUp',}\n<\/code><\/pre>\n<p>\u60f3\u8981\u5f55\u5236\u56de\u653e\uff0c\u8fd8\u8981\u501f\u52a9<code>monkey_playback.py<\/code>\u6587\u4ef6\uff0c\u8fd9\u4e2a\u6587\u4ef6\u7528\u6765\u8fde\u63a5\u624b\u673a\uff0c\u521d\u59cb\u5316\u4e00\u4e9b\u53d8\u91cf\uff0c\u89e3\u6790\u6211\u4eec\u7684myRecorder.mr\u3002<br \/>\n\u4f7f\u7528\u547d\u4ee4<code>monkeyrunner monkey_playback.py myRecorder.mr<\/code>\u5373\u53ef\u8fd0\u884c\u3002<\/p>\n<h3>\u603b\u7ed3<\/h3>\n<p>MonkeyRunner\u63d0\u4f9b\u7684\u64cd\u4f5c\u6709\u9650\uff0c\u9002\u5408\u91cd\u590d\u6027\u3001\u77ed\u8def\u5f84\u7684\u6d4b\u8bd5\uff0c\u4e0d\u9002\u5408\u8fde\u7eed\u6027\u7684\u64cd\u4f5c\uff0c\u7531\u4e8etype\u548cdrag\u65b9\u6cd5\u7684\u4e0d\u7a33\u5b9a\uff0c\u5f88\u5bb9\u6613\u504f\u79bb\u6211\u4eec\u7684\u9884\u671f\u3002MonkeyRunner\u64cd\u4f5c\u4f9d\u8d56\u4e8e\u63a7\u4ef6\u5750\u6807\uff08\u4e5f\u53ef\u4ee5\u901a\u8fc7id\uff0c\u6bd4\u8f83\u8017\u65f6\uff09\uff0c\u4e00\u65e6UI\u53d1\u751f\u6539\u53d8\uff0c\u811a\u672c\u65e0\u6cd5\u4f7f\u7528\u3002<\/p>\n<p>Monkeyrunner\u7684\u7b49\u5f85\u673a\u5236<code>MonkeyRunner.sleep(5)<\/code>,\u65e0\u6cd5\u66f4\u52a0\u53cb\u597d\u5730\u7b49\u5f85\uff1bMonkeyRunner\u4e0d\u63d0\u4f9b\u7ed3\u679c\u65ad\u8a00\uff0c\u6211\u4eec\u53ea\u80fd\u901a\u8fc7\u622a\u56fe\u6bd4\u8f83\uff0c\u65e0\u6cd5\u6446\u8131\u4eba\u5de5\u5e72\u9884\u3002<\/p>\n<p>\u6211\u5728github\u4e0a\u4f20\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff0c\u5730\u5740<a href=\"https:\/\/github.com\/yeetrack\/monkeyrunner\"><\/a><a href=\"https:\/\/github.com\/yeetrack\/monkeyrunner\">https:\/\/github.com\/yeetrack\/monkeyrunner<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>MonkeyRunner\u662fGoogle Android SDK\u4e0b\u7684\u4e00\u4e2a\u5de5\u5177\uff0c\u7528\u4e8e\u81ea\u52a8\u5316\u6d4b\u8bd5Android\u7a0b\u5e8f\u3002 Monkeyrunner\u5de5\u5177\u63d0\u4f9b\u4e86\u4e00\u5957API\uff0c\u4f7f\u7528\u8fd9\u4e9bapi\u5199\u51fa\u7684Python\u7a0b\u5e8f\u53ef\u4ee5\u5728&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[33],"tags":[43,7,16,42],"class_list":["post-629","post","type-post","status-publish","format-standard","hentry","category-coding","tag-android","tag-7","tag-16","tag-42"],"views":8656,"_links":{"self":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/629","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=629"}],"version-history":[{"count":6,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/629\/revisions"}],"predecessor-version":[{"id":1518,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/629\/revisions\/1518"}],"wp:attachment":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=629"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=629"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=629"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}