-
Notifications
You must be signed in to change notification settings - Fork 474
/
form.rb
724 lines (609 loc) · 18.5 KB
/
form.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
require 'mechanize/element_matcher'
# This class encapsulates a form parsed out of an HTML page. Each type of
# input field available in a form can be accessed through this object.
#
# == Examples
#
# Find a form and print out its fields
#
# form = page.forms.first # => Mechanize::Form
# form.fields.each { |f| puts f.name }
#
# Set the input field 'name' to "Aaron"
#
# form['name'] = 'Aaron'
# puts form['name']
class Mechanize::Form
extend Forwardable
extend Mechanize::ElementMatcher
attr_accessor :method, :action, :name
attr_reader :fields, :buttons, :file_uploads, :radiobuttons, :checkboxes
# Content-Type for form data (i.e. application/x-www-form-urlencoded)
attr_accessor :enctype
# Character encoding of form data (i.e. UTF-8)
attr_accessor :encoding
# When true, character encoding errors will never be never raised on form
# submission. Default is false
attr_accessor :ignore_encoding_error
alias :elements :fields
attr_reader :node
alias form_node node # for backward compatibility
attr_reader :page
def initialize(node, mech = nil, page = nil)
@enctype = node['enctype'] || 'application/x-www-form-urlencoded'
@node = node
@action = Mechanize::Util.html_unescape(node['action'])
@method = (node['method'] || 'GET').upcase
@name = node['name']
@clicked_buttons = []
@page = page
@mech = mech
@encoding = node['accept-charset'] || (page && page.encoding) || nil
@ignore_encoding_error = false
parse
end
# Returns whether or not the form contains a field with +field_name+
def has_field?(field_name)
fields.any? { |f| f.name == field_name }
end
alias :has_key? :has_field?
# Returns whether or not the form contains a field with +value+
def has_value?(value)
fields.any? { |f| f.value == value }
end
# Returns all field names (keys) for this form
def keys
fields.map(&:name)
end
# Returns all field values for this form
def values
fields.map(&:value)
end
# Returns all buttons of type Submit
def submits
@submits ||= buttons.select { |f| f.class == Submit }
end
# Returns all buttons of type Reset
def resets
@resets ||= buttons.select { |f| f.class == Reset }
end
# Returns all fields of type Text
def texts
@texts ||= fields.select { |f| f.class == Text }
end
# Returns all fields of type Hidden
def hiddens
@hiddens ||= fields.select { |f| f.class == Hidden }
end
# Returns all fields of type Textarea
def textareas
@textareas ||= fields.select { |f| f.class == Textarea }
end
# Returns all fields of type Keygen
def keygens
@keygens ||= fields.select { |f| f.class == Keygen }
end
# Returns whether or not the form contains a Submit button named +button_name+
def submit_button?(button_name)
submits.find { |f| f.name == button_name }
end
# Returns whether or not the form contains a Reset button named +button_name+
def reset_button?(button_name)
resets.find { |f| f.name == button_name }
end
# Returns whether or not the form contains a Text field named +field_name+
def text_field?(field_name)
texts.find { |f| f.name == field_name }
end
# Returns whether or not the form contains a Hidden field named +field_name+
def hidden_field?(field_name)
hiddens.find { |f| f.name == field_name }
end
# Returns whether or not the form contains a Textarea named +field_name+
def textarea_field?(field_name)
textareas.find { |f| f.name == field_name }
end
# This method is a shortcut to get form's DOM id.
# Common usage:
# page.form_with(:dom_id => "foorm")
# Note that you can also use +:id+ to get to this method:
# page.form_with(:id => "foorm")
def dom_id
@node['id']
end
# This method is a shortcut to get form's DOM class.
# Common usage:
# page.form_with(:dom_class => "foorm")
# Note that you can also use +:class+ to get to this method:
# page.form_with(:class => "foorm")
# However, attribute values are compared literally as string, so
# form_with(class: "a") does not match a form with class="a b".
# Use form_with(css: "form.a") instead.
def dom_class
@node['class']
end
##
# :method: search
#
# Shorthand for +node.search+.
#
# See Nokogiri::XML::Node#search for details.
##
# :method: css
#
# Shorthand for +node.css+.
#
# See also Nokogiri::XML::Node#css for details.
##
# :method: xpath
#
# Shorthand for +node.xpath+.
#
# See also Nokogiri::XML::Node#xpath for details.
##
# :method: at
#
# Shorthand for +node.at+.
#
# See also Nokogiri::XML::Node#at for details.
##
# :method: at_css
#
# Shorthand for +node.at_css+.
#
# See also Nokogiri::XML::Node#at_css for details.
##
# :method: at_xpath
#
# Shorthand for +node.at_xpath+.
#
# See also Nokogiri::XML::Node#at_xpath for details.
def_delegators :node, :search, :css, :xpath, :at, :at_css, :at_xpath
# Add a field with +field_name+ and +value+
def add_field!(field_name, value = nil)
fields << Field.new({'name' => field_name}, value)
end
##
# This method sets multiple fields on the form. It takes a list of +fields+
# which are name, value pairs.
#
# If there is more than one field found with the same name, this method will
# set the first one found. If you want to set the value of a duplicate
# field, use a value which is a Hash with the key as the index in to the
# form. The index is zero based.
#
# For example, to set the second field named 'foo', you could do the
# following:
#
# form.set_fields :foo => { 1 => 'bar' }
def set_fields fields = {}
fields.each do |name, v|
case v
when Hash
v.each do |index, value|
self.fields_with(:name => name.to_s)[index].value = value
end
else
value = nil
index = 0
[v].flatten.each do |val|
index = val.to_i if value
value = val unless value
end
self.fields_with(:name => name.to_s)[index].value = value
end
end
end
# Fetch the value of the first input field with the name passed in. Example:
# puts form['name']
def [](field_name)
f = field(field_name)
f && f.value
end
# Set the value of the first input field with the name passed in. Example:
# form['name'] = 'Aaron'
def []=(field_name, value)
f = field(field_name)
if f
f.value = value
else
add_field!(field_name, value)
end
end
# Treat form fields like accessors.
def method_missing(meth, *args)
(method = meth.to_s).chomp!('=')
if field(method)
return field(method).value if args.empty?
return field(method).value = args[0]
end
super
end
# Submit the form. Does not include the +button+ as a form parameter.
# Use +click_button+ or provide button as a parameter.
def submit button = nil, headers = {}
@mech.submit(self, button, headers)
end
# Submit form using +button+. Defaults
# to the first button.
def click_button(button = buttons.first)
submit(button)
end
# This method is sub-method of build_query.
# It converts charset of query value of fields into expected one.
def proc_query(field)
return unless field.query_value
field.query_value.map{|(name, val)|
[from_native_charset(name), from_native_charset(val.to_s)]
}
end
private :proc_query
def from_native_charset str
Mechanize::Util.from_native_charset(str, encoding, @ignore_encoding_error,
@mech && @mech.log)
end
private :from_native_charset
# This method builds an array of arrays that represent the query
# parameters to be used with this form. The return value can then
# be used to create a query string for this form.
def build_query(buttons = [])
query = []
@mech.log.info("form encoding: #{encoding}") if @mech && @mech.log
save_hash_field_order
successful_controls = []
(fields + checkboxes).reject do |f|
f.node["disabled"] || f.node["name"] == ""
end.sort.each do |f|
case f
when Mechanize::Form::CheckBox
if f.checked
successful_controls << f
end
when Mechanize::Form::Field
successful_controls << f
end
end
radio_groups = {}
radiobuttons.each do |f|
fname = from_native_charset(f.name)
radio_groups[fname] ||= []
radio_groups[fname] << f
end
# take one radio button from each group
radio_groups.each_value do |g|
checked = g.select(&:checked)
if checked.uniq.size > 1 then
values = checked.map(&:value).join(', ').inspect
name = checked.first.name.inspect
raise Mechanize::Error,
"radiobuttons #{values} are checked in the #{name} group, " \
"only one is allowed"
else
successful_controls << checked.first unless checked.empty?
end
end
@clicked_buttons.each { |b|
successful_controls << b
}
successful_controls.sort.each do |ctrl| # DOM order
qval = proc_query(ctrl)
query.push(*qval)
end
query
end
# This method adds an index to all fields that have Hash nodes. This
# enables field sorting to maintain order.
def save_hash_field_order
index = 0
fields.each do |field|
if Hash === field.node
field.index = index
index += 1
end
end
end
# This method adds a button to the query. If the form needs to be
# submitted with multiple buttons, pass each button to this method.
def add_button_to_query(button)
unless button.node.document == @node.document then
message =
"#{button.inspect} does not belong to the same page as " \
"the form #{@name.inspect} in #{@page.uri}"
raise ArgumentError, message
end
@clicked_buttons << button
end
# This method allows the same form to be submitted second time
# with the different submit button being clicked.
def reset
# In the future, should add more functionality here to reset the form values to their defaults.
@clicked_buttons = []
end
CRLF = "\r\n".freeze
# This method calculates the request data to be sent back to the server
# for this form, depending on if this is a regular post, get, or a
# multi-part post,
def request_data
query_params = build_query()
case @enctype.downcase
when /^multipart\/form-data/
boundary = rand_string(20)
@enctype = "multipart/form-data; boundary=#{boundary}"
delimiter = "--#{boundary}\r\n"
data = ::String.new
query_params.each do |k,v|
if k
data << delimiter
param_to_multipart(k, v, data)
end
end
@file_uploads.each do |f|
data << delimiter
file_to_multipart(f, data)
end
data << "--#{boundary}--\r\n"
else
Mechanize::Util.build_query_string(query_params)
end
end
# Removes all fields with name +field_name+.
def delete_field!(field_name)
@fields.delete_if{ |f| f.name == field_name}
end
##
# :method: field_with(criteria)
#
# Find one field that matches +criteria+
# Example:
# form.field_with(:id => "exact_field_id").value = 'hello'
##
# :method: field_with!(criteria)
#
# Same as +field_with+ but raises an ElementNotFoundError if no field matches
# +criteria+
##
# :method: fields_with(criteria)
#
# Find all fields that match +criteria+
# Example:
# form.fields_with(:value => /foo/).each do |field|
# field.value = 'hello!'
# end
elements_with :field
##
# :method: button_with(criteria)
#
# Find one button that matches +criteria+
# Example:
# form.button_with(:value => /submit/).value = 'hello'
##
# :method: button_with!(criteria)
#
# Same as +button_with+ but raises an ElementNotFoundError if no button
# matches +criteria+
##
# :method: buttons_with(criteria)
#
# Find all buttons that match +criteria+
# Example:
# form.buttons_with(:value => /submit/).each do |button|
# button.value = 'hello!'
# end
elements_with :button
##
# :method: file_upload_with(criteria)
#
# Find one file upload field that matches +criteria+
# Example:
# form.file_upload_with(:file_name => /picture/).value = 'foo'
##
# :mehtod: file_upload_with!(criteria)
#
# Same as +file_upload_with+ but raises an ElementNotFoundError if no button
# matches +criteria+
##
# :method: file_uploads_with(criteria)
#
# Find all file upload fields that match +criteria+
# Example:
# form.file_uploads_with(:file_name => /picutre/).each do |field|
# field.value = 'foo!'
# end
elements_with :file_upload
##
# :method: radiobutton_with(criteria)
#
# Find one radio button that matches +criteria+
# Example:
# form.radiobutton_with(:name => /woo/).check
##
# :mehtod: radiobutton_with!(criteria)
#
# Same as +radiobutton_with+ but raises an ElementNotFoundError if no button
# matches +criteria+
##
# :method: radiobuttons_with(criteria)
#
# Find all radio buttons that match +criteria+
# Example:
# form.radiobuttons_with(:name => /woo/).each do |field|
# field.check
# end
elements_with :radiobutton
##
# :method: checkbox_with(criteria)
#
# Find one checkbox that matches +criteria+
# Example:
# form.checkbox_with(:name => /woo/).check
##
# :mehtod: checkbox_with!(criteria)
#
# Same as +checkbox_with+ but raises an ElementNotFoundError if no button
# matches +criteria+
##
# :method: checkboxes_with(criteria)
#
# Find all checkboxes that match +criteria+
# Example:
# form.checkboxes_with(:name => /woo/).each do |field|
# field.check
# end
elements_with :checkbox, :checkboxes
def pretty_print(q) # :nodoc:
q.object_group(self) {
q.breakable; q.group(1, '{name', '}') { q.breakable; q.pp name }
q.breakable; q.group(1, '{method', '}') { q.breakable; q.pp method }
q.breakable; q.group(1, '{action', '}') { q.breakable; q.pp action }
q.breakable; q.group(1, '{fields', '}') {
fields.each do |field|
q.breakable
q.pp field
end
}
q.breakable; q.group(1, '{radiobuttons', '}') {
radiobuttons.each { |b| q.breakable; q.pp b }
}
q.breakable; q.group(1, '{checkboxes', '}') {
checkboxes.each { |b| q.breakable; q.pp b }
}
q.breakable; q.group(1, '{file_uploads', '}') {
file_uploads.each { |b| q.breakable; q.pp b }
}
q.breakable; q.group(1, '{buttons', '}') {
buttons.each { |b| q.breakable; q.pp b }
}
}
end
alias inspect pretty_inspect # :nodoc:
private
def parse
@fields = []
@buttons = []
@file_uploads = []
@radiobuttons = []
@checkboxes = []
# Find all input tags
@node.search('input').each do |node|
type = (node['type'] || 'text').downcase
name = node['name']
next if name.nil? && !%w[submit button image].include?(type)
case type
when 'radio'
@radiobuttons << RadioButton.new(node, self)
when 'checkbox'
@checkboxes << CheckBox.new(node, self)
when 'file'
@file_uploads << FileUpload.new(node, nil)
when 'submit'
@buttons << Submit.new(node)
when 'button'
@buttons << Button.new(node)
when 'reset'
@buttons << Reset.new(node)
when 'image'
@buttons << ImageButton.new(node)
when 'hidden'
@fields << Hidden.new(node, node['value'] || '')
when 'text'
@fields << Text.new(node, node['value'] || '')
when 'textarea'
@fields << Textarea.new(node, node['value'] || '')
else
@fields << Field.new(node, node['value'] || '')
end
end
# Find all textarea tags
@node.search('textarea').each do |node|
next unless node['name']
@fields << Textarea.new(node, node.inner_text)
end
# Find all select tags
@node.search('select').each do |node|
next unless node['name']
if node.has_attribute? 'multiple'
@fields << MultiSelectList.new(node)
else
@fields << SelectList.new(node)
end
end
# Find all submit button tags
# FIXME: what can I do with the reset buttons?
@node.search('button').each do |node|
type = (node['type'] || 'submit').downcase
next if type == 'reset'
@buttons << Button.new(node)
end
# Find all keygen tags
@node.search('keygen').each do |node|
@fields << Keygen.new(node, node['value'] || '')
end
end
unless ::String.method_defined?(:b)
# Define String#b for Ruby < 2.0
class ::String
def b
dup.force_encoding(Encoding::ASCII_8BIT)
end
end
end
def rand_string(len = 10)
chars = ("a".."z").to_a + ("A".."Z").to_a
string = ::String.new
1.upto(len) { |i| string << chars[rand(chars.size-1)] }
string
end
def mime_value_quote(str)
str.b.gsub(/(["\r\\])/, '\\\\\1')
end
def param_to_multipart(name, value, buf = ::String.new)
buf <<
"Content-Disposition: form-data; name=\"".freeze <<
mime_value_quote(name) <<
"\"\r\n\r\n".freeze <<
value.b <<
CRLF
end
def file_to_multipart(file, buf = ::String.new)
file_name = file.file_name ? ::File.basename(file.file_name) : ''
body = buf <<
"Content-Disposition: form-data; name=\"".freeze <<
mime_value_quote(file.name) <<
"\"; filename=\"".freeze <<
mime_value_quote(file_name) <<
"\"\r\nContent-Transfer-Encoding: binary\r\n".freeze
if file.file_data.nil? and file.file_name
file.file_data = File.binread(file.file_name)
file.mime_type =
WEBrick::HTTPUtils.mime_type(file.file_name,
WEBrick::HTTPUtils::DefaultMimeTypes)
end
if file.mime_type
body << "Content-Type: ".freeze << file.mime_type << CRLF
end
body << CRLF
if file_data = file.file_data
if file_data.respond_to? :read
body << file_data.read.force_encoding(Encoding::ASCII_8BIT)
else
body << file_data.b
end
end
body << CRLF
end
end
require 'mechanize/form/field'
require 'mechanize/form/button'
require 'mechanize/form/hidden'
require 'mechanize/form/text'
require 'mechanize/form/textarea'
require 'mechanize/form/submit'
require 'mechanize/form/reset'
require 'mechanize/form/file_upload'
require 'mechanize/form/keygen'
require 'mechanize/form/image_button'
require 'mechanize/form/multi_select_list'
require 'mechanize/form/option'
require 'mechanize/form/radio_button'
require 'mechanize/form/check_box'
require 'mechanize/form/select_list'