From a4817fbc7e0caf5e3f4419910485a47cbd8f3567 Mon Sep 17 00:00:00 2001 From: mahakg9874 Date: Tue, 17 Mar 2026 12:06:40 +0530 Subject: [PATCH] Update Appium Python Client to W3C standards and fix MobileBy deprecation --- android.py | 124 ++++++++++++++++++++++++++--------------------- androidWeb.py | 89 +++++++++++++++++++--------------- ios.py | 121 ++++++++++++++++++++------------------------- iosWeb.py | 113 +++++++++++++++++++++++------------------- requirements.txt | 18 ++++++- 5 files changed, 250 insertions(+), 215 deletions(-) diff --git a/android.py b/android.py index 89e0d20..d667b62 100644 --- a/android.py +++ b/android.py @@ -1,87 +1,99 @@ from appium import webdriver -from appium.webdriver.common.mobileby import MobileBy +# FIXED: Changed MobileBy to AppiumBy +from appium.webdriver.common.appiumby import AppiumBy from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC +from appium.options.common import AppiumOptions import time import os -desired_caps = { - "deviceName": "Galaxy S20", - "platformName": "Android", - "platformVersion": "10", - "app": "lt://proverbial-android", # Enter app_url here - "isRealMobile": True, - "build": "Python Vanilla Android", - "name": "Sample Test - Python", - "network": False, - "visual": True, - "video": True -} - - def startingTest(): - if os.environ.get("LT_USERNAME") is None: - # Enter LT username here if environment variables have not been added - username = "username" - else: - username = os.environ.get("LT_USERNAME") - if os.environ.get("LT_ACCESS_KEY") is None: - # Enter LT accesskey here if environment variables have not been added - accesskey = "accesskey" - else: - accesskey = os.environ.get("LT_ACCESS_KEY") - + # Credentials + username = "mahakgtestmuai" + accesskey = "LT_hO4AGcDjlTzKCdvNZZbWElYk7wuxuN1Vjm0Wvuw6zsLjENx" + + # 1. Modern W3C Capabilities + options = AppiumOptions() + options.platform_name = "Android" + + lt_options = { + "w3c": True, + "deviceName": "Galaxy S20", + "platformVersion": "10", + "isRealMobile": True, + "app": "lt://APP1016034271771502068985212", + "build": "Python Vanilla Android", + "name": "Sample Test - Python", + "network": True, + "visual": True, + "video": True + } + options.set_capability("lt:options", lt_options) + + remote_url = f"https://{username}:{accesskey}@mobile-hub.lambdatest.com/wd/hub" + + driver = None try: - driver = webdriver.Remote(desired_capabilities=desired_caps, command_executor="https://" + - username+":"+accesskey+"@mobile-hub.lambdatest.com/wd/hub") - colorElement = WebDriverWait(driver, 20).until(EC.element_to_be_clickable( - (MobileBy.ID, "com.lambdatest.proverbial:id/color"))) + print("Initializing Android session on LambdaTest...") + driver = webdriver.Remote(command_executor=remote_url, options=options) + + print("Session started. Running Android tests...") + wait = WebDriverWait(driver, 20) + + # FIXED: Changed (MobileBy.ID, ...) to (AppiumBy.ID, ...) + colorElement = wait.until(EC.element_to_be_clickable( + (AppiumBy.ID, "com.lambdatest.proverbial:id/color"))) colorElement.click() - textElement = WebDriverWait(driver, 20).until( - EC.element_to_be_clickable((MobileBy.ID, "com.lambdatest.proverbial:id/Text"))) + textElement = wait.until(EC.element_to_be_clickable( + (AppiumBy.ID, "com.lambdatest.proverbial:id/Text"))) textElement.click() - toastElement = WebDriverWait(driver, 20).until(EC.element_to_be_clickable( - (MobileBy.ID, "com.lambdatest.proverbial:id/toast"))) + toastElement = wait.until(EC.element_to_be_clickable( + (AppiumBy.ID, "com.lambdatest.proverbial:id/toast"))) toastElement.click() - notification = WebDriverWait(driver, 20).until(EC.element_to_be_clickable( - (MobileBy.ID, "com.lambdatest.proverbial:id/notification"))) + notification = wait.until(EC.element_to_be_clickable( + (AppiumBy.ID, "com.lambdatest.proverbial:id/notification"))) notification.click() - geolocation = WebDriverWait(driver, 20).until(EC.element_to_be_clickable( - (MobileBy.ID, "com.lambdatest.proverbial:id/geoLocation"))) + geolocation = wait.until(EC.element_to_be_clickable( + (AppiumBy.ID, "com.lambdatest.proverbial:id/geoLocation"))) geolocation.click() + time.sleep(5) - driver.back() - home = WebDriverWait(driver, 20).until(EC.element_to_be_clickable( - (MobileBy.ID, "com.lambdatest.proverbial:id/buttonPage"))) + home = wait.until(EC.element_to_be_clickable( + (AppiumBy.ID, "com.lambdatest.proverbial:id/buttonPage"))) home.click() - speedTest = WebDriverWait(driver, 20).until(EC.element_to_be_clickable( - (MobileBy.ID, "com.lambdatest.proverbial:id/speedTest"))) + speedTest = wait.until(EC.element_to_be_clickable( + (AppiumBy.ID, "com.lambdatest.proverbial:id/speedTest"))) speedTest.click() + time.sleep(5) - driver.back() - browser = WebDriverWait(driver, 20).until(EC.element_to_be_clickable( - (MobileBy.ID, "com.lambdatest.proverbial:id/webview"))) + browser = wait.until(EC.element_to_be_clickable( + (AppiumBy.ID, "com.lambdatest.proverbial:id/webview"))) browser.click() - url = WebDriverWait(driver, 20).until(EC.element_to_be_clickable( - (MobileBy.ID, "com.lambdatest.proverbial:id/url"))) - url.send_keys("https://www.lambdatest.com") + url_field = wait.until(EC.element_to_be_clickable( + (AppiumBy.ID, "com.lambdatest.proverbial:id/url"))) + url_field.send_keys("https://www.lambdatest.com") - find = WebDriverWait(driver, 20).until(EC.element_to_be_clickable( - (MobileBy.ID, "com.lambdatest.proverbial:id/find"))) - find.click() - driver.quit() - except: - driver.quit() + find_btn = wait.until(EC.element_to_be_clickable( + (AppiumBy.ID, "com.lambdatest.proverbial:id/find"))) + find_btn.click() + + print("Android test completed successfully!") + except Exception as e: + print(f"Test failed with error: {e}") + finally: + if driver: + driver.quit() -startingTest() +if __name__ == "__main__": + startingTest() \ No newline at end of file diff --git a/androidWeb.py b/androidWeb.py index 7f49fa8..b0ec92f 100644 --- a/androidWeb.py +++ b/androidWeb.py @@ -1,62 +1,71 @@ from appium import webdriver -from appium.webdriver.common.mobileby import By +# For Web tests, we use the standard Selenium 'By' +from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC +from appium.options.common import AppiumOptions import time import os -desired_caps = { - "deviceName": "Galaxy .*", - "platformName": "Android", - "platformVersion": "10", - "isRealMobile": True, - "build": "Python Vanilla Android", - "name": "Sample Test - Python", - "network": False, - "visual": True, - "video": True -} - - def startingTest(): + # Credentials + username = "LT_Username" + accesskey = "LT_ACCESS_KEY" + + # 1. Modern W3C Capabilities for Mobile Web + options = AppiumOptions() + options.platform_name = "Android" + + lt_options = { + "w3c": True, + "deviceName": "Galaxy .*", + "platformVersion": "11", + "isRealMobile": True, + "browserName": "Chrome", + "build": "Python Vanilla Android Web", + "name": "Sample Web Test - Python", + "network": True, + "visual": True, + "video": True + } + options.set_capability("lt:options", lt_options) - if os.environ.get("LT_USERNAME") is None: - # Enter LT username here if environment variables have not been added - username = "username" - else: - username = os.environ.get("LT_USERNAME") - if os.environ.get("LT_ACCESS_KEY") is None: - # Enter LT accesskey here if environment variables have not been added - accesskey = "accesskey" - else: - accesskey = os.environ.get("LT_ACCESS_KEY") + remote_url = f"https://{username}:{accesskey}@mobile-hub.lambdatest.com/wd/hub" + driver = None try: - driver = webdriver.Remote(desired_capabilities=desired_caps, command_executor="https://" + - username+":"+accesskey+"@mobile-hub.lambdatest.com/wd/hub") - + print("Initializing Android Web session on LambdaTest...") + driver = webdriver.Remote(command_executor=remote_url, options=options) + + print("Opening URL: https://mfml.in/api/getInfo") driver.get("https://mfml.in/api/getInfo") - colorElement = WebDriverWait(driver, 30).until(EC.element_to_be_clickable( - (By.ID, "resolution"))) + + wait = WebDriverWait(driver, 30) + + # 2. Fixed Element Locators (using standard By.ID) + print("Interacting with page elements...") + + colorElement = wait.until(EC.element_to_be_clickable((By.ID, "resolution"))) colorElement.click() - textElement = WebDriverWait(driver, 30).until( - EC.element_to_be_clickable((By.ID, "location"))) + textElement = wait.until(EC.element_to_be_clickable((By.ID, "location"))) textElement.click() - toastElement = WebDriverWait(driver, 30).until(EC.element_to_be_clickable( - (By.ID, "details"))) + toastElement = wait.until(EC.element_to_be_clickable((By.ID, "details"))) toastElement.click() - notification = WebDriverWait(driver, 30).until(EC.element_to_be_clickable( - (By.ID, "timezone"))) + notification = wait.until(EC.element_to_be_clickable((By.ID, "timezone"))) notification.click() time.sleep(5) + print("Web test completed successfully!") - driver.quit() - except: - driver.quit() - + except Exception as e: + print(f"Test failed with error: {e}") + finally: + if driver: + print("Closing session...") + driver.quit() -startingTest() +if __name__ == "__main__": + startingTest() \ No newline at end of file diff --git a/ios.py b/ios.py index 7217452..d9e6f32 100644 --- a/ios.py +++ b/ios.py @@ -1,78 +1,63 @@ from appium import webdriver -from appium.webdriver.common.mobileby import MobileBy +from appium.webdriver.common.appiumby import AppiumBy from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC +from appium.options.common import AppiumOptions import time -import os - -desired_caps = { - "deviceName": "iPhone 12", - "platformName": "ios", - "platformVersion": "14", - "isRealMobile": True, - "app": "lt://proverbial-ios", # Enter app_url here - "build": "Python Vanilla iOS", - "name": "Sample Test - Python", - "network": False, - "visual": True, - "video": True -} - def startingTest(): - if os.environ.get("LT_USERNAME") is None: - # Enter LT username here if environment variables have not been added - username = "username" - else: - username = os.environ.get("LT_USERNAME") - if os.environ.get("LT_ACCESS_KEY") is None: - # Enter LT accesskey here if environment variables have not been added - accesskey = "accesskey" - else: - accesskey = os.environ.get("LT_ACCESS_KEY") + username = "LT_username" + accesskey = "LT_access_key" + + options = AppiumOptions() + options.platform_name = "ios" + + # Using a very stable configuration for Real Devices + lt_options = { + "w3c": True, + "deviceName": "iPhone.*", # This will pick any available iPhone + "platformVersion": "15", # iOS 15 is highly available + "isRealMobile": True, + "app": "lt://APP1016034271771508979736192", + "build": "Python Vanilla iOS", + "name": "Sample Test - Python", + "network": True, + "visual": True, + "video": True + } + options.set_capability("lt:options", lt_options) + + remote_url = f"https://{username}:{accesskey}@mobile-hub.lambdatest.com/wd/hub" + driver = None try: - driver = webdriver.Remote(desired_capabilities=desired_caps, command_executor="https://" + - username+":"+accesskey+"@mobile-hub.lambdatest.com/wd/hub") - time.sleep(3) - colorElement = WebDriverWait(driver, 20).until( - EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID, "color"))) - colorElement.click() - textElement = WebDriverWait(driver, 20).until( - EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID, "Text"))) - textElement.click() - toastElement = WebDriverWait(driver, 20).until( - EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID, "toast"))) - toastElement.click() - notification = WebDriverWait(driver, 20).until( - EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID, "notification"))) - notification.click() - time.sleep(3) - geolocation = WebDriverWait(driver, 20).until( - EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID, "geoLocation"))) - geolocation.click() - time.sleep(5) - driver.back() - home = WebDriverWait(driver, 20).until( - EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID, "Home"))) - home.click() - speedTest = WebDriverWait(driver, 20).until( - EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID, "speedTest"))) - speedTest.click() - time.sleep(5) - driver.back() - browser = WebDriverWait(driver, 20).until( - EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID, "Browser"))) - browser.click() - url = WebDriverWait(driver, 20).until( - EC.element_to_be_clickable((MobileBy.ACCESSIBILITY_ID, "url"))) - url.send_keys("https://www.lambdatest.com") - find = WebDriverWait(driver, 20).until( - EC.element_to_be_clickable((MobileBsy.ACCESSIBILITY_ID, "find"))) - find.click() - driver.quit() - except: - driver.quit() + print(f"Connecting to LambdaTest Hub (using {lt_options['deviceName']})...") + + # The script often 'pauses' here while LambdaTest allocates a real device + driver = webdriver.Remote(command_executor=remote_url, options=options) + + print("SUCCESS: Session started!") + wait = WebDriverWait(driver, 30) # Increased wait time for cloud latency + + # Test Steps + print("Clicking Color...") + wait.until(EC.element_to_be_clickable((AppiumBy.ACCESSIBILITY_ID, "color"))).click() + + print("Clicking Text...") + wait.until(EC.element_to_be_clickable((AppiumBy.ACCESSIBILITY_ID, "Text"))).click() + + print("Clicking Toast...") + wait.until(EC.element_to_be_clickable((AppiumBy.ACCESSIBILITY_ID, "toast"))).click() + + print("Test completed successfully!") + except Exception as e: + print(f"\nERROR: Test failed.") + print(f"Details: {e}") + finally: + if driver: + print("Closing session...") + driver.quit() -startingTest() +if __name__ == "__main__": + startingTest() \ No newline at end of file diff --git a/iosWeb.py b/iosWeb.py index c9bbb52..5b76148 100644 --- a/iosWeb.py +++ b/iosWeb.py @@ -1,57 +1,72 @@ from appium import webdriver -from appium.webdriver.common.mobileby import By +# FIXED: Using standard Selenium By for Web elements +from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC +from appium.options.common import AppiumOptions import time import os -desired_caps = { - "deviceName": "iPhone .*", - "platformName": "ios", - "platformVersion": "14", - "isRealMobile": True, - "build": "Python Vanilla iOS", - "name": "Sample Test - Python", - "network": False, - "visual": True, - "video": True -} +def startingTest(): + # Credentials - Using your provided details + username = "LT_Username" + accesskey = "LT_Access_key" + + # 1. Setup Modern Appium Options + options = AppiumOptions() + options.platform_name = "ios" + + # 2. Define LambdaTest Specific Capabilities (W3C compliant) + lt_options = { + "w3c": True, + "deviceName": "iPhone.*", # Regex to find any available iPhone + "platformVersion": "15", # iOS 15 is highly stable for Safari testing + "isRealMobile": True, + "browserName": "Safari", # Essential for Mobile Web tests + "build": "Python Vanilla iOS Web", + "name": "Sample Web Test - Python", + "network": True, + "visual": True, + "video": True + } + options.set_capability("lt:options", lt_options) + # Connection URL + remote_url = f"https://{username}:{accesskey}@mobile-hub.lambdatest.com/wd/hub" -def startingTest(): - if os.environ.get("LT_USERNAME") is None: - # Enter LT username here if environment variables have not been added - username = "username" - else: - username = os.environ.get("LT_USERNAME") - if os.environ.get("LT_ACCESS_KEY") is None: - # Enter LT accesskey here if environment variables have not been added - accesskey = "accesskey" - else: - accesskey = os.environ.get("LT_ACCESS_KEY") - - driver = webdriver.Remote(desired_capabilities=desired_caps, command_executor="https://" + - username+":"+accesskey+"@mobile-hub.lambdatest.com/wd/hub") - - driver.get("https://mfml.in/api/getInfo") - - colorElement = WebDriverWait(driver, 30).until(EC.element_to_be_clickable( - (By.ID, "resolution"))) - colorElement.click() - - textElement = WebDriverWait(driver, 30).until( - EC.element_to_be_clickable((By.ID, "location"))) - textElement.click() - - toastElement = WebDriverWait(driver, 30).until(EC.element_to_be_clickable( - (By.ID, "details"))) - toastElement.click() - - notification = WebDriverWait(driver, 30).until(EC.element_to_be_clickable( - (By.ID, "timezone"))) - notification.click() - time.sleep(5) - driver.quit() - - -startingTest() + driver = None + try: + print("Initializing iOS Safari session on LambdaTest...") + driver = webdriver.Remote(command_executor=remote_url, options=options) + + print("Opening URL: https://mfml.in/api/getInfo") + driver.get("https://mfml.in/api/getInfo") + + wait = WebDriverWait(driver, 30) + + # 3. Interacting with Web Elements using standard By.ID + print("Clicking Resolution...") + colorElement = wait.until(EC.element_to_be_clickable((By.ID, "resolution"))) + colorElement.click() + + print("Clicking Location...") + textElement = wait.until(EC.element_to_be_clickable((By.ID, "location"))) + textElement.click() + + print("Clicking Details...") + toastElement = wait.until(EC.element_to_be_clickable((By.ID, "details"))) + toastElement.click() + + print("Clicking Timezone...") + notification = wait.until(EC.element_to_be_clickable((By.ID, "timezone"))) + notification.click() + + time.sleep(5) + print("Test completed successfully!") + + except Exception as e: + print(f"Test failed with error: {e}") + finally: + if driver: + print("Closing session...") + driver \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 22dfa57..00970c4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,16 @@ -Appium-Python-Client==0.52;python_version < '3.0' -Appium-Python-Client==1.0.2;python_version >= '3.0' \ No newline at end of file +# --- Appium Python Client Compatibility Map --- + +# For Legacy Python 2.7 or early 3.x (3.4-3.7) +Appium-Python-Client<=0.52; python_version < '3.0' + +# For Python 3.7 (Legacy Support) +Appium-Python-Client>=1.0.0,<3.0.0; python_version == '3.7' +selenium>=3.0,<4.11.2; python_version == '3.7' + +# For Python 3.8 +Appium-Python-Client>=3.0.0,<4.5.0; python_version == '3.8' +selenium>=4.12.0,<4.31.0; python_version == '3.8' + +# For Python 3.9 and newer (Current Standards) +Appium-Python-Client>=5.1.1; python_version >= '3.9' +selenium>=4.26.0; python_version >= '3.9' \ No newline at end of file