Class: Spec::Matchers::FormMatcher::HaveForm

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_form_matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action = nil) ⇒ HaveForm

Returns a new instance of HaveForm.



7
8
9
10
11
12
13
14
15
# File 'lib/rspec_form_matcher.rb', line 7

def initialize(action = nil)
  case action
    when Hash then action.each { |m, a| @method, @action = m, a }
    when String, Regexp then @action = action
  end
      
  @tags   = []
  @checks = []
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



5
6
7
# File 'lib/rspec_form_matcher.rb', line 5

def action
  @action
end

#fieldObject (readonly)

Returns the value of attribute field.



5
6
7
# File 'lib/rspec_form_matcher.rb', line 5

def field
  @field
end

#methodObject (readonly)

Returns the value of attribute method.



5
6
7
# File 'lib/rspec_form_matcher.rb', line 5

def method
  @method
end

#submit_buttonObject (readonly)

Returns the value of attribute submit_button.



5
6
7
# File 'lib/rspec_form_matcher.rb', line 5

def submit_button
  @submit_button
end

Instance Method Details

#checkbox(field_name, *values) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/rspec_form_matcher.rb', line 63

def checkbox(field_name, *values)
  if values.empty?
    input(:checkbox, field_name)
  else
    values.each { |v| input(:checkbox, field_name, v) }
  end
  return self
end

#failure_messageObject



78
79
80
# File 'lib/rspec_form_matcher.rb', line 78

def failure_message
  "Specified form (XPath: << #{@xpath} >>) not found in document: \n#{@document.to_s}"
end

#hidden(field_name, value = nil) ⇒ Object



34
35
36
# File 'lib/rspec_form_matcher.rb', line 34

def hidden(field_name, value = nil)
  input(:hidden, field_name, value)
end

#input(type, field_name, value = nil) ⇒ Object



23
24
25
26
27
# File 'lib/rspec_form_matcher.rb', line 23

def input(type, field_name, value = nil)
  attributes = { :type => type, :name => field_name }
  attributes[:value] = value if value
  tag('input', attributes)
end

#matches?(stringlike) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
# File 'lib/rspec_form_matcher.rb', line 72

def matches?(stringlike)
  @document = document(stringlike)
  @xpath    = build_xpath_query
  check_xpath!
end

#negative_failure_messageObject



82
83
84
# File 'lib/rspec_form_matcher.rb', line 82

def negative_failure_message
  "Unexpectedly found specified form (XPath: << #{@xpath} >>) in document: \n#{@document.to_s}"
end

#password(field_name, value = nil) ⇒ Object



59
60
61
# File 'lib/rspec_form_matcher.rb', line 59

def password(field_name, value = nil)
  input(:password, field_name, value)
end

#radio(field_name, *values) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/rspec_form_matcher.rb', line 50

def radio(field_name, *values)
  if values.empty?
    input(:radio, field_name)
  else
    values.each { |v| input(:radio, field_name, v) }
  end
  return self
end

#tag(tag, attributes = {}) ⇒ Object



29
30
31
32
# File 'lib/rspec_form_matcher.rb', line 29

def tag(tag, attributes = {})
  @checks << tag_xpath(tag, attributes)
  return self
end

#text(field_name, value = nil) ⇒ Object



46
47
48
# File 'lib/rspec_form_matcher.rb', line 46

def text(field_name, value = nil)
  input(:text, field_name, value)      
end

#with(method, *args) ⇒ Object Also known as: and_with



17
18
19
# File 'lib/rspec_form_matcher.rb', line 17

def with(method, *args)
  self.send(method, *args)
end