Today, I re-read the previously written monkey runner script. I refer to the official monkey runner documentation, saying that subprocess can be used in monkey runner, however, if I enter import subprocess in the monkey runner script, an error is prompted because subprocess requires Python interpreter instead of monkey runner interpreter, so I used python to execute the monkey runner script.
1. The Python code is as follows:
Ubuntu:
Import subprocess
Subprocess. popen ("/android-sdk-linux_x86/tools/monkeyrunner/autotesting/zhekou/Startapp. py", shell = true)
2. Startapp. py is the monkeyrunner script, so you can execute the monkey runner using python.
Windows:
Import subprocess
Subprocess. popen ("E:/androids/tools/monkeyrunner F: \ autotesting \ SRC \ Zhe \ zhe_start_app.py", shell = true)
Add monkeyruner to path and run the following command:
Subprocess. popen ("monkeyrunner F: \ autotesting \ SRC \ Zhe \ zhe_start_app.py", shell = true)
The following error may occur:
Can't open specified script file
Usage: monkeyrunner [Options] script_file
-S monkeyserver IP address.
-P monkeyserver TCP port.
-V monkeyserver logging level (all, finest, finer, fine, config, info, warning, severe, off)
Solution:
Subprocess. popen ("monkeyrunner F:/autotesting/src/zhe/zhe_start_app.py", shell = true)
3. instance code:
Import sys
From Com. Android. monkeyrunner import monkeyrunner as Mr, monkeydevice as MD
Def Startapp (d ):
Pack = "com. AA. BB"
Act = ". splashactivity"
Runcom = pack + "/" + Act
D. startactivity (Component = runcom)
Mr. Sleep (5)
Def screenshot (d ):
Result = D. takesnapshot ()
Result. writetofile ('\ SRC \ AA \ aa_image \ shot1.png', 'png ') # Set the absolute path of Image Storage
Print 'over'
Def main ():
Device = mr. waitforconnection ()
If not device:
Print "device is not found! "
SYS. Exit ()
Print "device is connected! "
Startapp (device)
Screenshot (device)
If _ name __= = '_ main __':
Main ()
4. Start Sina Weibo:
Import sys
From Com. Android. monkeyrunner import monkeyrunner as Mr, monkeydevice as MD
Def Startapp (d ):
Pack = "com. Sina. Weibo"
Act = ". splashactivity"
Runcom = pack + "/" + Act
D. startactivity (Component = runcom)
Mr. Sleep (5)
Def screenshot (d ):
Result = D. takesnapshot ()
Result. writetofile ('f: \ mywork \ autotesting \ SRC \ sina_weibo \ sina_weibo_image \ shot1.png ', 'png ')
Print 'image is saved! '
# Compare Images
Def Execcommand (d ):
Print "Start Execute Command"
Command = "Python compare_image.py"
# Command = "ls-l"
OS. System (command)
Def main ():
Device = mr. waitforconnection ()
If not device:
Print "device is not found! "
SYS. Exit ()
Print "device is connected! "
Startapp (device)
Screenshot (device)
Execcommand (device)
If _ name __= = '_ main __':
Main ()