Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions appium/webdriver/common/appiumby.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
ByType = Literal[
'-ios predicate string',
'-ios class chain',
'-android predicate string',
'-android class chain',
'-android uiautomator',
'-android viewtag',
'-android datamatcher',
Expand All @@ -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'
Expand Down
20 changes: 13 additions & 7 deletions test/unit/webdriver/search_context/android_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,35 @@
import json

import httpretty
import pytest

from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.webelement import WebElement as MobileWebElement
from test.unit.helper.test_helper import android_w3c_driver, appium_command, get_httpretty_request_body


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
Expand Down
Loading