Skip to content

Commit feb4b04

Browse files
authored
use normal element for find image by (#236)
* use normal element * get rid of png * get rid of imagelement.py * apply formatter
1 parent 2a44439 commit feb4b04

File tree

4 files changed

+32
-97
lines changed

4 files changed

+32
-97
lines changed

appium/webdriver/imagelement.py

Lines changed: 0 additions & 53 deletions
This file was deleted.

appium/webdriver/webdriver.py

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121

2222
from selenium.webdriver.common.by import By
2323
from selenium.webdriver.support.ui import WebDriverWait
24-
from selenium.common.exceptions import (TimeoutException, WebDriverException,
25-
InvalidArgumentException, NoSuchElementException)
24+
from selenium.common.exceptions import TimeoutException, InvalidArgumentException
2625

2726
from selenium.webdriver.remote.command import Command as RemoteCommand
2827

@@ -35,9 +34,6 @@
3534
from .errorhandler import MobileErrorHandler
3635
from .switch_to import MobileSwitchTo
3736
from .webelement import WebElement as MobileWebElement
38-
from .imagelement import ImageElement
39-
40-
DEFAULT_MATCH_THRESHOLD = 0.5
4137

4238
# From remote/webdriver.py
4339
_W3C_CAPABILITY_NAMES = frozenset([
@@ -207,6 +203,7 @@ def find_element(self, by=By.ID, value=None):
207203
:rtype: WebElement
208204
"""
209205
# if self.w3c:
206+
210207
# if by == By.ID:
211208
# by = By.CSS_SELECTOR
212209
# value = '[id="%s"]' % value
@@ -218,8 +215,6 @@ def find_element(self, by=By.ID, value=None):
218215
# elif by == By.NAME:
219216
# by = By.CSS_SELECTOR
220217
# value = '[name="%s"]' % value
221-
if by == By.IMAGE:
222-
return self.find_element_by_image(value)
223218

224219
return self.execute(RemoteCommand.FIND_ELEMENT, {
225220
'using': by,
@@ -251,9 +246,6 @@ def find_elements(self, by=By.ID, value=None):
251246
# Return empty list if driver returns null
252247
# See https://github.com/SeleniumHQ/selenium/issues/4555
253248

254-
if by == By.IMAGE:
255-
return self.find_elements_by_image(value)
256-
257249
return self.execute(RemoteCommand.FIND_ELEMENTS, {
258250
'using': by,
259251
'value': value})['value'] or []
@@ -370,51 +362,34 @@ def find_elements_by_android_viewtag(self, tag):
370362
"""
371363
return self.find_elements(by=By.ANDROID_VIEWTAG, value=tag)
372364

373-
def find_element_by_image(self, png_img_path,
374-
match_threshold=DEFAULT_MATCH_THRESHOLD):
365+
def find_element_by_image(self, img_path):
375366
"""Finds a portion of a screenshot by an image.
376367
Uses driver.find_image_occurrence under the hood.
377368
378369
:Args:
379-
- png_img_path - a string corresponding to the path of a PNG image
380-
- match_threshold - a double between 0 and 1 below which matches will
381-
be rejected as element not found
370+
- img_path - a string corresponding to the path of a image
382371
383-
:return: an ImageElement object
372+
:return: an Element object
384373
"""
385-
screenshot = self.get_screenshot_as_base64()
386-
with open(png_img_path, 'rb') as png_file:
387-
b64_data = base64.b64encode(png_file.read()).decode('UTF-8')
388-
try:
389-
res = self.find_image_occurrence(screenshot, b64_data,
390-
threshold=match_threshold)
391-
except WebDriverException as e:
392-
if 'Cannot find any occurrences' in str(e):
393-
raise NoSuchElementException(e)
394-
raise
395-
rect = res['rect']
396-
return ImageElement(self, rect['x'], rect['y'], rect['width'],
397-
rect['height'])
398-
399-
def find_elements_by_image(self, png_img_path,
400-
match_threshold=DEFAULT_MATCH_THRESHOLD):
374+
with open(img_path, 'rb') as i_file:
375+
b64_data = base64.b64encode(i_file.read()).decode('UTF-8')
376+
377+
return self.find_element(by=By.IMAGE, value=b64_data)
378+
379+
def find_elements_by_image(self, img_path):
401380
"""Finds a portion of a screenshot by an image.
402381
Uses driver.find_image_occurrence under the hood. Note that this will
403382
only ever return at most one element
404383
405384
:Args:
406-
- png_img_path - a string corresponding to the path of a PNG image
407-
- match_threshold - a double between 0 and 1 below which matches will
408-
be rejected as element not found
385+
- img_path - a string corresponding to the path of a image
409386
410-
:return: possibly-empty list of ImageElements
387+
:return: possibly-empty list of Elements
411388
"""
412-
els = []
413-
try:
414-
els.append(self.find_element_by_image(png_img_path, match_threshold))
415-
except NoSuchElementException:
416-
pass
417-
return els
389+
with open(img_path, 'rb') as i_file:
390+
b64_data = base64.b64encode(i_file.read()).decode('UTF-8')
391+
392+
return self.find_elements(by=By.IMAGE, value=b64_data)
418393

419394
def find_element_by_accessibility_id(self, accessibility_id):
420395
"""Finds an element by accessibility id.
-968 Bytes
Loading

test/functional/android/find_by_image_tests.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,30 @@
2121
from selenium.webdriver.support import expected_conditions as EC
2222
import desired_capabilities
2323

24+
import base64
25+
2426

2527
class FindByImageTests(unittest.TestCase):
2628

2729
def setUp(self):
2830
desired_caps = desired_capabilities.get_desired_capabilities('ApiDemos-debug.apk')
2931
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
3032

33+
# relax template matching
34+
self.driver.update_settings({"fixImageFindScreenshotDims": "false",
35+
"fixImageTemplateSize": "true",
36+
"autoUpdateImageElementPosition": "true"})
37+
3138
def tearDown(self):
3239
self.driver.quit()
3340

3441
def test_find_based_on_image_template(self):
3542
image_path = desired_capabilities.PATH('find_by_image_success.png')
43+
with open(image_path, 'rb') as png_file:
44+
b64_data = base64.b64encode(png_file.read()).decode('UTF-8')
45+
3646
el = WebDriverWait(self.driver, 3).until(
37-
EC.presence_of_element_located((By.IMAGE, image_path))
47+
EC.presence_of_element_located((By.IMAGE, b64_data))
3848
)
3949
size = el.size
4050
self.assertIsNotNone(size['width'])
@@ -62,9 +72,12 @@ def test_find_multiple_elements_by_image_just_returns_one(self):
6272

6373
def test_find_throws_no_such_element(self):
6474
image_path = desired_capabilities.PATH('find_by_image_failure.png')
75+
with open(image_path, 'rb') as png_file:
76+
b64_data = base64.b64encode(png_file.read()).decode('UTF-8')
77+
6578
with self.assertRaises(TimeoutException):
6679
WebDriverWait(self.driver, 3).until(
67-
EC.presence_of_element_located((By.IMAGE, image_path))
80+
EC.presence_of_element_located((By.IMAGE, b64_data))
6881
)
6982
with self.assertRaises(NoSuchElementException):
7083
self.driver.find_element_by_image(image_path)

0 commit comments

Comments
 (0)