Skip to content

Commit f08854a

Browse files
Escape single quotes for iOS JS selectors
1 parent b1de4b6 commit f08854a

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

lib/appium_lib/ios/element/generic.rb

+11
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def all_ele_js predicate
5959
# @param text [String] the text to search for
6060
# @return [Element] the first matching element
6161
def find text
62+
text = escape_single_quote text
6263
ele = nil
6364
# prefer value search. this may error with:
6465
# Can't use in/contains operator with collection 1
@@ -81,6 +82,7 @@ def find text
8182
# @param text [String] the text to search for
8283
# @return [Array<Element>] all matching elements
8384
def finds text
85+
text = escape_single_quote text
8486
eles = []
8587
# value contains may error
8688
js = all_ele_js "value contains[c] '#{text}'"
@@ -95,6 +97,7 @@ def finds text
9597
# @param text [String] the text to search for
9698
# @return [Element] the first matching element
9799
def text text
100+
text = escape_single_quote text
98101
js = first_ele_js "value contains[c] '#{text}'"
99102
execute_script js
100103
end
@@ -103,6 +106,7 @@ def text text
103106
# @param text [String] the text to search for
104107
# @return [Array<Element>] all matching elements
105108
def texts text
109+
text = escape_single_quote text
106110
# XPath //* is not implemented on iOS
107111
# https://github.com/appium/appium/issues/430
108112
js = all_ele_js "value contains[c] '#{text}'"
@@ -115,10 +119,12 @@ def texts text
115119
# @param name [String] the name to search for
116120
# @return [Element] the first matching element
117121
def name name
122+
name = escape_single_quote name
118123
mobile :findElementNameContains, name: name
119124
end
120125

121126
def name_exact name
127+
name = escape_single_quote name
122128
js = all_ele_js "name == '#{name}'"
123129
result = execute_script js
124130

@@ -137,9 +143,14 @@ def name_exact name
137143
# @param name [String] the name to search for
138144
# @return [Array<Element>] all matching elements
139145
def names name
146+
name = escape_single_quote name
140147
# :name is not consistent across iOS and Android so use custom JavaScript
141148
# https://github.com/appium/appium/issues/379
142149
js = all_ele_js "name contains[c] '#{name}' || label contains[c] '#{name}'"
143150
execute_script js
144151
end
152+
153+
def escape_single_quote text_to_escape
154+
text_to_escape.gsub("'", '\\' * 4 + "'")
155+
end
145156
end # module Appium::Ios

0 commit comments

Comments
 (0)