Class: Capybara::Session

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

Constant Summary collapse

FIELDS_PATHS =
{
  :text_field => proc { |id| "//input[@type='text'][@id='#{id}']" },
  :text_area => proc { |id| "//textarea[@id='#{id}']" },
  :password_field => proc { |id| "//input[@type='password'][@id='#{id}']" },
  :radio => proc { |id| "//input[@type='radio'][@id='#{id}']" },
  :hidden_field => proc { |id| "//input[@type='hidden'][@id='#{id}']" },
  :checkbox => proc { |id| "//input[@type='checkbox'][@id='#{id}']" },
  :select => proc { |id| "//select[@id='#{id}']" },
  :file_field => proc { |id| "//input[@type='file'][@id='#{id}']" }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode, app) ⇒ Session

Returns a new instance of Session.



16
17
18
19
# File 'lib/capybara/session.rb', line 16

def initialize(mode, app)
  @mode = mode
  @app = app
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



14
15
16
# File 'lib/capybara/session.rb', line 14

def app
  @app
end

#modeObject (readonly)

Returns the value of attribute mode.



14
15
16
# File 'lib/capybara/session.rb', line 14

def mode
  @mode
end

Instance Method Details

#attach_file(locator, path) ⇒ Object



66
67
68
# File 'lib/capybara/session.rb', line 66

def attach_file(locator, path)
  find_field(locator, :file_field).set(path)
end

#bodyObject



70
71
72
# File 'lib/capybara/session.rb', line 70

def body
  driver.body
end

#check(locator) ⇒ Object



54
55
56
# File 'lib/capybara/session.rb', line 54

def check(locator)
  find_field(locator, :checkbox).set(true)
end

#choose(locator) ⇒ Object



50
51
52
# File 'lib/capybara/session.rb', line 50

def choose(locator)
  find_field(locator, :radio).set(true)
end

#click_button(locator) ⇒ Object



42
43
44
# File 'lib/capybara/session.rb', line 42

def click_button(locator)
  find_button(locator).click
end


38
39
40
# File 'lib/capybara/session.rb', line 38

def click_link(locator)
  find_link(locator).click
end

#driverObject



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/capybara/session.rb', line 21

def driver
  @driver ||= case mode
  when :rack_test
    Capybara::Driver::RackTest.new(app)
  when :culerity
    Capybara::Driver::Culerity.new(app)
  when :selenium
    Capybara::Driver::Selenium.new(app)
  else
    raise Capybara::DriverNotFoundError, "no driver called #{mode} was found"
  end
end

#fill_in(locator, options = {}) ⇒ Object



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

def fill_in(locator, options={})
  find_field(locator, :text_field, :text_area, :password_field).set(options[:with])
end

#find_button(locator) ⇒ Object



134
135
136
137
138
# File 'lib/capybara/session.rb', line 134

def find_button(locator)
  button = find("//input[@type='submit' or @type='image'][@id='#{locator}' or @value='#{locator}']").first
  raise Capybara::ElementNotFound, "no button with value or id '#{locator}' found" unless button
  button
end

#find_field(locator, *kinds) ⇒ Object Also known as: field_labeled



120
121
122
123
124
125
# File 'lib/capybara/session.rb', line 120

def find_field(locator, *kinds)
  kinds = FIELDS_PATHS.keys if kinds.empty?
  field = find_field_by_id(locator, *kinds) || find_field_by_label(locator, *kinds)
  raise Capybara::ElementNotFound, "no field of kind #{kinds.inspect} with id or label '#{locator}' found" unless field
  field
end


128
129
130
131
132
# File 'lib/capybara/session.rb', line 128

def find_link(locator)
  link = find("//a[@id='#{locator}' or contains(.,'#{locator}') or @title='#{locator}']").first
  raise Capybara::ElementNotFound, "no link with title, id or text '#{locator}' found" unless link
  link
end

#has_content?(content) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/capybara/session.rb', line 74

def has_content?(content)
  has_xpath?("//*[contains(.,'#{content}')]")
end

#has_css?(path, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/capybara/session.rb', line 90

def has_css?(path, options={})
  has_xpath?(css_to_xpath(path), options)
end

#has_xpath?(path, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
86
87
88
# File 'lib/capybara/session.rb', line 78

def has_xpath?(path, options={})
  results = find(path)
  if options[:text]
    results = filter_by_text(results, options[:text])
  end
  if options[:count]
    results.size == options[:count]
  else
    results.size > 0
  end
end

#save_and_open_pageObject



115
116
117
118
# File 'lib/capybara/session.rb', line 115

def save_and_open_page
  require 'capybara/save_and_open_page'
  Capybara::SaveAndOpenPage.save_and_open_page(body)
end

#select(value, options = {}) ⇒ Object



62
63
64
# File 'lib/capybara/session.rb', line 62

def select(value, options={})
  find_field(options[:from], :select).select(value)
end

#uncheck(locator) ⇒ Object



58
59
60
# File 'lib/capybara/session.rb', line 58

def uncheck(locator)
  find_field(locator, :checkbox).set(false)
end

#visit(path) ⇒ Object



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

def visit(path)
  driver.visit(path)
end

#within(kind, scope = nil) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/capybara/session.rb', line 94

def within(kind, scope=nil)
  kind, scope = :xpath, kind unless scope
  scope = css_to_xpath(scope) if kind == :css
  raise Capybara::ElementNotFound, "scope '#{scope}' not found on page" if find(scope).empty?
  scopes.push(scope)
  yield
  scopes.pop
end

#within_fieldset(locator) ⇒ Object



103
104
105
106
107
# File 'lib/capybara/session.rb', line 103

def within_fieldset(locator)
  within "//fieldset[@id='#{locator}' or contains(legend,'#{locator}')]" do
    yield
  end
end

#within_table(locator) ⇒ Object



109
110
111
112
113
# File 'lib/capybara/session.rb', line 109

def within_table(locator)
  within "//table[@id='#{locator}' or contains(caption,'#{locator}')]" do
    yield
  end
end