Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@jlipps
Copy link
Member

@jlipps jlipps commented Jun 12, 2018

cc @imurchie @dpgraham @mykola-mokhnach @KazuCocoa .

This represents my vision for how clients should expose the template matching functionality with opencv. I.e., return a "clone" of WebElement with a smaller set of methods, so users can imagine they are simply finding "elements" and clicking on them.

Note that tests will only pass with appium/appium-support#74 applied to the Appium server used for testing.

def test_find_based_on_image_template(self):
image_path = desired_capabilities.PATH('find_by_image_success.png')
el = WebDriverWait(self.driver, 3).until(
EC.presence_of_element_located((By.IMAGE, image_path))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah.. interesting.

screenshot = self.get_screenshot_as_base64()
with open(png_img_path, 'rb') as png_file:
b64_data = base64.encodestring(png_file.read())
b64_data = b64_data.replace("\n", "")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this is needed?

except Exception as e:
if 'Cannot find any occurrences' in str(e):
raise NoSuchElementException(e)
else:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else is redundant after raise

b64_data = b64_data.replace("\n", "")
try:
res = self.find_image_occurrence(screenshot, b64_data)
except Exception as e:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, we only want to catch WebDriverException instances

els = []
try:
els.append(self.find_element_by_image(png_img_path))
except Exception:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

"""
return self.find_elements(by=By.ANDROID_UIAUTOMATOR, value=uia_string)

def find_element_by_image(self, png_img_path):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be handy to pass the threshold to this method explicitly and set it to 0.5 by default, so clients can customise the behaviour if needed

Copy link
Member

@KazuCocoa KazuCocoa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left a couple of comments

Uses driver.find_image_occurrence under the hood.
:Args:
- png_img_path - a string corresponding to the path of a PNG image
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we address match_threshold?

els.append(self.find_element_by_image(png_img_path, match_threshold))
except NoSuchElementException:
pass
return els
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@jlipps jlipps merged commit 5eaf4bd into master Jun 12, 2018
@jlipps jlipps deleted the jlipps-find-by-image branch June 12, 2018 06:47
@jlipps
Copy link
Member Author

jlipps commented Jun 12, 2018

@imurchie mind publishing a new version when you get a chance?

@KazuCocoa KazuCocoa mentioned this pull request Aug 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants