diff --git a/appium/webdriver/common/appiumby.py b/appium/webdriver/common/appiumby.py index b5d695e1..0241862e 100644 --- a/appium/webdriver/common/appiumby.py +++ b/appium/webdriver/common/appiumby.py @@ -19,6 +19,8 @@ ByType = Literal[ '-ios predicate string', '-ios class chain', + '-android predicate string', + '-android class chain', '-android uiautomator', '-android viewtag', '-android datamatcher', @@ -35,9 +37,19 @@ class AppiumBy(By): + """Appium locator strategies for use with find_element and find_elements.""" + IOS_PREDICATE: ByType = '-ios predicate string' IOS_CLASS_CHAIN: ByType = '-ios class chain' + ANDROID_PREDICATE: ByType = '-android predicate string' + """Match Android elements by a predicate such as ``text == 'Gallery'`` (UiAutomator2 10.2.0+).""" + + ANDROID_CLASS_CHAIN: ByType = '-android class chain' + """Match a path in the Android accessibility tree such as ``**/android.widget.Button[2]`` (UiAutomator2 10.1.0+).""" + ANDROID_UIAUTOMATOR: ByType = '-android uiautomator' + """Deprecated UiSelector strategy. Use ANDROID_PREDICATE or ANDROID_CLASS_CHAIN for element lookup.""" + ANDROID_VIEWTAG: ByType = '-android viewtag' ANDROID_DATA_MATCHER: ByType = '-android datamatcher' ANDROID_VIEW_MATCHER: ByType = '-android viewmatcher' diff --git a/test/unit/webdriver/search_context/android_test.py b/test/unit/webdriver/search_context/android_test.py index cab932f2..8d409e6e 100644 --- a/test/unit/webdriver/search_context/android_test.py +++ b/test/unit/webdriver/search_context/android_test.py @@ -15,6 +15,7 @@ import json import httpretty +import pytest from appium.webdriver.common.appiumby import AppiumBy from appium.webdriver.webelement import WebElement as MobileWebElement @@ -22,22 +23,27 @@ class TestWebDriverAndroidSearchContext: + @pytest.mark.parametrize( + ('by', 'strategy', 'selector'), + [ + (AppiumBy.ID, 'id', 'id data'), + (AppiumBy.ANDROID_PREDICATE, '-android predicate string', "text == 'Gallery'"), + (AppiumBy.ANDROID_CLASS_CHAIN, '-android class chain', '**/android.widget.Button[2]'), + ], + ) @httpretty.activate - def test_find_element_by_id(self): + def test_find_element(self, by, strategy, selector): driver = android_w3c_driver() httpretty.register_uri( httpretty.POST, appium_command('/session/1234567890/element'), body='{"value": {"element-6066-11e4-a52e-4f735466cecf": "element-id"}}', ) - el = driver.find_element( - by=AppiumBy.ID, - value='id data', - ) + el = driver.find_element(by=by, value=selector) d = get_httpretty_request_body(httpretty.last_request()) - assert d['using'] == 'id' - assert d['value'] == 'id data' + assert d['using'] == strategy + assert d['value'] == selector assert isinstance(el, MobileWebElement) @httpretty.activate