zoukankan      html  css  js  c++  java
  • Android测试monkeyRunner

    monkeyrunner的官方文档:

    https://developer.android.com/studio/test/monkeyrunner

     monkeyrunner脚本可以执行截图操作

    具体执行步骤:

    进入monkeyrunner.bat的文件夹

    D:android_sdk	oolsin
    

    执行dos命令

    monkeyrunner C:UsersuserDesktop	estReportatDocmonkeyrunnerTest.py
    

    遇到报错

    SWT folder '..frameworkx86_64' does not exist. Please set ANDROID_SWT to point to the folder conta

    解决办法: https://blog.csdn.net/qq_21650171/article/details/78986788

    具体python脚本如下:

    import sys
    from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage
    #parameter
    txt1_x=30
    txt1_y=400
    txt2_x=30
    txt2_y=600
    btn1_x=100
    btn1_y=700
    seconds=1
    type='DOWN_AND_UP'
    txt1_msg='zhizhi'
    txt2_msg='yin'
    
    #package name and activity name
    package ='com.patech.testApp'
    activity='.AddressActivity'
    component=package+'/'+activity
    #connect device
    device=MonkeyRunner.waitForConnection()
    
    device.installPackage('C:\Users\user\Desktop\testReport\batDoc\app-debug.apk')
    print ('Installing apk')
    
    device.startActivity(component)
    print('launching apk')
    
    MonkeyRunner.sleep(seconds)
    
    device.touch(txt1_x,txt1_y,type)
    device.type(txt1_msg)
    #device.press('KEYCODE_ENTER','DOWN_AND_UP')
    print ('input txt1')
    
    device.touch(txt2_x,txt2_y,type)
    device.type(txt2_msg)
    #device.press('KEYCODE_ENTER','DOWN_AND_UP')
    print ('input txt2')
    
    MonkeyRunner.sleep(seconds)
    
    device.touch(btn1_x,btn1_y,type)
    print('click button')
    
    #get the snapshot
    picture=device.takeSnapshot()
    picture.writeToFile('C:\Users\user\Desktop\testReport\batDoc\apkTest.png','png')
    print ("you've get a picture")
    device.press('KEYCODE_HOME','DOWN_AND_UP')
    print ('Back to home.')
    

     

    执行一些系统操作:

     

    import sys
    from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage
    #parameter
    device_id='8b4235be'
    device=MonkeyRunner.waitForConnection(1000,device_id)
    device.installPackage('C:\Users\user\Desktop\testReport\batDoc\app-debug.apk')
    #删除应用
    device.remove('com.patech.testApp')
    

      

    获取一些信息(在手机端执行的)

    import android
    droid=android.Android()
    pkgs=droid.getRunningPackages()
    apps=droid.getLaunchableApplications()
    print(pkgs.result)
    print(apps.result)
    

     

    在PC端展示一些提示窗口

    import sys
    from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage
    #parameter
    message = 'please input qq'
    initialValue='350343754'
    title='zhizhi'
    okTitle='save'
    cancelTitle='cancel'
    
    QQ=MonkeyRunner.input(message,initialValue,title,okTitle,cancelTitle)
    MonkeyRunner.alert(QQ,title,okTitle)  

     

     截图到当前目录下

    import os
    import sys
    from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice
    device=MonkeyRunner.waitForConnection()
    picture = device.takeSnapshot()
    path=os.path.abspath(__file__)
    print(path)
    path=os.path.dirname(os.path.abspath(__file__))
    print(path)
    picture.writeToFile(path+'/snapshot.png','png')
    

      

    获取手机的基本信息

    from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice
    device=MonkeyRunner.waitForConnection()
    display=device.getProperty('build.type')
    print(display)
    #getSystemProperty = adb shell getprop
    display=device.getSystemProperty('build.type')
    print(display)
    

      

    图像比较,图像转换,获取像素点,唤醒手机等等省略。

    最后提一下怎么获取monkeyrunner的帮助文档。

    参考:https://www.jianshu.com/p/67e78ff87da1

    在monkeyrunner.bat的同个文件夹下,新建help.py,内容如下,在android源码中有,但是我没下载,网上downlo的:

    下载地址:http://androidxref.com/source/xref/sdk/monkeyrunner/scripts/

    #!/usr/bin/env monkeyrunner
    # Copyright 2010, The Android Open Source Project
    #
    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at
    #
    #     http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.
    from com.android.monkeyrunner import MonkeyRunner as mr
    
    import os
    import sys
    
    supported_formats = ['html', 'text', 'sdk-docs']
    
    if len(sys.argv) != 3:
      print 'help.py: format output'
      sys.exit(1)
    
    (format, saveto_path) = sys.argv[1:]
    
    if not format.lower() in supported_formats:
      print 'format %s is not a supported format' % format
      sys.exit(2)
    
    output = mr.help(format=format)
    if not output:
      print 'Error generating help format'
      sys.exit(3)
    
    dirname = os.path.dirname(saveto_path)
    try:
        os.makedirs(dirname)
    except:
        print 'oops'
        pass # It already existed
    
    fp = open(saveto_path, 'w')
    fp.write(output)
    fp.close()
    
    sys.exit(0)
    

      

    进入monkeyrunner.jar的目录,我的是D:android_sdk oolslib,解压monkeyrunner.jar,将解压后的D:android_sdk oolslib esourcescomandroidmonkeyrunner 目录下的三个.cs文件:html.cs,sdk-doc.cs,text.cs挪到D:android_sdk oolslibcomandroidmonkeyrunner 文件夹中。

    然后执行monkeyrunner的命令:

    monkeyrunner help.py html C:UsersuserDesktop	estReportmonkeyrunner.html
    

      

    文档就downlo到对应的地址了。

    monkeyrunner的脚本录制功能

    从网上下载playback.py和recorder.py,网址:http://androidxref.com/source/xref/sdk/monkeyrunner/scripts/

    运行命令

    monkeyrunner monkey_recorder.py
    

      

    录制完成后保存脚本为.mr格式,执行语句如下:

    monkeyrunner monkey_playback.py C:UsersuserDesktop	estReportatDoc
    ecord.mr
    

      

  • 相关阅读:
    Spinner用法与ListView用法
    ViewPager实现选项卡功能
    android:layout_weight的真实含义
    vb和vb.net事件机制
    go
    挨踢江湖之十一
    蓝桥杯-地铁换乘
    【Android LibGDX游戏引擎开发教程】第06期:图形图像的绘制(下)图片整合工具的使用
    Eclipse3.6 添加JUnit源代码
    【分享】如何使用sublime代码片段快速输入PHP头部版本声明
  • 原文地址:https://www.cnblogs.com/zhizhiyin/p/11347621.html
Copyright © 2011-2022 走看看