Python _ use the python interpreter to execute the monkey runner script

Source: Internet
Author: User

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 ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.