tksn.adb - Python wrapper for Android Debug Bridge¶
tksn.adb package is intended to provide simple access to ADB(Android Debug Bridge) from Python programs. Especially, tksn.adb is intended to be used by Android QA engineers who want to automate miscellaneous data collection required upon bug reporting. Such data collection includes logcat, screenshot, screenrecord, bugreport etc.
Contents:
Examples¶
import logging
import time
import tksn.adb
def main():
# Get the first device displayed by 'adb devices'
dev = tksn.adb.Device()
# Print IMEI and Phone number of the device
print('{}: IMEI={}, PhoneNumber={}'.format(
dev.serialno, dev.imei, dev.phone_number))
# === Key press operations ============================
print('Press and release VOLUME DOWN')
dev.key('KEY_VOLUMEDOWN').press()
time.sleep(0.5)
print('Press and hold VOLUME DOWN for 0.5 sec, then release it')
vdown = dev.key('KEY_VOLUMEDOWN').press(auto_release=False)
time.sleep(0.5)
vdown.release()
time.sleep(1.0)
print('Press and hold VOLUME UP for 0.5 sec, then release it')
vup = dev.key('KEY_VOLUMEUP').press(auto_release=False)
time.sleep(0.5)
vup.release()
time.sleep(1.0)
# === File push / pull ================================
print('PUSH this file -> /sdcard/example0.py')
dev.push('.\\example0.py', '/sdcard/example0.py')
print('PULL /sdcard/example0.py -> example0_copy.py')
dev.pull('/sdcard/example0.py', '.\\work\\example0_copy.py')
# === Logcat ==========================================
print('LOGCAT clear')
dev.logcat.clear()
print('LOGCAT capture 5 sec')
logcat = dev.logcat.start()
time.sleep(5.0)
logcat.stop()
logcat.save_as('.\\work\\logcat.txt')
print('LOGCAT capture 3 sec')
# start() takes 'out' parameter. This is for same objective as save_as, but more efficient
with open('.\\work\\logcat-2.txt', 'wb') as f:
# This time, only main and radio is captured
logcat = dev.logcat.start(out=f, buffer=('main', 'radio'))
time.sleep(3.0)
logcat.stop()
# === Screenrecord =====================================
print('SCREENRECORD')
scr = dev.screenrecord.start()
time.sleep(5.0)
scr.stop().save_as('.\\work\\screenrecord.mp4')
# === Screencap ========================================
# Usually screen cap takes less than 5 seconds.
# Therefore it is reasonable to wait completion immediately after start.
# Note that stop() kills subprocess immediately, so you shouldn't use it for screencap.
print('SCREENCAP(1)')
scr = dev.screencap.start().wait().save_as('.\\work\\screencap0.png')
time.sleep(1.0)
# There is a shorter form which does same thing as above.
print('SCREENCAP(2)')
dev.screencap.save_as('.\\work\\screencap1.png')
time.sleep(1.0)
# === Bugreport ========================================
# bugreport usually takes 5 minutes or so.
# You may want to do something while you are waiting.
# You should use wait() instead of stop() otherwise bugreport won't be generated correctly.
print('BUGREPORT')
bugreport = dev.bugreport.start()
time.sleep(10) # do something here
bugreport.wait().save_as('.\\work\\bugreport.txt')
if __name__ == '__main__':
# In this example, logging level is set to INFO, so that you can observe
# adb commands being invoked.
# Usually this is not necessary.
logging.basicConfig(level=logging.INFO)
main()
tksn.adb package¶
tksn.adb package root includes aliases of submodule’s objects:
| Alias | Submodule’s object |
| tksn.adb.Device | tksn.adb.device.Device |
| tksn.adb.DeviceNotFoundError | tksn.adb.device.DeviceNotFoundError |
| tksn.adb.CallError | tksn.adb.call.CallError |
| tksn.adb.get_device_serials | tksn.adb.utils.get_device_serials |