This repository has been archived by the owner on Mar 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
hzsunshx
committed
Mar 6, 2016
1 parent
f9e43e7
commit 6b11dea
Showing
3 changed files
with
121 additions
and
218 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,214 +1 @@ | ||
Airtest (中文版) | ||
================ | ||
|
||
`|Build Status| <https://travis-ci.org/codeskyblue/airtest>`_ | ||
|
||
代码重构中(不要着急) | ||
==================== | ||
|
||
另外软件的更新也需要 | ||
|
||
为什么要重构 | ||
------------ | ||
|
||
很多的代码不符合pytohn编码规范, | ||
还有一些很冗余的功能夹杂在里面,很不好维护。 | ||
为了能够重现该软件昔日的光芒,是时候擦亮代码,重出江湖了。 | ||
|
||
Contribute | ||
---------- | ||
|
||
如何才能让软件变的更好,这其中也一定需要你的参与才行,发现问题去在github提个issue, | ||
一定会有相应的开发人员看到并处理的。 | ||
|
||
有开发能力的也可以先跟开发者讨论下想贡献的内容,并提相应的PR由开发人员审核。 | ||
|
||
主要更新内容 | ||
------------ | ||
|
||
- 截图方式从adb screencap转成使用uiautomator | ||
|
||
依赖 | ||
---- | ||
|
||
1. python2.7 | ||
2. opencv2.4+ | ||
3. Android4.1+ | ||
|
||
安装 | ||
---- | ||
|
||
1. 首先安装opencv(>=2.4 && <3.0)到你的电脑上 | ||
|
||
windows推荐直接通过pip安装, 根据你是win32还是amd64选择合适的版本 | ||
|
||
**win32** | ||
|
||
:: | ||
|
||
pip install https://github.com/NetEase/aircv/releases/download/cv2binary/opencv_python-2.4.12-cp27-none-win32.whl | ||
|
||
**amd64** | ||
|
||
:: | ||
|
||
pip install https://github.com/NetEase/aircv/releases/download/cv2binary/opencv_python-2.4.12-cp27-none-win_amd64.whl | ||
|
||
2. 安装airtest | ||
|
||
:: | ||
|
||
pip install -U https://github.com/codeskyblue/airtest/archive/master.zip | ||
|
||
3. 安装android依赖 | ||
|
||
下载adb安装到电脑上,推荐下载地址 http://adbshell.com/ | ||
|
||
快速入门 | ||
-------- | ||
|
||
1. 连接一台安卓手机 (4.1+) | ||
|
||
打开windows命令行,执行 ``adb devices``, 请确保看到类似输出, | ||
没有其他的错误 | ||
|
||
:: | ||
|
||
$ adb devices | ||
List of devices attached | ||
EP7333W7XB device | ||
|
||
2. 创建一个python文件 ``test.py``, 内容如下 | ||
|
||
:: | ||
|
||
# coding: utf-8 | ||
import airtest | ||
|
||
d = airtest.connect(None) # 如果多个手机连接电脑,则需要将None改成对应的设备号 | ||
d.screenshot('screen.png') # 截图 | ||
|
||
运行 ``python test.py`` | ||
|
||
3. 截图 | ||
|
||
使用windows的画图板打开 ``screen.png`` 这个文件, | ||
利用其中的截图功能截取需要点击的按钮或者图标 *PS: | ||
这里其实有个IDE截图的最好了,因为时间精力问题还没有做* | ||
|
||
截图后的文件另存为 ``button.png``, ``test.py`` 最后增加一行 | ||
``d.touch_image('button.png')`` | ||
|
||
重新运行 ``python test.py``, 此时差不多可以看到代码可以点击那个按钮了 | ||
|
||
4. 更多 | ||
|
||
可以使用的接口还有很多,请接着往下看 | ||
|
||
接口 | ||
---- | ||
|
||
截图 | ||
~~~~ | ||
|
||
``snapshot(filename)`` | ||
|
||
可以自动识别屏幕的旋转 | ||
|
||
Parameters | ||
|
||
:: | ||
|
||
Name | Type | Description | ||
|
||
---------\|--------\|------------ filename \| string \| **Required** | ||
保存的文件名 | ||
|
||
返回值 | ||
|
||
无 | ||
|
||
点击图片(制作中) | ||
~~~~~~~~~~~~~~~~ | ||
|
||
``touch_image(img)`` | ||
|
||
img support two types string(file path) or TouchImage | ||
|
||
from airtest.types import TouchImage | ||
|
||
Parameters | ||
|
||
Name \| Type \| Description ----------\|-----------\|------------ img | ||
\|string or TouchImage \| 需要点击的图片 | ||
|
||
Example | ||
|
||
:: | ||
|
||
touch_image('start.png') | ||
|
||
# or | ||
touch_image(TouchImage(file='start.png', offset=(0, 0))) | ||
|
||
代码导读 | ||
-------- | ||
|
||
``connect`` 函数负责根据平台返回相应的类(AndroidDevice or IOSDevice) | ||
|
||
图像识别依赖于另一个库 `aircv <https://github.com/netease/aircv>`_, | ||
虽然这个库还不怎么稳定,也还酬和能用吧 | ||
|
||
其他待补充 | ||
|
||
相关的项目 | ||
---------- | ||
|
||
1. 基于opencv的图像识别库 https://github.com/netease/aircv | ||
|
||
License | ||
------- | ||
|
||
This project is under the MIT License. See the `LICENSE <LICENSE>`_ file | ||
for the full license text. | ||
|
||
历史文档 (below, 下面就不要看了) | ||
-------------------------------- | ||
|
||
Python lib for **android** app test. (Not for ios) | ||
|
||
更新说明 | ||
-------- | ||
|
||
0.10.0 | ||
~~~~~~ | ||
|
||
经过慎重的考虑,airtest如果处理的事情太多的的话,整个库就不能很好的维护,也不符合开源的哲学(做一件事,并把它做好),所以去除了监控功能, | ||
去除的iOS支持,windows支持,只保留下安卓的自动化支持。 | ||
|
||
文档 | ||
---- | ||
|
||
在线文档 http://netease.github.io/airtest | ||
|
||
作为在线文档的一个补充,有个pydoc生成的API列表可以作为参考 | ||
http://netease.github.io/airtest/airtest.devsuit.html | ||
|
||
离线文档使用方法: | ||
|
||
:: | ||
|
||
git clone https://github.com/netease/airtest && cd airtest | ||
gem install jekyll | ||
git checkout gh-pages | ||
jekyll serve --baseurl='' | ||
|
||
如何给给该项目贡献 | ||
------------------ | ||
|
||
因为刚项目常常更新,所以可能会有一些没有测试到的bug。 | ||
|
||
可以在发现了问题后,提个issue给作者。 另外一些新的思路也可以提到issue中。 | ||
|
||
.. |Build | ||
Status| image:: https://travis-ci.org/codeskyblue/airtest.svg?branch=master | ||
Documents in <https://github.com/codeskyblue/airtest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,121 @@ | ||
#!/usr/bin/env python | ||
|
||
# USAGE | ||
# python click_and_crop.py --image jurassic_park_kitchen.jpg | ||
|
||
# import the necessary packages | ||
import os | ||
import sys | ||
import argparse | ||
from StringIO import StringIO | ||
|
||
import cv2 | ||
import Tkinter | ||
import tkSimpleDialog | ||
from PIL import ImageTk, Image | ||
|
||
import airtest | ||
|
||
|
||
def make_mouse_callback(imgs, ref_pt): | ||
# initialize the list of reference points and boolean indicating | ||
# whether cropping is being performed or not | ||
cropping = [False] | ||
clone = imgs[0] | ||
|
||
def _click_and_crop(event, x, y, flags, param): | ||
# grab references to the global variables | ||
# global ref_pt, cropping | ||
|
||
# if the left mouse button was clicked, record the starting | ||
# (x, y) coordinates and indicate that cropping is being | ||
# performed | ||
if event == cv2.EVENT_LBUTTONDOWN: | ||
ref_pt[0] = (x, y) | ||
cropping[0] = True | ||
|
||
# check to see if the left mouse button was released | ||
elif event == cv2.EVENT_LBUTTONUP: | ||
# record the ending (x, y) coordinates and indicate that | ||
# the cropping operation is finished | ||
ref_pt[1] = (x, y) | ||
cropping[0] = False | ||
|
||
# draw a rectangle around the region of interest | ||
imgs[1] = image = clone.copy() | ||
cv2.rectangle(image, ref_pt[0], ref_pt[1], (0, 255, 0), 2) | ||
cv2.imshow("image", image) | ||
elif event == cv2.EVENT_MOUSEMOVE and cropping[0]: | ||
img2 = clone.copy() | ||
cv2.rectangle(img2, ref_pt[0], (x, y), (0, 255, 0), 2) | ||
imgs[1] = image = img2 | ||
cv2.imshow("image", image) | ||
return _click_and_crop | ||
|
||
def interactive_save(image, save_to=None): | ||
img_str = cv2.imencode('.png', image)[1].tostring() | ||
imgpil = Image.open(StringIO(img_str)) | ||
|
||
root = Tkinter.Tk() | ||
root.geometry('{}x{}'.format(400, 400)) | ||
imgtk = ImageTk.PhotoImage(image=imgpil) | ||
panel = Tkinter.Label(root, image=imgtk) #.pack() | ||
panel.pack(side="bottom", fill="both", expand="yes") | ||
Tkinter.Button(root, text="Hello!").pack() | ||
# if not save_to: | ||
# save_to = tkSimpleDialog.askstring("Save cropped image", "Enter filename") | ||
if save_to: | ||
if save_to.find('.') == -1: | ||
save_to += '.png' | ||
print 'Save to:', save_to | ||
cv2.imwrite(save_to, image) | ||
root.destroy() | ||
|
||
def main(): | ||
# construct the argument parser and parse the arguments | ||
ap = argparse.ArgumentParser() | ||
ap.add_argument("-s", "--serial", required=False, help="Android serialno") | ||
ap.add_argument("-o", "--output", required=True, help="Output image file, ext must be png") | ||
args = vars(ap.parse_args()) | ||
|
||
output = args["output"] | ||
(_, ext) = os.path.splitext(output) | ||
if ext != '.png': | ||
sys.exit("File must has ext .png") | ||
|
||
d = airtest.connect(args["serial"]) | ||
d.screenshot(output) | ||
|
||
# load the image, clone it, and setup the mouse callback function | ||
image = cv2.imread(output) | ||
clone = image.copy() | ||
images = [clone, image] | ||
ref_pt = [None, None] | ||
|
||
cv2.namedWindow("image") | ||
cv2.setMouseCallback("image", make_mouse_callback(images, ref_pt)) | ||
|
||
# keep looping until the 'q' key is pressed | ||
while True: | ||
# display the image and wait for a keypress | ||
cv2.imshow("image", images[1]) | ||
key = cv2.waitKey(1) & 0xFF | ||
|
||
# if the 'c' key is pressed, break from the loop | ||
if key == ord("c"): | ||
if ref_pt[1] is None: | ||
continue | ||
roi = clone[ref_pt[0][1]:ref_pt[1][1], ref_pt[0][0]:ref_pt[1][0]] | ||
interactive_save(roi, output) | ||
break | ||
elif key == ord("q"): | ||
break | ||
|
||
# if there are two reference points, then crop the region of interest | ||
# from teh image and display it | ||
|
||
# close all open windows | ||
cv2.destroyAllWindows() | ||
|
||
if __name__ == '__main__': | ||
from airtest import console | ||
console.main() | ||
main() |