ADB is a tool in the Android SDK. Using ADB, you can directly operate and manage the Android emulator or real Andriod devices.
The main functions of ADB are:
1、 Run Shell (command line) on Android device
2、 Manage port mapping for emulators or devices
3、 Upload/download files between computer and device
4、 Install the local APK software on the computer to the Android emulator or device
Python has two ways to call adb commands, one is os.system() and the other is os.popen().
The difference between the two is that the former cannot get the return value, and the latter can get the return value.
os.popen() returns a file object, which can be read directly using the read() method.
Content expansion:
How to call adb command in python
Two modules, os and subprocess, can be used to execute cmd commands in Python. The difference is that os is blocking and subprocess is non-blocking, so it is more suitable for us to use subprocess. Next, I will give a command to query the connected device to see how it is written in python. The command used is adb devices.
import subprocess
order='adb devices' #Get connected device
pi= subprocess.Popen(order,shell=True,stdout=subprocess.PIPE)
print pi.stdout.read() #Print result
In the actual printing result, you can see that the current computer is connected to three devices. Here we need to explain again that the result of the adb devices command is one-off, so we can use the read method to read data without any problem. However, some of the adb commands return results in real time, such as the command logcat that outputs mobile phone logs. The result will continue to print out the current device operation log information content. If we need to get the print result of this type of command in python, if we still use the read method, the waiting time for the return of the result will be very long, here we have to change One way to read the result is as follows.
import subprocess
order='adb logcat'
pi= subprocess.Popen(order,shell=True,stdout=subprocess.PIPE)for i initer(pi.stdout.readline,'b'):
print I
This printing effect is the same as the operation in cmd, and the log information is printed in real time. Here we use the readline method. In fact, this writing method is similar to reading files, single line reading and full content reading. Because the framework of pyapp has been basically written, I have the idea of writing this article, and share some of python's experience in processing adb commands. At present, the main difference between python in calling adb commands is these two points. The purpose is to find the required function commands to obtain the result data, and then process the returned data through python to achieve the purpose of automated testing. Everyone should use the adb command well, and one thing to note is the combination of various parameters of each command. For example, the implementation of pyapp supports multi-device connection, so when we perform adb command operations on a mobile phone, we need Bring -s plus the device number to indicate the specific device to be operated, otherwise the command will report an error.
For example, when we click on a device, the command should be written like this: adb -s 49dsd4554wdsa shell input tap 600 900, where '49dsd4554wdsa' is the device number, and '600 900' click on the screen coordinates. So you can see that after adding -s, you can easily operate multiple devices at the same time.
So far, this article about the functions of adb in python is introduced. For more detailed explanations of python adb functions, please search for the previous articles of ZaLou.Cn or continue to browse the related articles below. I hope you will support ZaLou.Cn more in the future. !
Recommended Posts