Skip to content

Commit cfad029

Browse files
Rewrite iOS textfield helpers
1 parent 98a561b commit cfad029

File tree

2 files changed

+49
-24
lines changed

2 files changed

+49
-24
lines changed

lib/appium_lib/ios/element/textfield.rb

+40-22
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,41 @@ module Ios
66
private
77

88
# @private
9-
def _textfield_visible_string opts={}
10-
index = opts.fetch :index, false
11-
if index
12-
%Q((//#{UIATextField}[@visible="true"])[#{index}] | (//#{UIASecureTextField}[@visible="true"])[#{index}])
13-
else
14-
%Q(//#{UIATextField}[@visible="true"] | //#{UIASecureTextField}[@visible="true"])
15-
end
9+
def _textfield_visible
10+
{
11+
typeArray: [UIATextField, UIASecureTextField],
12+
onlyVisible: true,
13+
}
1614
end
1715

1816
# @private
1917
def _textfield_exact_string value
20-
textfield = string_visible_exact UIATextField, value
21-
secure = string_visible_exact UIASecureTextField, value
22-
"#{textfield} | #{secure}"
18+
exact = {
19+
target: value,
20+
substring: false,
21+
insensitive: false,
22+
}
23+
exact_obj = {
24+
name: exact,
25+
label: exact,
26+
value: exact,
27+
}
28+
_textfield_visible.merge(exact_obj)
2329
end
2430

2531
# @private
2632
def _textfield_contains_string value
27-
textfield = string_visible_contains UIATextField, value
28-
secure = string_visible_contains UIASecureTextField, value
29-
"#{textfield} | #{secure}"
33+
contains = {
34+
target: value,
35+
substring: true,
36+
insensitive: true,
37+
}
38+
contains_obj = {
39+
name: contains,
40+
label: contains,
41+
value: contains,
42+
}
43+
_textfield_visible.merge(contains_obj)
3044
end
3145

3246
public
@@ -40,47 +54,51 @@ def textfield value
4054
# iOS needs to combine textfield and secure to match Android.
4155
if value.is_a? Numeric
4256
index = value
43-
raise "#{index} is not a valid xpath index. Must be >= 1" if index <= 0
57+
raise "#{index} is not a valid index. Must be >= 1" if index <= 0
4458

45-
return xpath _textfield_visible_string index: index
59+
result = eles_by_json(_textfield_visible)[index]
60+
raise _no_such_element if result.nil?
61+
return result
4662
end
4763

48-
xpath _textfield_contains_string value
64+
ele_by_json _textfield_contains_string value
4965
end
5066

5167
# Find all TextFields containing value.
5268
# If value is omitted, all TextFields are returned.
5369
# @param value [String] the value to search for
5470
# @return [Array<TextField>]
5571
def textfields value=false
56-
return xpaths _textfield_visible_string unless value
57-
xpaths _textfield_contains_string value
72+
return eles_by_json _textfield_visible unless value
73+
eles_by_json _textfield_contains_string value
5874
end
5975

6076
# Find the first TextField.
6177
# @return [TextField]
6278
def first_textfield
63-
xpath _textfield_visible_string
79+
ele_by_json _textfield_visible
6480
end
6581

6682
# Find the last TextField.
6783
# @return [TextField]
6884
def last_textfield
69-
xpath _textfield_visible_string index: 'last()'
85+
result = eles_by_json(_textfield_visible).last
86+
raise _no_such_element if result.nil?
87+
result
7088
end
7189

7290
# Find the first TextField that exactly matches value.
7391
# @param value [String] the value to match exactly
7492
# @return [TextField]
7593
def textfield_exact value
76-
xpath _textfield_exact_string value
94+
ele_by_json _textfield_exact_string value
7795
end
7896

7997
# Find all TextFields that exactly match value.
8098
# @param value [String] the value to match exactly
8199
# @return [Array<TextField>]
82100
def textfields_exact value
83-
xpaths _textfield_exact_string value
101+
eles_by_json _textfield_exact_string value
84102
end
85103
end # module Ios
86104
end # module Appium

lib/appium_lib/ios/helper.rb

+9-2
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,11 @@ def _validate_object *objects
447447
raise 'objects must be an array' unless objects.is_a? Array
448448
objects.each do |obj|
449449
next unless obj # obj may be nil. if so, ignore.
450+
451+
valid_keys = [:target, :substring, :insensitive]
452+
unknown_keys = obj.keys - valid_keys
453+
raise "Unknown keys: #{unknown_keys}" unless unknown_keys.empty?
454+
450455
target = obj[:target]
451456
raise 'target must be a string' unless target.is_a? String
452457

@@ -500,7 +505,8 @@ def _by_json opts
500505
onlyVisible = opts[:onlyVisible]
501506
raise 'onlyVisible must be a boolean' unless [true, false].include? onlyVisible
502507

503-
raise 'at least one name, label, or value object must exist' unless opts[:name] || opts[:label] || opts[:value]
508+
# name/label/value are optional. when searching for class only, then none
509+
# will be present.
504510
_validate_object opts[:name], opts[:label], opts[:value]
505511

506512
element_or_elements_by_type = <<-JS
@@ -518,7 +524,6 @@ def _by_json opts
518524
#
519525
# eles_by_json({
520526
# typeArray: ["UIAStaticText"],
521-
# onlyFirst: true,
522527
# onlyVisible: true,
523528
# name: {
524529
# target: "Buttons, Various uses of UIButton",
@@ -527,11 +532,13 @@ def _by_json opts
527532
# },
528533
# })
529534
def eles_by_json opts
535+
opts[:onlyFirst] = false
530536
return _by_json opts
531537
end
532538

533539
# see eles_by_json
534540
def ele_by_json opts
541+
opts[:onlyFirst] = true
535542
result = _by_json(opts).first
536543
raise _no_such_element if result.nil?
537544
result

0 commit comments

Comments
 (0)