-
Notifications
You must be signed in to change notification settings - Fork 568
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use normal element #236
use normal element #236
Changes from 1 commit
a0d3f94
722eaef
6bcb955
54965ca
3adf019
fd27458
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -18,7 +18,6 @@ | |||
from .errorhandler import MobileErrorHandler | ||||
from .switch_to import MobileSwitchTo | ||||
from .webelement import WebElement as MobileWebElement | ||||
from .imagelement import ImageElement | ||||
|
||||
from appium.webdriver.clipboard_content_type import ClipboardContentType | ||||
from appium.webdriver.common.mobileby import MobileBy | ||||
|
@@ -27,16 +26,13 @@ | |||
|
||||
from selenium.webdriver.common.by import By | ||||
from selenium.webdriver.support.ui import WebDriverWait | ||||
from selenium.common.exceptions import (TimeoutException, | ||||
WebDriverException, InvalidArgumentException, NoSuchElementException) | ||||
from selenium.common.exceptions import TimeoutException, InvalidArgumentException | ||||
|
||||
from selenium.webdriver.remote.command import Command as RemoteCommand | ||||
|
||||
import base64 | ||||
import copy | ||||
|
||||
DEFAULT_MATCH_THRESHOLD = 0.5 | ||||
|
||||
# From remote/webdriver.py | ||||
_W3C_CAPABILITY_NAMES = frozenset([ | ||||
'acceptInsecureCerts', | ||||
|
@@ -213,8 +209,6 @@ def find_element(self, by=By.ID, value=None): | |||
# elif by == By.NAME: | ||||
# by = By.CSS_SELECTOR | ||||
# value = '[name="%s"]' % value | ||||
if by == By.IMAGE: | ||||
return self.find_element_by_image(value) | ||||
|
||||
return self.execute(RemoteCommand.FIND_ELEMENT, { | ||||
'using': by, | ||||
|
@@ -246,9 +240,6 @@ def find_elements(self, by=By.ID, value=None): | |||
# Return empty list if driver returns null | ||||
# See https://github.com/SeleniumHQ/selenium/issues/4555 | ||||
|
||||
if by == By.IMAGE: | ||||
return self.find_elements_by_image(value) | ||||
|
||||
return self.execute(RemoteCommand.FIND_ELEMENTS, { | ||||
'using': by, | ||||
'value': value})['value'] or [] | ||||
|
@@ -341,51 +332,34 @@ def find_elements_by_android_uiautomator(self, uia_string): | |||
""" | ||||
return self.find_elements(by=By.ANDROID_UIAUTOMATOR, value=uia_string) | ||||
|
||||
def find_element_by_image(self, png_img_path, | ||||
match_threshold=DEFAULT_MATCH_THRESHOLD): | ||||
def find_element_by_image(self, png_img_path): | ||||
"""Finds a portion of a screenshot by an image. | ||||
Uses driver.find_image_occurrence under the hood. | ||||
|
||||
:Args: | ||||
- png_img_path - a string corresponding to the path of a PNG image | ||||
- match_threshold - a double between 0 and 1 below which matches will | ||||
be rejected as element not found | ||||
|
||||
:return: an ImageElement object | ||||
:return: an Element object | ||||
""" | ||||
screenshot = self.get_screenshot_as_base64() | ||||
with open(png_img_path, 'rb') as png_file: | ||||
b64_data = base64.b64encode(png_file.read()).decode('UTF-8') | ||||
try: | ||||
res = self.find_image_occurrence(screenshot, b64_data, | ||||
threshold=match_threshold) | ||||
except WebDriverException as e: | ||||
if 'Cannot find any occurrences' in str(e): | ||||
raise NoSuchElementException(e) | ||||
raise | ||||
rect = res['rect'] | ||||
return ImageElement(self, rect['x'], rect['y'], rect['width'], | ||||
rect['height']) | ||||
|
||||
def find_elements_by_image(self, png_img_path, | ||||
match_threshold=DEFAULT_MATCH_THRESHOLD): | ||||
|
||||
return self.find_element(by=By.IMAGE, value=b64_data) | ||||
|
||||
def find_elements_by_image(self, png_img_path): | ||||
"""Finds a portion of a screenshot by an image. | ||||
Uses driver.find_image_occurrence under the hood. Note that this will | ||||
only ever return at most one element | ||||
|
||||
:Args: | ||||
- png_img_path - a string corresponding to the path of a PNG image | ||||
- match_threshold - a double between 0 and 1 below which matches will | ||||
be rejected as element not found | ||||
|
||||
:return: possibly-empty list of ImageElements | ||||
:return: possibly-empty list of Elements | ||||
""" | ||||
els = [] | ||||
try: | ||||
els.append(self.find_element_by_image(png_img_path, match_threshold)) | ||||
except NoSuchElementException: | ||||
pass | ||||
return els | ||||
with open(png_img_path, 'rb') as png_file: | ||||
b64_data = base64.b64encode(png_file.read()).decode('UTF-8') | ||||
|
||||
return self.find_elements(by=By.IMAGE, value=b64_data) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in java I call this locator type By.imageTemplate. How do you think which one is more appropriate? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah => python-client/appium/webdriver/webdriver.py Line 111 in c6f58bc
Comparing 1 and 2, so far, (I considered the naming, too 🤔 ) |
||||
|
||||
def find_element_by_accessibility_id(self, id): | ||||
"""Finds an element by accessibility id. | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is no necessarily a png file. The format might be anything support by OpenCV itself
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡