Created
August 10, 2022 22:20
-
-
Save RohanImmanuel/1952de446274eb87937177196d25be80 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from appium import webdriver | |
import base64 | |
# Reference: http://appium.io/docs/en/advanced-concepts/image-elements/ | |
# BrowserStack Credentials | |
BROWSERSTACK_USERNAME = "<BROWSERSTACK_USERNAME>" | |
BROWSERSTACK_ACCESS_KEY = "<BROWSERSTACK_ACCESS_KEY>" | |
# BrowserStack APP URL | |
BROWSERSTACK_APP_URL = "<BROWSERSTACK_APP_URL>" | |
# Path to element image | |
ELEMENT_IMAGE = "<PATH_TO_ELEMENT_IMAGE>" | |
# template matching method for find element by image | |
methods = ["TM_CCOEFF", "TM_CCOEFF_NORMED", "TM_CCORR", | |
"TM_CCORR_NORMED", "TM_SQDIFF", "TM_SQDIFF_NORMED"] | |
# capabilities for device selection | |
desired_capabilities = { | |
"os_version": "12.0", | |
"deviceName": "Google Pixel 6", | |
"app": BROWSERSTACK_APP_URL, | |
"build": "SET_BROWSERSTACK_BUILD_NAME", | |
"platformName": "android", | |
"browserstack.appium_version" : "1.22.0", | |
"nativeWebTap": "true" | |
} | |
# start a BrowserStack session | |
print("Starting BrowserStack Session...") | |
driver = webdriver.Remote( | |
command_executor= "https://" + BROWSERSTACK_USERNAME + ":" | |
+ BROWSERSTACK_ACCESS_KEY | |
+ "@hub-cloud.browserstack.com/wd/hub", | |
desired_capabilities=desired_capabilities) | |
print("Session: https://app-automate.browserstack.com/dashboard/v2/sessions/" | |
+ driver.session_id) | |
# update appium settings | |
print("\nUpdating Appium Settings") | |
driver.update_settings({ | |
"fixImageTemplateSize": True, | |
"fixImageTemplateScale": True, | |
"getMatchedImageResult": True | |
}) | |
# find element using every template matching method | |
for method in methods: | |
try: | |
# update template matching method | |
print(u'\u2500' * 40) | |
print("Using Method: " + method) | |
driver.update_settings({ | |
"imageMatchMethod": method | |
}) | |
# find element by image | |
ball = driver.find_element_by_image(ELEMENT_IMAGE) | |
print("Element Found") | |
# save debug screenshot | |
canvas_png = base64.b64decode(ball.get_attribute('visual')) | |
with open(r"Debug_Screenshot-" + method + ".png", 'wb') as f: | |
f.write(canvas_png) | |
print("Debug screenshot saved to: ") | |
print("Debug_Screenshot-" + method + ".png") | |
# click on found element | |
ball.click() | |
print("Element Clicked") | |
except Exception as e: | |
print(method + " failed") | |
# End BrowserStack Session | |
driver.quit() | |
print(u'\u2500' * 40) | |
print("\nQuiting BrowserStack Session") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment