Skip to content

Commit 109139b

Browse files
Merge pull request #207 from appium/no_xpath
No xpath
2 parents 0c51bca + 682797d commit 109139b

File tree

12 files changed

+169
-179
lines changed

12 files changed

+169
-179
lines changed

android_tests/lib/android/specs/android/element/button.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def fade_in
2020

2121
t 'button' do
2222
# by index
23-
wait { button(1).name.must_equal fade_in }
23+
2.times { wait { button(1).name.must_equal fade_in } }
2424

2525
# by name contains
2626
wait { button('ade').name.must_equal fade_in }
@@ -45,7 +45,7 @@ def fade_in
4545
end
4646

4747
t 'buttons_exact' do
48-
wait { buttons_exact(fade_in).first.name.must_equal fade_in }
48+
2.times { wait { buttons_exact(fade_in).first.name.must_equal fade_in } }
4949
end
5050

5151
t { after_last }

android_tests/lib/android/specs/android/helper.rb

+5-6
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,10 @@ def id_value
6868
end
6969

7070
t 'find by id' do
71-
wait do
72-
find('accessibility').click
73-
find('accessibility node provider').click
74-
id 'accessibility_node_provider' # Accessibility/Accessibility Node Provider
75-
2.times { back }
76-
end
71+
wait { find('accessibility').click }
72+
wait { find('accessibility node provider').click }
73+
wait { id 'accessibility_node_provider' } # Accessibility/Accessibility Node Provider
74+
2.times { back }
7775
end
76+
7877
end

android_tests/lib/android/specs/common/device.rb

+10-13
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@
4141
end
4242

4343
t 'set_context' do
44-
wait do
45-
scroll_to('Views').click
46-
scroll_to('WebView').click
47-
set_context 'WEBVIEW'
48-
current_context.must_equal 'WEBVIEW_1'
49-
end
44+
wait { scroll_to('Views').click }
45+
wait { scroll_to('WebView').click }
46+
wait { set_context 'WEBVIEW' }
47+
wait { current_context.must_equal 'WEBVIEW_1' }
48+
49+
wait { set_context 'NATIVE_APP' }
50+
wait { current_context.must_equal 'NATIVE_APP' }
5051

51-
set_context 'NATIVE_APP'
5252
2.times { back; sleep 1 }
5353
end
5454

@@ -66,10 +66,7 @@
6666
end
6767

6868
t 'app_strings' do
69-
wait do
70-
strs = app_strings
71-
strs.has_key?('activity_save_restore').must_equal true
72-
end
69+
wait_true { app_strings.has_key? 'activity_save_restore' }
7370
end
7471

7572
def must_return_element element
@@ -116,9 +113,9 @@ def must_return_element element
116113
end
117114

118115
t 'push and pull file' do
116+
file = 'A Fine Day'
117+
path = '/data/local/tmp/remote.txt'
119118
wait do
120-
file = "A Fine Day"
121-
path = "/data/local/tmp/remote.txt"
122119
push_file path, file
123120
read_file = pull_file path
124121
read_file.must_equal file

android_tests/lib/android/specs/common/helper.rb

-14
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,6 @@
7878
wait { tags('android.widget.TextView').length.must_equal 12 }
7979
end
8080

81-
t 'find_ele_by_attr_include' do
82-
wait do
83-
el_text = find_ele_by_attr_include('android.widget.TextView', :text, 'acc').text
84-
el_text.must_equal 'Accessibility'
85-
end
86-
end
87-
88-
t 'find_eles_by_attr_include' do
89-
wait do
90-
ele_count = find_eles_by_attr_include('android.widget.TextView', :text, 'e').length
91-
ele_count.must_equal 7
92-
end
93-
end
94-
9581
t 'first_ele' do
9682
wait do
9783
first_ele('android.widget.TextView').text.must_equal 'API Demos'

android_tests/lib/android/specs/driver.rb

+11-3
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ def is_sauce
3333
actual[:caps][:app] = File.basename actual[:caps][:app]
3434
expected = { caps: { platformName: 'android',
3535
app: 'api.apk',
36-
appPackage: '',
37-
appActivity: '', },
36+
appPackage: 'io.appium.android.apis',
37+
appActivity: '.ApiDemos', },
3838
custom_url: false,
3939
export_session: false,
4040
default_wait: 1,
@@ -45,7 +45,15 @@ def is_sauce
4545
device: :android,
4646
debug: true, }
4747

48-
actual.must_equal expected
48+
if actual != expected
49+
diff = HashDiff.diff expected, actual
50+
diff = "diff (expected, actual):\n#{diff}"
51+
# example:
52+
# change :ios in expected to match 'ios' in actual
53+
# [["~", "caps.platformName", :ios, "ios"]]
54+
message = "\n\nactual:\n\n: #{actual.ai}expected:\n\n#{expected.ai}\n\n#{diff}"
55+
fail message
56+
end
4957
end
5058
end
5159

android_tests/lib/run.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'rubygems'
22
require 'spec'
33
require 'fakefs/safe'
4+
require 'hashdiff'
45
require_relative '../../lib/appium_lib'
56

67
=begin

lib/appium_lib/android/element/button.rb

+39-16
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,43 @@ module Android
77
private
88

99
# @private
10-
def _button_visible_string opts={}
11-
index = opts.fetch :index, false
12-
if index
13-
%Q(//#{Button}[#{index}] | //#{ImageButton}[#{index}])
10+
def _button_visible_selectors opts={}
11+
button_index = opts.fetch :button_index, false
12+
image_button_index = opts.fetch :image_button_index, false
13+
14+
# complex_find(...)
15+
# 4 = className(String className)
16+
# 9 = instance(final int instance)
17+
18+
if button_index && image_button_index
19+
[
20+
# className().instance()
21+
[[4, Button], [9, button_index]],
22+
# className().instance()
23+
[[4, ImageButton], [9, image_button_index]]
24+
]
1425
else
15-
%Q(//#{Button} | //#{ImageButton})
26+
[
27+
# className()
28+
[[4, Button]],
29+
# className()
30+
[[4, ImageButton]]
31+
]
1632
end
1733
end
1834

1935
# @private
2036
def _button_exact_string value
2137
button = string_visible_exact Button, value
2238
image_button = string_visible_exact ImageButton, value
23-
"#{button} | #{image_button}"
39+
button + image_button
2440
end
2541

2642
# @private
2743
def _button_contains_string value
2844
button = string_visible_contains Button, value
2945
image_button = string_visible_contains ImageButton, value
30-
"#{button} | #{image_button}"
46+
button + image_button
3147
end
3248

3349
public
@@ -41,47 +57,54 @@ def button value
4157
# Android needs to combine button and image button to match iOS.
4258
if value.is_a? Numeric
4359
index = value
44-
raise "#{index} is not a valid xpath index. Must be >= 1" if index <= 0
60+
raise "#{index} is not a valid index. Must be >= 1" if index <= 0
4561

46-
return xpath _button_visible_string index: index
62+
return complex_find _button_visible_selectors index: index
4763
end
4864

49-
xpath _button_contains_string value
65+
complex_find _button_contains_string value
5066
end
5167

5268
# Find all buttons containing value.
5369
# If value is omitted, all buttons are returned.
5470
# @param value [String] the value to search for
5571
# @return [Array<Button>]
5672
def buttons value=false
57-
return xpaths _button_visible_string unless value
58-
xpaths _button_contains_string value
73+
return complex_find mode: 'all', selectors: _button_visible_selectors unless value
74+
complex_find mode: 'all', selectors: _button_contains_string(value)
5975
end
6076

6177
# Find the first button.
6278
# @return [Button]
6379
def first_button
64-
xpath _button_visible_string
80+
complex_find _button_visible_selectors button_index: 0, image_button_index: 0
6581
end
6682

6783
# Find the last button.
6884
# @return [Button]
6985
def last_button
70-
xpath _button_visible_string index: 'last()'
86+
# uiautomator index doesn't support last
87+
# and it's 0 indexed
88+
button_index = tags(Button).length
89+
button_index -= 1 if button_index >= 0
90+
image_button_index = tags(ImageButton).length
91+
image_button_index -= 1 if image_button_index >= 0
92+
93+
complex_find _button_visible_selectors button_index: button_index, image_button_index: image_button_index
7194
end
7295

7396
# Find the first button that exactly matches value.
7497
# @param value [String] the value to match exactly
7598
# @return [Button]
7699
def button_exact value
77-
xpath _button_exact_string value
100+
complex_find _button_exact_string value
78101
end
79102

80103
# Find all buttons that exactly match value.
81104
# @param value [String] the value to match exactly
82105
# @return [Array<Button>]
83106
def buttons_exact value
84-
xpaths _button_exact_string value
107+
complex_find mode: 'all', selectors: _button_exact_string(value)
85108
end
86109
end # module Android
87110
end # module Appium

lib/appium_lib/android/element/generic.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@ module Android
55
# @param value [String] the value to search for
66
# @return [Element]
77
def find value
8-
xpath_visible_contains '*', value
8+
complex_find_contains '*', value
99
end
1010

1111
# Find all elements containing value
1212
# @param value [String] the value to search for
1313
# @return [Array<Element>]
1414
def finds value
15-
xpaths_visible_contains '*', value
15+
complex_finds_contains '*', value
1616
end
1717

1818
# Find the first element exactly matching value
1919
# @param value [String] the value to search for
2020
# @return [Element]
2121
def find_exact value
22-
xpath_visible_exact '*', value
22+
complex_find_exact '*', value
2323
end
2424

2525
# Find all elements exactly matching value
2626
# @param value [String] the value to search for
2727
# @return [Array<Element>]
2828
def finds_exact value
29-
xpaths_visible_exact '*', value
29+
complex_finds_exact '*', value
3030
end
3131

3232
# Scroll to the first element containing target text or description.

lib/appium_lib/android/element/text.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module Android
99
# @return [TextView]
1010
def text value
1111
return ele_index TextView, value if value.is_a? Numeric
12-
xpath_visible_contains TextView, value
12+
complex_find_contains TextView, value
1313
end
1414

1515
# Find all TextViews containing value.
@@ -18,7 +18,7 @@ def text value
1818
# @return [Array<TextView>]
1919
def texts value=false
2020
return tags TextView unless value
21-
xpaths_visible_contains TextView, value
21+
complex_finds_contains TextView, value
2222
end
2323

2424
# Find the first TextView.
@@ -37,14 +37,14 @@ def last_text
3737
# @param value [String] the value to match exactly
3838
# @return [TextView]
3939
def text_exact value
40-
xpath_visible_exact TextView, value
40+
complex_find_exact TextView, value
4141
end
4242

4343
# Find all TextViews that exactly match value.
4444
# @param value [String] the value to match exactly
4545
# @return [Array<TextView>]
4646
def texts_exact value
47-
xpaths_visible_exact TextView, value
47+
complex_finds_exact TextView, value
4848
end
4949
end # module Android
5050
end # module Appium

lib/appium_lib/android/element/textfield.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Android
88
# @return [EditText]
99
def textfield value
1010
return ele_index EditText, value if value.is_a? Numeric
11-
xpath_visible_contains EditText, value
11+
complex_find_contains EditText, value
1212
end
1313

1414
# Find all EditTexts containing value.
@@ -17,7 +17,7 @@ def textfield value
1717
# @return [Array<EditText>]
1818
def textfields value=false
1919
return tags EditText unless value
20-
xpaths_visible_contains EditText, value
20+
complex_finds_contains EditText, value
2121
end
2222

2323
# Find the first EditText.
@@ -36,14 +36,14 @@ def last_textfield
3636
# @param value [String] the value to match exactly
3737
# @return [EditText]
3838
def textfield_exact value
39-
xpath_visible_exact EditText, value
39+
complex_find_exact EditText, value
4040
end
4141

4242
# Find all EditTexts that exactly match value.
4343
# @param value [String] the value to match exactly
4444
# @return [Array<EditText>]
4545
def textfields_exact value
46-
xpaths_visible_exact EditText, value
46+
complex_finds_exact EditText, value
4747
end
4848
end # module Android
4949
end # module Appium

0 commit comments

Comments
 (0)