Skip to content

Commit

Permalink
added HTML5 <input> types: search, email, url, et al.
Browse files Browse the repository at this point in the history
  • Loading branch information
James A. Rosen committed Aug 12, 2010
1 parent dcd17ac commit 446b79a
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 13 deletions.
97 changes: 97 additions & 0 deletions lib/webrat/core/elements/field.rb
Expand Up @@ -52,6 +52,19 @@ def self.field_class(element)
when "hidden" then HiddenField
when "radio" then RadioField
when "password" then PasswordField
when 'search' then SearchField
when 'email' then EmailField
when 'url' then URLField
when 'tel' then TelephoneField
when 'color' then ColorField
when 'number' then NumberField
when 'range' then RangeField
when 'date' then DateField
when 'month' then MonthField
when 'week' then WeekField
when 'time' then TimeField
when 'datetime' then DateTimeField
when 'datetime-local' then DateTimeLocalField
when "file" then FileField
when "reset" then ResetField
when "submit" then ButtonField
Expand Down Expand Up @@ -367,6 +380,84 @@ def self.xpath_search
[".//input[@type = 'text']", ".//input[not(@type)]"]
end
end

class SearchField < Field #:nodoc:
def self.xpath_search
[".//input[@type = 'search']"]
end
end

class EmailField < Field #:nodoc:
def self.xpath_search
[".//input[@type = 'email']"]
end
end

class URLField < Field #:nodoc:
def self.xpath_search
[".//input[@type = 'url']"]
end
end

class TelephoneField < Field #:nodoc:
def self.xpath_search
[".//input[@type = 'tel']"]
end
end

class ColorField < Field #:nodoc:
def self.xpath_search
[".//input[@type = 'color']"]
end
end

class NumberField < Field #:nodoc:
def self.xpath_search
[".//input[@type = 'number']"]
end
end

class RangeField < Field #:nodoc:
def self.xpath_search
[".//input[@type = 'range']"]
end
end

class DateField < Field #:nodoc:
def self.xpath_search
[".//input[@type = 'date']"]
end
end

class MonthField < Field #:nodoc:
def self.xpath_search
[".//input[@type = 'month']"]
end
end

class WeekField < Field #:nodoc:
def self.xpath_search
[".//input[@type = 'week']"]
end
end

class TimeField < Field #:nodoc:
def self.xpath_search
[".//input[@type = 'time']"]
end
end

class DateTimeField < Field #:nodoc:
def self.xpath_search
[".//input[@type = 'datetime']"]
end
end

class DateTimeLocalField < Field #:nodoc:
def self.xpath_search
[".//input[@type = 'datetime-local']"]
end
end

class ResetField < Field #:nodoc:
def self.xpath_search
Expand Down Expand Up @@ -435,4 +526,10 @@ def default_value

end

TEXTUAL_FIELDS = TextField, TextareaField, PasswordField,
SearchField, EmailField, URLField, TelephoneField,
ColorField, NumberField, RangeField,
DateField, MonthField, WeekField, TimeField,
DateTimeField, DateTimeLocalField

end
2 changes: 1 addition & 1 deletion lib/webrat/core/scope.rb
Expand Up @@ -48,7 +48,7 @@ def initialize(session, &block) #:nodoc:
# <tt>field</tt> can be either the value of a name attribute (i.e. <tt>user[email]</tt>)
# or the text inside a <tt><label></tt> element that points at the <tt><input></tt> field.
def fill_in(field_locator, options = {})
field = locate_field(field_locator, TextField, TextareaField, PasswordField)
field = locate_field(field_locator, *Webrat::TEXTUAL_FIELDS)
field.raise_error_if_disabled
field.set(options[:with])
end
Expand Down
30 changes: 18 additions & 12 deletions spec/public/fill_in_spec.rb
Expand Up @@ -31,18 +31,24 @@
click_button
end

it "should work with password fields" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input id="user_text" name="user[text]" type="password" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "user" => {"text" => "pass"})
fill_in "user_text", :with => "pass"
click_button
[
'password', 'search', 'email', 'url', 'tel', 'color',
'number', 'range', 'date', 'month', 'week', 'time',
'datetime', 'datetime-local'
].each do |type|
it "should work with #{type} fields" do
with_html <<-HTML
<html>
<form method="post" action="/login">
<input id="user_text" name="user[text]" type="#{type}" />
<input type="submit" />
</form>
</html>
HTML
webrat_session.should_receive(:post).with("/login", "user" => {"text" => "some value"})
fill_in "user_text", :with => "some value"
click_button
end
end

it "should fail if input not found" do
Expand Down

0 comments on commit 446b79a

Please sign in to comment.