Skip to content

Commit 27f4ccf

Browse files
Add alert for Android
Make text and name cross platform Button and text now defaults to fuzzy match Add button and text exact match methods Prefix static text element commands with s_ to avoid text conflict Fix page output Rename set_value to type Use tag_name to get the old value of type Fix #8 Fix #9 Fix #10
1 parent 4bb466f commit 27f4ccf

File tree

10 files changed

+201
-119
lines changed

10 files changed

+201
-119
lines changed
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# encoding: utf-8
2+
if $os == :android
3+
4+
# Tap the alert button identified by value.
5+
# @param value [Integer, String] either an integer index of the button or the button's name
6+
# @return [void]
7+
def alert_click value
8+
button(value).click
9+
end
10+
11+
# Get the alert message text.
12+
# @return [String]
13+
def alert_text
14+
get_page
15+
end
16+
17+
# Accept the alert.
18+
# The last button is considered "accept."
19+
# @return [void]
20+
def alert_accept
21+
last_button.click
22+
end
23+
24+
# Get the text of the alert's accept button.
25+
# The last button is considered "accept."
26+
# @return [String]
27+
def alert_accept_text
28+
last_button.text
29+
end
30+
31+
# Dismiss the alert.
32+
# The first button is considered "dismiss."
33+
# @return [void]
34+
def alert_dismiss
35+
first_button.click
36+
end
37+
38+
# Get the text of the alert's dismiss button.
39+
# The first button is considered "dismiss."
40+
# @return [String]
41+
def alert_dismiss_text
42+
first_button.text
43+
end
44+
45+
end # if $os == :android
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# encoding: utf-8
2+
if $os == :android
3+
=begin
4+
name, names, text, text should match substring and case insensitive.
5+
6+
In Android //* is used to find partial case insensitive text matches.
7+
//* is not currently implemented in iOS.
8+
9+
find_element :name by default uses a partial case insensitive match.
10+
On iOS the default is an exact name match.
11+
=end
12+
13+
# Return the first element matching text.
14+
# @param text [String] the text to search for
15+
# @return [Element] the first matching element
16+
def text text
17+
# TODO: Use XPath index once it's implemented
18+
# https://github.com/appium/appium/issues/295
19+
texts(text).first
20+
end
21+
22+
# Return all elements matching text.
23+
# @param text [String] the text to search for
24+
# @return [Array<Element>] all matching elements
25+
def texts text
26+
$driver.find_elements :xpath, "//*[contains(@text, '#{text}')]"
27+
end
28+
29+
# Return the first element matching name.
30+
# on Android name is content description
31+
# on iOS name is the accessibility label or the text.
32+
# @param name [String] the name to search for
33+
# @return [Element] the first matching element
34+
def name name
35+
$driver.find_element :name, name
36+
end
37+
38+
# Return all elements matching name.
39+
# on Android name is content description
40+
# on iOS name is the accessibility label or the text.
41+
# @param name [String] the name to search for
42+
# @return [Array<Element>] all matching elements
43+
def names name
44+
$driver.find_elements :name, name
45+
end
46+
47+
end # if $os == :android

lib/appium_lib/element/android/textfield.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# encoding: utf-8
2-
# UIATextField methods
3-
42
if $os == :android
3+
# UIATextField methods
54

65
# Get an array of textfield texts.
76
# @return [Array<String>]
@@ -42,4 +41,4 @@ def textfield_include text
4241
find_ele_by_text_include :textfield, text
4342
end
4443

45-
end # if $os
44+
end # if $os == :android

lib/appium_lib/element/button.rb

+26-58
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,24 @@
11
# encoding: utf-8
22
# UIAButton methods
33

4-
=begin
5-
Method Signatures:
6-
7-
button( text, number = -1 )
8-
buttons( text = nil )
9-
button_include( text )
10-
buttons_include( text )
11-
first_button
12-
last_button
13-
14-
Examples:
15-
16-
button 'text' # 1st button exactly matching text
17-
button 'text', 2 # 2nd button exactly matching text
18-
19-
buttons # text of all buttons.
20-
buttons 'text' # all buttons exactly matching text
21-
22-
button_include 'text' # the first button that includes text
23-
buttons_include 'text' # all buttons that include text
24-
25-
first_button # the first button
26-
last_button # the last button
27-
=end
28-
294
# Find a button by text and optionally number.
305
# @param text [String, Integer] the text to exactly match. If int then the button at that index is returned.
316
# @param number [Integer] the occurance of the button matching text. Defaults to the first button.
327
# @return [Button] the button found with text and matching number
338
def button text, number=0
9+
# return button at index.
3410
return ele_index :button, text if text.is_a? Numeric
3511

36-
number >= 1 ? button_text_num( text, number ) :
37-
button_text( text )
12+
number >= 1 ? button_num( text, number ) :
13+
find_ele_by_text_include( :button, text )
3814
end
3915

4016
# Get an array of button texts or button elements if text is provided.
4117
# @param text [String] the text to exactly match
4218
# @return [Array<String>, Array<Buttons>] either an array of button texts or an array of button elements if text is provided.
4319
def buttons text=nil
44-
text == nil ? find_eles_attr(:button, :text) :
45-
find_ele_by_text(:button, text)
46-
end
47-
48-
# Get the first button that includes text.
49-
# @param text [String] the text that the element must include
50-
# @return [Button]
51-
def button_include text
52-
find_ele_by_text_include :button, text
53-
end
54-
55-
# Get all buttons that include text.
56-
# @param text [String] the text that the element must include
57-
# @return [Array<Button>]
58-
def buttons_include text
59-
find_eles_by_text_include :button, text
20+
text == nil ? find_eles_attr( :button, :text ) :
21+
find_eles_by_text_include( :button, text )
6022
end
6123

6224
# Get the first button element.
@@ -71,16 +33,28 @@ def last_button
7133
last_ele :button
7234
end
7335

74-
# -- prefer above methods before using these.
75-
private
76-
7736
# Get the first button element that exactly matches text.
7837
# @param text [String] the text to match exactly
7938
# @return [Button]
80-
def button_text text
39+
def button_exact text
8140
find_ele_by_text :button, text
8241
end
8342

43+
# Get all button elements that exactly match text.
44+
# @param text [String] the text to match exactly
45+
# @return [Array<Button>]
46+
def buttons_exact text
47+
find_eles_by_text :button, text
48+
end
49+
50+
# Get an array of button elements.
51+
# @return [Array<Button>]
52+
def e_buttons
53+
find_eles :button
54+
end
55+
56+
# Expected to be called via button method.
57+
#
8458
# Get the button element exactly matching text and
8559
# occurrence. number=2 means the 2nd occurrence.
8660
#
@@ -92,24 +66,18 @@ def button_text text
9266
# so if there's no button found at number then
9367
# return the first button.
9468
#
95-
# @param text [String] the text to match exactly
69+
# @param text [String] the text to match
9670
# @param number [Integer] the button occurance to return. 1 = first button
97-
# @return [Button] the button that exactly matches text and number
98-
def button_text_num text, number=1
71+
# @return [Button] the button that matches text and number
72+
def button_num text, number=1
9973
raise "Number must be >= 1" if number <= 0
10074
number = number - 1 # zero indexed
10175

10276
result = nil
10377

104-
elements = find_eles_by_text :button, text
78+
elements = buttons text
10579
elements.size > number ? result = elements[number]
10680
: result = elements.first
10781

10882
result
109-
end
110-
111-
# Get an array of button elements.
112-
# @return [Array<Button>]
113-
def e_buttons
114-
find_eles :button
115-
end
83+
end

lib/appium_lib/element/alert.rb lib/appium_lib/element/ios/alert.rb

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# encoding: utf-8
2-
2+
if $os == :ios
33
# iOS only
44
# Tap the alert button identified by value.
55
# @param value [Integer, String] either an integer index of the button or the button's name
@@ -44,4 +44,6 @@ def alert_dismiss_text
4444
return if a.nil?
4545
b = a.find_elements(:tag_name, :button)
4646
b.first.text if b && b.size >= 1
47-
end
47+
end
48+
49+
end # if $os == :ios

lib/appium_lib/element/ios/generic.rb

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# encoding: utf-8
2+
if $os == :ios
3+
=begin
4+
name, names, text, text should match substring and case insensitive.
5+
6+
iOS .name() is the accessibility attribute. If not defined, then .label() is used instead.
7+
This differs from Android where name (the content description) is empty when not set.
8+
=end
9+
10+
# Return the first element matching text.
11+
# @param text [String] the text to search for
12+
# @return [Element] the first matching element
13+
def text text
14+
# returnElems requires a wrapped $(element).
15+
js = %Q(
16+
var element = $(au.mainWindow.elements().firstWithPredicate("name contains[c] '#{text}'"));
17+
au._returnElems(element);
18+
)
19+
20+
execute_script(js).first
21+
end
22+
23+
# Return all elements matching text.
24+
# @param text [String] the text to search for
25+
# @return [Array<Element>] all matching elements
26+
def texts text
27+
# returnElems requires a wrapped $(element).
28+
# must call toArray when using withPredicate instead of firstWithPredicate.
29+
js = %Q(
30+
var a = au.mainWindow.elements().withPredicate("name contains[c] '#{text}'").toArray();
31+
au._returnElems($(a));
32+
)
33+
34+
execute_script js
35+
end
36+
37+
# Return the first element matching name.
38+
# on Android name is content description
39+
# on iOS name is the accessibility label or the text.
40+
# @param name [String] the name to search for
41+
# @return [Element] the first matching element
42+
def name name
43+
text name
44+
end
45+
46+
# Return all elements matching name.
47+
# on Android name is content description
48+
# on iOS name is the accessibility label or the text.
49+
# @param name [String] the name to search for
50+
# @return [Array<Element>] all matching elements
51+
def names name
52+
texts name
53+
end
54+
55+
end # if ios

lib/appium_lib/element/ios/textfield.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
# encoding: utf-8
2+
if $os == :ios
23
# UIATextField & UIASecureTextField methods
34
#
45
# Find textfield and then secure elements in one server call
56
# to match Android.
67

7-
if $os == :ios
8-
98
# Get an array of textfield texts.
109
# @return [Array<String>]
1110
def textfields

lib/appium_lib/element/text.rb

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
11
# encoding: utf-8
22
# UIAStaticText methods
33

4+
# s_ prefix for static_text to avoid conflict with generic text methods.
5+
46
# Get an array of text texts.
57
# @return [Array<String>]
6-
def texts
8+
def s_texts
79
find_eles_attr :text, :text
810
end
911

1012
# Get an array of text elements.
1113
# @return [Array<Text>]
12-
def e_texts
14+
def s_e_texts
1315
find_eles :text
1416
end
1517

1618
# Get the first text element.
1719
# @return [Text]
18-
def first_text
20+
def s_first_text
1921
first_ele :text
2022
end
2123

2224
# Get the last text element
2325
# @return [Text]
24-
def last_text
26+
def s_last_text
2527
last_ele :text
2628
end
2729

28-
# Get the first element that matches text.
29-
# @param text [String, Integer] the text to find exactly. If int then the text at that index is returned.
30+
# Get the first element that includes text.
31+
# @param text [String, Integer] the text to find. If int then the text at that index is returned.
3032
# @return [Text]
31-
def text text
33+
def s_text text
3234
return ele_index :text, text if text.is_a? Numeric
33-
find_ele_by_text :text, text
35+
find_ele_by_text_include :text, text
3436
end
3537

36-
# Get the first textfield that includes text.
37-
# @param text [String] the text that the tag must include
38+
# Get the first textfield that matches text.
39+
# @param text [String] the text that the tag must match
3840
# @return [Text]
39-
def text_include text
40-
find_ele_by_text_include :text, text
41+
def s_text_exact text
42+
find_ele_by_text :text, text
4143
end

0 commit comments

Comments
 (0)