Class: Capybara::Session

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode, app) ⇒ Session

Returns a new instance of Session.



4
5
6
7
# File 'lib/capybara/session.rb', line 4

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

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



2
3
4
# File 'lib/capybara/session.rb', line 2

def app
  @app
end

#modeObject (readonly)

Returns the value of attribute mode.



2
3
4
# File 'lib/capybara/session.rb', line 2

def mode
  @mode
end

Instance Method Details

#attach_file(locator, path) ⇒ Object



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

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

#bodyObject



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

def body
  driver.body
end

#check(locator) ⇒ Object



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

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

#choose(locator) ⇒ Object



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

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

#click_button(locator) ⇒ Object



30
31
32
# File 'lib/capybara/session.rb', line 30

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


26
27
28
# File 'lib/capybara/session.rb', line 26

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

#driverObject



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/capybara/session.rb', line 9

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



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

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

#find_button(locator) ⇒ Object



110
111
112
113
114
115
116
117
118
119
# File 'lib/capybara/session.rb', line 110

def find_button(locator)
  button = find_element(
    "//input[@type='submit'][@id='#{locator}']",
    "//input[@type='submit'][@value='#{locator}']",
    "//input[@type='image'][@id='#{locator}']",
    "//input[@type='image'][@value='#{locator}']"
  )
  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



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

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


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

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

#has_content?(content) ⇒ Boolean

Returns:

  • (Boolean)


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

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

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

Returns:

  • (Boolean)


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

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

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

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
76
# File 'lib/capybara/session.rb', line 66

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



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

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

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



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

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

#uncheck(locator) ⇒ Object



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

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

#visit(path) ⇒ Object



22
23
24
# File 'lib/capybara/session.rb', line 22

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

#within(kind, scope = nil) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/capybara/session.rb', line 82

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