Skip to content

Commit 8845a1e

Browse files
Rewrite iOS support for appium 1.0
1 parent b0e0bfd commit 8845a1e

File tree

12 files changed

+132
-331
lines changed

12 files changed

+132
-331
lines changed

ios_tests/lib/ios/specs/common/element/button.rb renamed to ios_tests/lib/ios/specs/ios/element/button.rb

+5-12
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# encoding: utf-8
2-
# rake ios['common/element/button']
3-
describe 'common/element/button' do
2+
# rake ios['ios/element/button']
3+
describe 'ios/element/button' do
44
def before_first
55
screen.must_equal catalog
66
# nav to buttons activity
7-
wait { name('buttons').click }
7+
wait { s_text('buttons').click }
88
end
99

1010
def after_last
@@ -29,8 +29,8 @@ def gray
2929
end
3030

3131
t 'buttons' do
32-
exp = ['Back', 'UINavigationBarBackIndicatorDefault.png', 'Gray', 'Right pointing arrow', 'Rounded', 'Custom Text', 'More info', 'More info', 'More info', 'Add contact']
33-
buttons.must_equal exp
32+
exp = ['Back', 'Gray', 'Right pointing arrow']
33+
buttons('a').map { |e| e.name }.must_equal exp
3434
end
3535

3636
t 'first_button' do
@@ -53,13 +53,6 @@ def gray
5353
e_buttons.length.must_equal 10
5454
end
5555

56-
t 'button_num' do
57-
# select x button containing 'in'
58-
button_num('in', 1).name.must_equal 'UINavigationBarBackIndicatorDefault.png'
59-
button_num('in', 2).name.must_equal 'Right pointing arrow'
60-
button_num('in', 3).name.must_equal 'More info'
61-
end
62-
6356
t 'after_last' do
6457
after_last
6558
end

ios_tests/lib/ios/specs/ios/element/generic.rb

+11-37
Original file line numberDiff line numberDiff line change
@@ -5,58 +5,32 @@ def before_first
55
screen.must_equal catalog
66
end
77

8-
def exp_text
9-
'TextFields, Uses of UITextField'
8+
def uibutton_text
9+
'Buttons, Various uses of UIButton'
1010
end
1111

12-
def search_text
13-
'TextFields'
12+
def verify element
13+
element = element.first if element.is_a? Array
14+
element.name.must_equal uibutton_text
1415
end
1516

1617
t 'before_first' do
1718
before_first
1819
end
1920

2021
t 'find' do
21-
el = find search_text
22-
el_text = el.text
23-
el = el_text.empty? ? el.name : el_text
24-
el.must_equal exp_text
22+
verify find 'tons'
2523
end
2624

2725
t 'finds' do
28-
el = finds(search_text).first
29-
el_text = el.text
30-
el = el_text.empty? ? el.name : el_text
31-
32-
el.must_equal exp_text
33-
end
34-
35-
t 'text' do
36-
el = text 'rows'
37-
el_text = el.text
38-
el = el_text.empty? ? el.name : el_text
39-
40-
el.include?('rows').must_equal true
41-
end
42-
43-
t 'texts' do
44-
el = texts('rows').first
45-
el_text = el.text
46-
el = el_text.empty? ? el.name : el_text
47-
48-
el.include?('rows').must_equal true
49-
end
50-
51-
t 'name' do
52-
name(search_text).name.must_equal exp_text
26+
verify finds 'tons'
5327
end
5428

55-
t 'names' do
56-
names(search_text).first.name.must_equal exp_text
29+
t 'find_exact' do
30+
verify find_exact uibutton_text
5731
end
5832

59-
t 'name_exact' do
60-
name_exact(exp_text).name.must_equal exp_text
33+
t 'finds_exact' do
34+
verify finds_exact uibutton_text
6135
end
6236
end

ios_tests/lib/ios/specs/ios/element/text.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ def before_first
1010
end
1111

1212
t 's_texts' do
13-
exp = ['UICatalog', 'Buttons, Various uses of UIButton', 'Controls, Various uses of UIControl', 'TextFields, Uses of UITextField', 'SearchBar, Use of UISearchBar', 'TextView, Use of UITextField', 'Pickers, Uses of UIDatePicker, UIPickerView', 'Images, Use of UIImageView', 'Web, Use of UIWebView', 'Segments, Various uses of UISegmentedControl', 'Toolbar, Uses of UIToolbar', 'Alerts, Various uses of UIAlertView, UIActionSheet', 'Transitions, Shows UIViewAnimationTransitions']
14-
s_texts_names.must_equal exp
13+
exp = ['Controls, Various uses of UIControl', 'Segments, Various uses of UISegmentedControl']
14+
s_texts('trol').map { |e| e.name }.must_equal exp
1515
end
1616

1717
t 'e_s_texts' do

ios_tests/lib/ios/specs/ios/element/textfield.rb

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ def after_last
1414
end
1515

1616
t 'textfields' do
17-
textfields.include?('<enter text>').must_equal true
18-
textfields.include?('<enter password>').must_equal true
17+
values = textfields('enter').map { |e| e.value }
18+
values.include?('<enter text>').must_equal true
19+
values.include?('<enter password>').must_equal true
1920
end
2021

2122
t 'e_textfields' do
@@ -85,7 +86,7 @@ def must_raise_no_element &block
8586
set_wait 1
8687
# must leave textfield screen for the rest of the tests
8788
leave_textfields
88-
textfields.must_equal []
89+
e_textfields.must_equal []
8990
end
9091

9192
t 'no e_textfields' do

ios_tests/lib/run.rb

+2-20
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,14 @@
1919
a = OpenStruct.new x: 'ok'
2020
raise 'x issue' unless a.x == 'ok'
2121

22-
## common methods
23-
24-
# ios 7 makes clicking the back button fun.
25-
def back_button
26-
result = nil
27-
names('back').each do |b|
28-
if b.displayed?
29-
result = b
30-
break
31-
end
32-
end
33-
result
34-
end
35-
36-
def back_displayed
37-
b = back_button
38-
b ? b.displayed? : false
39-
end
40-
22+
# common methods
4123
def back_click(opts={})
4224
opts ||= {}
4325
search_wait = opts.fetch(:wait, 60 * 1.7)
4426
# iOS may have multiple 'back' buttons
4527
# select the first displayed? back button.
4628
wait(search_wait) do
47-
back_button.click
29+
button_exact('Back').click
4830
end
4931
end
5032

lib/appium_lib/common/helper.rb

+31
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,37 @@ def resolve_id id
365365
@strings_xml[id]
366366
end
367367

368+
# xpath fragment helper
369+
# example: xpath_visible_contains 'UIATextField', text
370+
def xpath_visible_contains element, value
371+
result = []
372+
attributes = %w[name hint label value]
373+
374+
value_up = value.upcase
375+
value_down = value.downcase
376+
377+
attributes.each do |attribute|
378+
result << %Q(contains(translate(@#{attribute},"#{value_up}","#{value_down}"), "#{value_down}"))
379+
end
380+
381+
result = result.join(' or ')
382+
result = %Q(@visible="true" and (#{result}))
383+
"//#{element}[#{result}]"
384+
end
385+
386+
def xpath_visible_exact element, value
387+
result = []
388+
attributes = %w[name hint label value]
389+
390+
attributes.each do |attribute|
391+
result << %Q(@#{attribute}="#{value}")
392+
end
393+
394+
result = result.join(' or ')
395+
result = %Q(@visible="true" and (#{result}))
396+
"//#{element}[#{result}]"
397+
end
398+
368399
# Used to error when finding a single element fails.
369400
def raise_no_element_error
370401
raise Selenium::WebDriver::Error::NoSuchElementError, 'An element could not be located on the page using the given search parameters.'

lib/appium_lib/ios/element/button.rb

+13-47
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,17 @@
33
module Appium
44
module Ios
55
# Find a button by text and optionally number.
6-
# @param text [String, Integer] the text to exactly match. If int then the button at that index is returned.
6+
# @param value [String, Integer] the value to exactly match. If int then the button at that index is returned.
77
# @param number [Integer] the occurrence of the button matching text. Defaults to the first button.
88
# @return [Button] the button found with text and matching number
9-
def button text, number=0
9+
def button value, number=0
1010
# return button at index.
11-
return ele_index :button, text if text.is_a? Numeric
12-
13-
number >= 1 ? button_num(text, number) :
14-
find_ele_by_name_include(:button, text)
11+
return ele_index :button, value if value.is_a? Numeric
12+
xpath(xpath_visible_contains('UIAButton', value))
1513
end
1614

17-
# Get an array of button texts or button elements if text is provided.
18-
# @param text [String] the text to exactly match
19-
# @return [Array<String>, Array<Buttons>] either an array of button texts or an array of button elements if text is provided.
20-
def buttons text=nil
21-
text == nil ? find_eles_attr(:button, :name) :
22-
find_eles_by_name_include(:button, text)
15+
def buttons value
16+
xpaths(xpath_visible_contains('UIAButton', value))
2317
end
2418

2519
# Get the first button element.
@@ -35,51 +29,23 @@ def last_button
3529
end
3630

3731
# Get the first button element that exactly matches text.
38-
# @param text [String] the text to match exactly
32+
# @param value [String] the value to match exactly
3933
# @return [Button]
40-
def button_exact text
41-
find_ele_by_name :button, text
34+
def button_exact value
35+
xpath xpath_visible_exact 'UIAButton', value
4236
end
4337

4438
# Get all button elements that exactly match text.
45-
# @param text [String] the text to match exactly
39+
# @param value [String] the value to match exactly
4640
# @return [Array<Button>]
47-
def buttons_exact text
48-
find_eles_by_name :button, text
41+
def buttons_exact value
42+
xpaths xpath_visible_exact 'UIAButton', value
4943
end
5044

5145
# Get an array of button elements.
5246
# @return [Array<Button>]
5347
def e_buttons
54-
find_eles :button
55-
end
56-
57-
# Expected to be called via button method.
58-
#
59-
# Get the button element exactly matching text and
60-
# occurrence. number=2 means the 2nd occurrence.
61-
#
62-
# find the second Sign In button
63-
#
64-
# b = e_button 'Sign In', 2
65-
#
66-
# Button order will change in iOS vs Android
67-
# so if there's no button found at number then
68-
# return the first button.
69-
#
70-
# @param text [String] the text to match
71-
# @param number [Integer] the button occurance to return. 1 = first button
72-
# @return [Button] the button that matches text and number
73-
def button_num text, number=1
74-
raise 'Number must be >= 1' if number <= 0
75-
number = number - 1 # zero indexed
76-
result = nil
77-
78-
elements = buttons text
79-
elements.size > number ? result = elements[number]
80-
: result = elements.first
81-
82-
result
48+
find_elements :class, 'UIAButton'
8349
end
8450
end # module Common
8551
end # module Appium

0 commit comments

Comments
 (0)