Skip to content

Commit 712f6b1

Browse files
authored
add android datamatcher (#190)
* add android datamatcher * update changelog * add data matcher * update the test and example * tweak readme
1 parent caa9a9f commit 712f6b1

File tree

5 files changed

+35
-19
lines changed

5 files changed

+35
-19
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Read `release_notes.md` for commit level details.
55
## [Unreleased]
66

77
### Enhancements
8+
- Add `:data_matcher` find_element/s attribute [appium-espresso-driver#386](https://github.com/appium/appium-espresso-driver/pull/386)
89

910
### Bug fixes
1011

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ $ appium --relaxed-security # To run all tests in local
4040
```bash
4141
$ bundle install
4242
$ rake test:func:android # Andorid, uiautomator2
43-
$ AUTOMATION_NAME=espresso rake test:func:android # Andorid, uiautomator2
43+
$ AUTOMATION_NAME_DROID=espresso rake test:func:android # Andorid, uiautomator2
4444
$ rake test:func:ios # iOS
4545
```
4646

lib/appium_lib_core/common/base/search_context.rb

+20-17
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ module SearchContext
1212
# Android
1313
uiautomator: '-android uiautomator', # Unavailable in Espresso
1414
viewtag: '-android viewtag', # Available in Espresso
15+
data_matcher: '-android datamatcher', # Available in Espresso
1516
# iOS
1617
uiautomation: '-ios uiautomation',
1718
predicate: '-ios predicate string',
1819
class_chain: '-ios class chain',
19-
# Windows
20+
# Windows with windows prefix
2021
windows_uiautomation: '-windows uiautomation',
21-
# Tizen
22+
# Tizen with Tizen prefix
2223
tizen_uiautomation: '-tizen uiautomation'
2324
)
2425
# rubocop:enable Layout/AlignHash
@@ -49,47 +50,49 @@ module SearchContext
4950
# @example Find element with each keys
5051
#
5152
# # with accessibility id. All platforms.
52-
# find_elements :accessibility_id, 'Animation'
53-
# find_elements :accessibility_id, 'Animation'
53+
# @driver.find_elements :accessibility_id, 'Animation'
54+
# @driver.find_elements :accessibility_id, 'Animation'
5455
#
5556
# # with base64 encoded template image. All platforms.
56-
# find_elements :image, Base64.strict_encode64(File.read(file_path))
57+
# @driver.find_elements :image, Base64.strict_encode64(File.read(file_path))
5758
#
5859
# # For Android
5960
# ## With uiautomator
60-
# find_elements :uiautomator, 'new UiSelector().clickable(true)'
61+
# @driver.find_elements :uiautomator, 'new UiSelector().clickable(true)'
6162
# ## With viewtag, but only for Espresso
6263
# ## `setTag`/`getTag` in https://developer.android.com/reference/android/view/View
63-
# find_elements :viewtag, 'new UiSelector().clickable(true)'
64+
# @driver.find_elements :viewtag, 'new UiSelector().clickable(true)'
65+
# # With data_matcher. The argument should be JSON format.
66+
# @driver.find_elements :data_matcher, { name: 'hasEntry', args: %w(title Animation) }.to_json
6467
#
6568
# # For iOS
6669
# ## With :predicate
67-
# find_elements :predicate, "isWDVisible == 1"
68-
# find_elements :predicate, 'wdName == "Buttons"'
69-
# find_elements :predicate, 'wdValue == "SearchBar" AND isWDDivisible == 1'
70+
# @driver.find_elements :predicate, "isWDVisible == 1"
71+
# @driver.find_elements :predicate, 'wdName == "Buttons"'
72+
# @driver.find_elements :predicate, 'wdValue == "SearchBar" AND isWDDivisible == 1'
7073
#
7174
# ## With Class Chain
7275
# ### select the third child button of the first child window element
73-
# find_elements :class_chain, 'XCUIElementTypeWindow/XCUIElementTypeButton[3]'
76+
# @driver.find_elements :class_chain, 'XCUIElementTypeWindow/XCUIElementTypeButton[3]'
7477
# ### select all the children windows
75-
# find_elements :class_chain, 'XCUIElementTypeWindow'
78+
# @driver.find_elements :class_chain, 'XCUIElementTypeWindow'
7679
# ### select the second last child of the second child window
77-
# find_elements :class_chain, 'XCUIElementTypeWindow[2]/XCUIElementTypeAny[-2]'
80+
# @driver.find_elements :class_chain, 'XCUIElementTypeWindow[2]/XCUIElementTypeAny[-2]'
7881
# ### matching predicate. <code>`</code> is the mark.
79-
# find_elements :class_chain, 'XCUIElementTypeWindow[`visible = 1][`name = "bla"`]'
82+
# @driver.find_elements :class_chain, 'XCUIElementTypeWindow[`visible = 1][`name = "bla"`]'
8083
# ### containing predicate. `$` is the mark.
8184
# ### Require appium-xcuitest-driver 2.54.0+. PR: https://github.com/facebook/WebDriverAgent/pull/707/files
82-
# find_elements :class_chain, 'XCUIElementTypeWindow[$name = \"bla$$$bla\"$]'
85+
# @driver.find_elements :class_chain, 'XCUIElementTypeWindow[$name = \"bla$$$bla\"$]'
8386
# e = find_element :class_chain, "**/XCUIElementTypeWindow[$name == 'Buttons'$]"
8487
# e.tag_name #=> "XCUIElementTypeWindow"
8588
# e = find_element :class_chain, "**/XCUIElementTypeStaticText[$name == 'Buttons'$]"
8689
# e.tag_name #=> "XCUIElementTypeStaticText"
8790
#
8891
# # For Windows
89-
# find_elements :windows_uiautomation, '....'
92+
# @driver.find_elements :windows_uiautomation, '....'
9093
#
9194
# # For Tizen
92-
# find_elements :tizen_uiautomation, '....'
95+
# @driver.find_elements :tizen_uiautomation, '....'
9396
#
9497
def find_element(*args)
9598
how, what = extract_args(args)

test/functional/android/android/search_context_test.rb

+11
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ def test_viewtag
2929
e = @driver.find_elements :viewtag, 'example'
3030
assert_equal 0, e.size
3131
end
32+
33+
def test_datamatcher
34+
skip 'UiAutomator2 does not support viewtag' if @@core.automation_name != :espresso
35+
36+
e = @driver.find_elements :data_matcher, { name: 'hasEntry', args: %w(title Animation) }.to_json
37+
assert_equal 1, e.size
38+
39+
e.first.click
40+
@driver.find_element :accessibility_id, 'Cloning' # no error
41+
@driver.back
42+
end
3243
end
3344
end
3445
end

test/unit/driver_test.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def test_default_timeout_for_http_client_with_direct
103103

104104
# https://www.w3.org/TR/webdriver1/
105105
def test_search_context_in_element_class
106-
assert_equal 20, ::Selenium::WebDriver::Element::FINDERS.length
106+
assert_equal 21, ::Selenium::WebDriver::Element::FINDERS.length
107107
assert_equal({ class: 'class name',
108108
class_name: 'class name',
109109
css: 'css selector', # Defined in W3C spec
@@ -119,6 +119,7 @@ def test_search_context_in_element_class
119119
custom: '-custom',
120120
uiautomator: '-android uiautomator',
121121
viewtag: '-android viewtag',
122+
data_matcher: '-android datamatcher',
122123
uiautomation: '-ios uiautomation',
123124
predicate: '-ios predicate string',
124125
class_chain: '-ios class chain',

0 commit comments

Comments
 (0)