Python實現對Android截圖

 

背景:

測試過程當中,老是須要對Android設備進行截圖,而後在截圖中標註問題描述;python

手動方式:

1.使用adb scrrencap /sdcard/screen.png 命令對Android設備進行截圖android

2.而後再使用adb pull /sdcard/scrren.png導入到PC端shell

3.使用QQ截圖進行問題描述標註ide

 

自動化實現:


將scrrencap.py文件copy至某個目錄下,直接執行將保存截圖到當前目錄並自動打開展現;
C:\>screencap.py

使用方法:測試

C:\>screencap.py -h
Usage: screencap.py [-d <directory> -f <filename>]

Automatic screenshots for android, After in PC display .

Options:
  -h, --help            show this help message and exit
  -d DIRECTORY, --dir=DIRECTORY
                        directory of save the address
  -f FILENAME, --filename=FILENAME
                        filename of screen shots file name

 

 1 import os
 2 import time
 3 from optparse import OptionParser
 4 
 5 
 6 def option():
 7     # 獲取腳本所在當前目錄
 8     current_dir = os.path.dirname(__file__)
 9     # 根據截圖時間生成默認文件名:20170722142831.png
10     file_name = "%s.png" % time.strftime("%Y%m%d%H%M%S", time.localtime())
11 
12     usage = "screencap.py [-d <directory> -f <filename>]"
13     description = "Automatic screenshots for android, After in PC display ."
14 
15     p = OptionParser(usage=usage, description=description)
16 
17     p.add_option("-d", "--dir",
18                  dest="directory", default=current_dir,
19                  help="directory of save the address")
20 
21     p.add_option("-f", "--filename",
22                  dest="filename", default=file_name,
23                  help="filename of screen shots file name")
24     return p.parse_args()
25 
26 
27 def screen(options):
28     # 截圖
29     print(os.popen("adb shell screencap /sdcard/{filename}".format(filename=options.filename)).read())
30 
31     # 截圖導出
32     print(os.popen(r"adb pull /sdcard/{filename} {dir}".format(filename=options.filename,
33                                                                dir=options.directory)).read())
34     # 打開截圖
35     print(os.popen(r"start {filename}".format(filename=options.filename)).read())
36     # 刪除截圖
37     print(os.popen("adb shell rm /sdcard/{filename}".format(filename=options.filename)))
38 
39 
40 if __name__ == '__main__':
41     options, args = option()
42     # print(options)
43     # print(args)
44     screen(options)
screencap.py

 

  

做者: SaoFox 
出處: http://www.cnblogs.com/sao-fox 本文版權歸做者和博客園共有,歡迎轉載,但未經做者贊成必須保留此段聲明,且在文章頁面明顯位置給出原文鏈接。
相關文章
相關標籤/搜索