Monkey automated testing tools

Outline

Monkey is Android a command-line tool, you can run in the simulator or actual device. It sends a pseudorandom stream to the system user event ( such as a key input, touch screen input, gesture input etc. ) , to achieve the application being developed pressure test . Monkey test is a stability test software, the robustness of the method for rapid and effective. Monkey automated testing tools is a convenient means of visualization tools to improve usability and efficiency.


Set monkey seeds, delayed, execution times

-s <seed> Number Seed effect: a pseudo-random number generator seed value. If you use the same seed run again the value of Monkey , it will generate the same sequence of events.

--throttle <milliseconds> delayed action: inserting a fixed time (msec) delay between events, you can use this setting to slow Monkey running speed, if you do not specify this parameter, then there will be no delay between the events, the event It will be the fastest production.

-v log output level action: each on the command line -v will increase the level of detail of the feedback information. Simple (default), in addition to the start, the test is completed and the final results only provide less information. Medium, provides a more detailed test information, such as sending one by one to the Activity event information. Complex, provides more setup information, such as check or test the selected Activity information.


--ignore-crashes Normally, application crash or abnormal occurrence: the role of Monkey will stop running. If this is set, Monkey will continue to send events to the system until the event count is completed.

--ignore-security-exception  functions: In general, when a program license error occurs (for example, a license is required to start some of Activity time) caused by abnormal, Monkey will stop running. This set, Monkey will continue to send events to the system until the event count is completed.


image.png


Reads the package, select the package name to be tested

-p <allowed-package-name> package effect: If you specify one or more packets, Monkey allow access only to those packets in the Activity . If your application needs to access these packages ( such as selecting a contact ) than the Activity , you need to specify these packages. If you do not specify any package, Monkey will allow the system to start all packages Activity . Specify a plurality of packages, a plurality -p , a -p followed by the package name.

Click to read the package by reading the data / data acquired all of the package name directory, you first need to connect your phone to the PC , to test whether the connection is normal, in the cmd input in adb devices for verification; because tests for more than a specific the APP package, so it is necessary to know the name of the package to be tested package; can also  adb shell pm list packages   list all the package name to find it quickly select all of the application package by all the options have been canceled by the abolition of all options.


image.png


View log , generated Monkey log .

Monkey started by adb connect the phone and run logcat , generate logcat to the specified directory.

A key monkey can automatically generate the default parameters, simply start monkey test.


image.png



Monkey visual interface through the python built wx module is implemented, you can quickly build UI interface.

The main interface of the control arrangement of code:

1.       of MyFrame the main entrance of the entire form, by instantiating wx.Frame displays windows

class MyFrame(wx.Frame):

 

    // Set the default delay time value

delayDefault = "2"

// Set the default number of seeds

seedDefault = "5000000"

// Set the default number of executions

    executionFrequencyDefault = "60000000"

logDir = "./"

def __init__(self):

    // implementation defined

        excuteMode = [ "Ignore crashes"

                    "Ignore unresponsive"

                    "Ignore security exception"

                    "Error interrupt program"

                    "本地代码导致的崩溃",

                    "默认"

                    ]

 

        //日志输出等级区分

        logMode = ["简单","普通","详细"]

        executionModeDefault = excuteMode[0]

        //初始化菜单按钮

        menuBar = wx.MenuBar()

        menu1 = wx.Menu("")

        menuBar.Append(menu1, "File")

        self.SetMenuBar(menuBar)

        //初始化标签栏

        wx.StaticText(panel, -1, "种子数:", pos=(xPos, yPos))

        self.seedCtrl = wx.TextCtrl(panel, -1, "", pos=(xPos1, yPos))

        //绑定点击事件

        self.seedCtrl.Bind(wx.EVT_KILL_FOCUS, self.OnAction)

        self.seedCtrl.SetFocus()

 

        //初始化标签栏

        wx.StaticText(panel, -1, "执行次数:", pos=(xPos, yPos+yDelta))

        //设置窗口位置

        self.excuteNumCtrl = wx.TextCtrl(panel, -1, "", pos=(xPos1, yPos+yDelta))

        //初始化标签栏

        wx.StaticText(panel, -1, "延时:", pos=(xPos, yPos+2*yDelta))

        self.delayNumCtrl = wx.TextCtrl(panel, -1, "", pos=(xPos1, yPos+2*yDelta))

        //初始化标签栏       

        wx.StaticText(panel, -1, "执行方式:", pos=(xPos, yPos+3*yDelta))

        //设置窗口位置

        self.excuteModeCtrl = wx.ComboBox(panel, -1, "", (xPos1,yPos+3*yDelta), choices=excuteMode,style=wx.CB_DROPDOWN)

       

//设置初始化checklistbox,下拉菜单

        self.checkListBox = wx.CheckListBox(panel, -1, (xPos, yPos+4*yDelta ), (400, 350), [])

        wx.StaticText(panel, -1, "日志输出等级:", pos=(xPos, yPoslayout-yDelta))

        self.logModeCtrl = wx.ComboBox(panel, -1, "", (xPos1,yPoslayout-yDelta), choices=logMode,style=wx.CB_DROPDOWN)

 

        //初始化按钮,读取程序包按钮绑定readButton事件

        self.readButton = wx.Button(panel, -1, "读取程序包", pos=(xPos, yPoslayout))

        self.Bind(wx.EVT_BUTTON, self.OnReadClick, self.readButton)

        self.readButton.SetDefault()

 


        //初始化默认参数按钮,绑定defaultButton事件

        self.defaultButton = wx.Button(panel, -1, "默认参数", pos=(xPos, yPoslayout+yDelta))

        self.Bind(wx.EVT_BUTTON, self.OnResetClick, self.defaultButton)

        self.defaultButton.SetDefault()

 

        //初始化一键monkey按钮,按钮绑定quick事件

        self.quickButton = wx.Button(panel, -1, "一键Monkey", pos=(xPos+120, yPoslayout+yDelta))

        self.Bind(wx.EVT_BUTTON, self.OnQuickStartClick, self.quickButton)

        self.quickButton.SetDefault()

 

2.      生成log代码:

    //生成log函数

    def OnBuildLog(self,event):

        os.chdir(self.logDir)  

        //通过日期创建唯一标识文件名称

        date = time.strftime('%Y-%m-%d-%H-%M',time.localtime(time.time()))

        dir_m = "Monkey_Log_"+date.replace("-","")

        dir0 = "sdcard0_log"

        创建目标文件目录

        if (os.path.exists(dir_m+"/"+dir0)):

            print "already exists"

        else:

            os.system("mkdir -p "+dir_m+"/"+dir0)

 

        os.chdir(dir_m)

        //通过adb命令导出log文件到目标文件夹中

        os.system("adb pull /storage/sdcard0/log/ "+dir0)

        //查找异常log文件

        self.BuildFatalLog(os.getcwd())

 

    //遍历所有的log文件函数

def ListFiles(self,path):

    //遍历文件件

        for root,dirs,files in os.walk(path):

            log_f = ""

            for f in files:

                if(f.find("main") == 0):

                    log_f = f.strip()

                    //切换到目标目录

                    os.chdir(root)

                    //通过grep 命令查找所有的异常文件

                    if (log_f != ""):

            grep_cmd="grep-Eni-B2-A20'FATAL|error|exception|system.err|androidruntime' "+log_f+" > "+log_f+"_fatal.log"

                        os.system(grep_cmd)

    //查找异常文件函数

    def BuildFatalLog(self,path):

        self.ListFiles(path)

 

3.      读取程序包代码分析:

//读取程序包函数声明

def OnReadClick(self, event):

    //清空控件内容

        self.checkListBox.Clear()

        //通过读取手机data/data目录来确认所有的包名

        os.system("adb shell ls data/data > ~/log.log")

        //解析log.log文件

        home = os.path.expanduser('~')

        f = open(home+"/log.log", 'r')

        line = f.readline()

        while line:

            line = f.readline()

            if (line != ""):

                print "===="+line

                // will resolve package name, package name checkbox to add display

                self.checkListBox.Append(line)

        f.close()


Guess you like

Origin blog.51cto.com/14367728/2402552