Skip to content

Commit

Permalink
add ios support
Browse files Browse the repository at this point in the history
  • Loading branch information
hzliujingming committed Aug 16, 2016
1 parent 64214ee commit 45dc524
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Click target screen area base on OpenCV algorithm.


## Todo
1. Add Web/ios support.
1. Add Web support.
2. Separate _match_ and _click_ method.<br>
due to diff platform depend on diff driver(appium/selenium2library).<br>
3. This lib may just return (_x,y_), call click method by yourself.
47 changes: 43 additions & 4 deletions src/AircvLibrary/AircvLibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
from time import sleep, time
import aircv as ac
from robot.libraries.BuiltIn import BuiltIn
import collections
from PIL import Image
from xml.etree import cElementTree as ET

global CUSTOMER_LIBRARY_NAME
CUSTOMER_LIBRARY_NAME = 'AppiumLibrary'
Display = collections.namedtuple('Display', ['width', 'height'])


class AircvLibrary(object):
Expand Down Expand Up @@ -80,6 +84,7 @@ def mobile_image_set_path(self, path='None'):
def mobile_image_listdir(self):
"""show the target image directory's content
"""
self._get_appium_handle()
self._mobilelib._info("*" * 6)
if self.img_path:
self._mobilelib._info(self.img_path)
Expand Down Expand Up @@ -109,13 +114,22 @@ def _image_click(self, target, index=1):
im_source = ac.imread(self._screen.decode('utf-8').encode('gbk'))
im_search = ac.imread(target.decode('utf-8').encode('gbk'))
result = ac.find_all_template(im_source, im_search, self.TH)

result = sorted(result, key=lambda x: (x['result'][1], x['result'][0]))

if index == 0:
index = len(result) - 1
else:
index -= 1
re = result[index]
self._mobilelib._info(re)
self._mobilelib.click_a_point(re['result'][0], re['result'][1])

_scale = self.scale()
if self._mobilelib._is_android():
self._mobilelib.click_a_point(re['result'][0], re['result'][1])
if self._mobilelib._is_ios():
self._mobilelib.click_a_point(re['result'][0]/_scale, re['result'][1]/_scale)

return re

def mobile_click_image(self, target, index=1):
Expand Down Expand Up @@ -161,9 +175,17 @@ def _parse_image_in(self, parent_image, sub_image, phase_type='click'):
if self._coordinate_cmp(in_rect[3], result_rect[3]): # right-down
try:
if 'click' in phase_type:
self._mobilelib.click_a_point(result[i]['result'][0], result[i]['result'][1])
if self._mobilelib._is_android():
self._mobilelib.click_a_point(result[i]['result'][0], result[i]['result'][1])
if self._mobilelib._is_ios():
_scale = self.scale()
self._mobilelib.click_a_point(result[i]['result'][0]/_scale, result[i]['result'][1]/_scale)
elif 'coordinate' in phase_type:
return result[i]['result'][0], result[i]['result'][1]
if self._mobilelib._is_android():
return result[i]['result'][0], result[i]['result'][1]
if self._mobilelib._is_ios():
_scale = self.scale()
return result[i]['result'][0]/_scale, result[i]['result'][1]/_scale
except Exception, e:
print '[xxx]: %s ' % traceback.format_exc()
# Todo : return valid value
Expand Down Expand Up @@ -197,13 +219,19 @@ def _parse_images(self, target, index=1, parse_type='num'):
if 'num' in parse_type:
return len(result)
elif 'location' in parse_type:
result = sorted(result, key=lambda x: (x['result'][1], x['result'][0]))
if index == 0:
index = len(result) - 1
else:
index -= 1
re = result[index]
self._mobilelib._info(re)
return re['result'][0], re['result'][1]

if self._mobilelib._is_android():
return re['result'][0], re['result'][1]
if self._mobilelib._is_ios():
_scale = self.scale()
return re['result'][0]/_scale, re['result'][1]/_scale

def mobile_screen_should_contain(self, target):
"""assert current screen should contain target image
Expand Down Expand Up @@ -236,3 +264,14 @@ def _coordinate_cmp(self, a, b):
else:
return False
return True

def scale(self):
img = Image.open(self._screen)
display = Display(*sorted(img.size))
page = self._mobilelib.get_source()
tree = ET.ElementTree(ET.fromstring(page))
elem_of_UIAApplication = tree.find('UIAApplication')
_width = elem_of_UIAApplication.attrib['width']
_height = elem_of_UIAApplication.attrib['height']
_scale = min(display) / min(int(_width), int(_height))
return _scale
2 changes: 2 additions & 0 deletions src/AircvLibrary/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from AircvLibrary import AircvLibrary
__all__ = ['AircvLibrary']

0 comments on commit 45dc524

Please sign in to comment.