Skip to content

Commit

Permalink
Blur support for Appium (#38)
Browse files Browse the repository at this point in the history
* adding blur support for appium

* version bump
  • Loading branch information
jessezach authored Sep 25, 2019
1 parent 548a7dc commit 2e36c10
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
4 changes: 2 additions & 2 deletions RobotEyes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ def capture_full_screen(self, tolerance=None, blur=[], radius=50):
self.stats[key] = tolerance

# Captures a specific region in a mobile screen
def capture_mobile_element(self, selector, tolerance=None):
def capture_mobile_element(self, selector, tolerance=None, blur=[], radius=50):
tolerance = float(tolerance) if tolerance else self.tolerance
count = self.browser.capture_mobile_element(selector, self.path)
count = self.browser.capture_mobile_element(selector, self.path, blur, radius)
key = 'img' + str(count) + '.png'
self.stats[key] = tolerance

Expand Down
60 changes: 35 additions & 25 deletions RobotEyes/selenium_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@

class SeleniumHooks(object):
count = 0
mobile = False

def __init__(self, lib):
try:
s2l = BuiltIn().get_library_instance(lib)
if lib == 'AppiumLibrary':
self.driver = s2l._current_application()
self.mobile = True
else:
try:
self.driver = s2l.driver #SeleniumLibrary v4
Expand Down Expand Up @@ -46,7 +48,7 @@ def capture_element(self, path, locator, blur=[], radius=50):
im.save(path + '/img' + str(self.count) + '.png')
return self.count

def capture_mobile_element(self, selector, path):
def capture_mobile_element(self, selector, path, blur=[], radius=50):
self.count += 1
prefix, locator, search_element = self.find_element(selector)
location = search_element.location
Expand All @@ -56,6 +58,7 @@ def capture_mobile_element(self, selector, path):
top = location['y']
right = location['x'] + size['width']
bottom = location['y'] + size['height']
self.blur_regions(blur, radius, path) if blur else ''
image = Image.open(path + '/img' + str(self.count) + '.png')
image = image.crop((left, top, right, bottom))
image.save(path + '/img' + str(self.count) + '.png')
Expand Down Expand Up @@ -104,34 +107,41 @@ def find_element(self, selector):
return prefix, locator, search_element

def _get_coordinates(self, prefix, locator, element):
if prefix.lower() == 'xpath':
locator = locator.replace('"', "'")
cmd = "var e = document.evaluate(\"{0}\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null)" \
".singleNodeValue;return e.getBoundingClientRect();".format(locator)
if self.mobile:
coordinates = self._get_coordinates_from_driver(element)
else:
if prefix.lower() == 'xpath':
locator = locator.replace('"', "'")
cmd = "var e = document.evaluate(\"{0}\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null)" \
".singleNodeValue;return e.getBoundingClientRect();".format(locator)

elif prefix.lower() == 'css':
locator = locator.replace('"', "'")
cmd = "var e = document.querySelector(\"{0}\");return e.getBoundingClientRect();".format(locator)
elif prefix.lower() == 'css':
locator = locator.replace('"', "'")
cmd = "var e = document.querySelector(\"{0}\");return e.getBoundingClientRect();".format(locator)

elif prefix.lower() == 'id':
cmd = "var e = document.getElementById(\"{0}\");return e.getBoundingClientRect();".format(locator)
elif prefix.lower() == 'id':
cmd = "var e = document.getElementById(\"{0}\");return e.getBoundingClientRect();".format(locator)

elif prefix.lower() == 'class':
cmd = "var e = document.getElementsByClassName(\"{0}\")[0];return e.getBoundingClientRect();" \
.format(locator)
else:
raise Exception('Invalid locator %s' % locator)
elif prefix.lower() == 'class':
cmd = "var e = document.getElementsByClassName(\"{0}\")[0];return e.getBoundingClientRect();" \
.format(locator)
else:
raise Exception('Invalid locator %s' % locator)

try:
coordinates = self.driver.execute_script(cmd)
except JavascriptException:
coordinates = {}
location = element.location
size = element.size
coordinates['left'] = location['x']
coordinates['top'] = location['y']
coordinates['right'] = location['x'] + size['width']
coordinates['bottom'] = location['y'] + size['height']
try:
coordinates = self.driver.execute_script(cmd)
except JavascriptException:
coordinates = self._get_coordinates_from_driver(element)
return coordinates

def _get_coordinates_from_driver(self, element):
coordinates = {}
location = element.location
size = element.size
coordinates['left'] = location['x']
coordinates['top'] = location['y']
coordinates['right'] = location['x'] + size['width']
coordinates['bottom'] = location['y'] + size['height']
return coordinates

def _update_coordinates(self, left, right, top, bottom):
Expand Down

0 comments on commit 2e36c10

Please sign in to comment.