53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# File 'lib/insite/methods/common_methods.rb', line 53
def update_object(hash_args = {})
rescues = [
Watir::Exception::ObjectDisabledException,
Watir::Exception::UnknownObjectException,
Selenium::WebDriver::Error::ElementNotVisibleError,
Selenium::WebDriver::Error::UnknownError
]
failed = []
hash_args.each do |k, v|
sleep 0.2
begin
elem = public_send(k)
if elem.is_a?(Insite::Element)
if [Insite::FileField, Insite::TextField, Insite::TextArea].include? elem.class
elem.set v
elsif [Insite::Select].include? elem.class
elem.select v
elsif [Insite::Anchor, Insite::Button].include? elem.class
case v
when Symbol
elem.public_send v
when TrueClass
elem.click
when FalseClass
else
raise ArgumentError, "Unsupported argument for #{elem.class}: '#{v}'"
end
elsif elem.is_a?(Insite::Radio)
case v
when Symbol
elem.public_send v
when TrueClass
3.times do
elem.set
break if elem.set?
sleep 0.5
end
when FalseClass
raise ArgumentError, "Unsupported argument for #{elem.class}: '#{v}' \
(You can only set a radio button, so false is not a valid argument.)"
else
raise ArgumentError, "Unsupported argument for #{elem.class}: '#{v}'"
end
elsif elem.is_a?(Insite::CheckBox)
case v
when Symbol
elem.public_send v
when TrueClass
3.times do
elem.set
break if elem.set?
sleep 0.5
end
when FalseClass
3.times do
elem.clear
break if !elem.set?
sleep 0.5
end
elem.clear
else
raise ArgumentError, "Unsupported argument for #{elem.class}: '#{v}'"
end
else
case v
when Symbol
elem.public_send v
when TrueClass
elem.set
when FalseClass
elem.clear
else
raise ArgumentError, "Unsupported argument for #{elem.class}: '#{v}'"
end
end
elsif elem.is_a?(Component)
w = public_send(k)
begin
if w.is_a?(Component) && v.is_a?(Hash)
w.update_object(**v)
elsif w.is_a?(Component) && w.respond_to?(:update)
w.update(v)
end
rescue => e
begin
if v.is_a?(Array)
public_send(k).update(v)
else
public_send(k).update(*v)
end
rescue => e2
raise ArgumentError, "Dynamic method call failed for #{k}.", e2.backtrace.join("\n")
end
end
else
begin
if v.is_a?(Array)
public_send(k, *v)
else
public_send(k, v)
end
rescue => e
begin
if v.is_a?(Array)
public_send(k).update(v)
else
public_send(k).update(*v)
end
rescue => e2
raise(
ArgumentError,
"Dynamic method call failed for #{k}: #{e2.try(:message)}",
e2.backtrace
)
end
end
end
rescue => e
if rescues.any? { |err| e.is_a?(err) }
unless failed.include?(k)
puts "Rescued #{e.class} when trying to update #{k}. Sleeping 5 seconds and then trying again."
failed << k
sleep 5
redo
end
else
raise e, "Failure trying to update #{k} with #{v.class}: #{v}. Original message: #{e.try(:message)}\n" + e.backtrace.join("\n")
end
end
sleep 0.2
end
sleep 1
hash_args
end
|