Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Monkey Automated testing tool

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

Overview

Monkey is a command-line tool in Android that can be run in a simulator or in a real device. It sends pseudo-random user event streams (such as key input, touch screen input, gesture input, etc.) to the system to realize the stress test of the application under development. Monkey testing is a fast and effective method to test the stability and robustness of software. Monkey automated testing tool is a convenient tool for visual operation, which improves usability and efficiency.

Set the number of monkey seeds, delay, and number of execution times

-s seed count function: the threshold value of the pseudorandom number generator. If you run monkey again with the same threshold value, the same sequence of events will be generated.

-- throttle delay: insert a fixed time delay (millisecond) between events. You can use this setting to slow down Monkey. If you do not specify this parameter, there will be no delay between events and events will be generated as quickly as possible.

-v log output level function: each-v on the command line increases the level of detail of the feedback.

Simple (default), providing less information except for startup, test completion, and final results.

Medium, provides more detailed test information, such as event information sent to Activity one by one.

Complex, provides more setup information, such as selected or unselected Activity information in the test.

-- ignore-crashes function: usually, Monkey stops running when an application crashes or an exception occurs. If this is set, Monkey continues to send events to the system until the event count is complete.

-- ignore-security-exception function: usually, Monkey will stop running when a program has an exception caused by a license error (such as starting some Activity that requires a license). When this is set, Monkey continues to send events to the system until the event count is complete.

Read the package and select the name of the package to be tested

-p package function: if you specify one or more packages, Monkey will only allow access to the Activity in those packages. If your application needs to access Activity other than these packages (such as selecting contacts), you need to specify these packages. If you do not specify any packages, Monkey will allow the system to launch Activity for all packages. Specify multiple packages, using multiple-p, followed by a package name.

Click to read the package to get all the package names by reading the data/data directory. First, you need to connect the phone to PC and test whether the connection is normal. You can type adb devices in cmd to verify. Because the test is for a specific APP package, you need to know the package name of the test package. You can also use adb shell pm list packages to list all package names for lookup, quickly select all application packages by selecting all, and cancel the selected ones by canceling all.

Look at the log and generate the Monkey log.

Starting with Monkey, connect to the phone through adb and run logcat to generate logcat to the specified directory.

One click of monkey can automatically generate default parameters and start monkey testing directly.

The visual interface of Monkey is realized through the built-in wx module of python, which can quickly build the UI interface.

The control layout code of the main interface of the software:

1. MyFrame is the main entry of the whole form, and the window is displayed by instantiating wx.Frame

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 execution

ExecutionFrequencyDefault = "60000000"

LogDir = ". /"

Def _ init__ (self):

/ / definition of execution mode

ExcuteMode = ["ignore program crashes"

"ignore program is not responding"

"ignore security exceptions"

"error interrupts the program"

"crash caused by native code"

"default"

]

/ / distinction of log output levels

LogMode = ["simple", "ordinary", "detailed"]

ExecutionModeDefault = excuteMode [0]

/ / initialize menu button

MenuBar = wx.MenuBar ()

Menu1 = wx.Menu ("")

MenuBar.Append (menu1, "File")

Self.SetMenuBar (menuBar)

/ / initialize the label bar

Wx.StaticText (panel,-1, "seed count:", pos= (xPos, yPos))

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

/ / bind click event

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

Self.seedCtrl.SetFocus ()

/ / initialize the label bar

Wx.StaticText (panel,-1, "number of execution:", pos= (xPos, yPos+yDelta))

/ / set the window location

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

/ / initialize the label bar

Wx.StaticText (panel,-1, "delay:", pos= (xPos, yPos+2*yDelta))

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

/ / initialize the label bar

Wx.StaticText (panel,-1, "execution method:", pos= (xPos, yPos+3*yDelta))

/ / set the window location

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

/ / set initialization checklistbox, drop-down menu

Self.checkListBox = wx.CheckListBox (panel,-1, (xPos, yPos+4*yDelta), (400350), [])

Wx.StaticText (panel,-1, "log output level:", pos= (xPos, yPoslayout-yDelta))

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

/ / initialize button, read package button to bind readButton event

Self.readButton = wx.Button (panel,-1, "read package", pos= (xPos, yPoslayout))

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

Self.readButton.SetDefault ()

/ / initialize default parameter button to bind defaultButton event

Self.defaultButton = wx.Button (panel,-1, "default parameter", pos= (xPos, yPoslayout+yDelta))

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

Self.defaultButton.SetDefault ()

/ / initialize one-click monkey button, which binds the quick event

Self.quickButton = wx.Button (panel,-1, "one-button Monkey", pos= (xPos+120, yPoslayout+yDelta))

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

Self.quickButton.SetDefault ()

two。 Generate log code:

/ / generate log function

Def OnBuildLog (self,event):

Os.chdir (self.logDir)

/ / create a unique identity file name by date

Date = time.strftime ('% Ymuri% mmi% dmure% Hmure% Maureje time.localtime (time.time ()

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

Dir0 = "sdcard0_log"

Create the destination file directory

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

Print "already exists"

Else:

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

Os.chdir (dir_m)

/ / Export the log file to the destination folder through the adb command

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

/ / find the exception log file

Self.BuildFatalLog (os.getcwd ())

/ / traverse all log file functions

Def ListFiles (self,path):

/ / traversing the file

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

Log_f = ""

For f in files:

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

Log_f = f.strip ()

/ / switch to the target directory

Os.chdir (root)

/ / find all exception files through the grep command

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)

/ / find exception file function

Def BuildFatalLog (self,path):

Self.ListFiles (path)

3. Read the package code analysis:

/ / read the package function declaration

Def OnReadClick (self, event):

/ / clear the contents of the control

Self.checkListBox.Clear ()

/ / confirm all package names by reading the phone's data/data directory

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

/ / parse the log.log file

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

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

Line = f.readline ()

While line:

Line = f.readline ()

If (line! = ""):

Print "=" + line

/ / the package name to be resolved. Add the package name shown in checkbox

Self.checkListBox.Append (line)

F.close ()

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report