Class: Capybara::Session
- Inherits:
-
Object
- Object
- Capybara::Session
- 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
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#mode ⇒ Object
readonly
Returns the value of attribute mode.
Instance Method Summary collapse
- #attach_file(locator, path) ⇒ Object
- #body ⇒ Object
- #check(locator) ⇒ Object
- #choose(locator) ⇒ Object
- #click_button(locator) ⇒ Object
- #click_link(locator) ⇒ Object
- #driver ⇒ Object
- #fill_in(locator, options = {}) ⇒ Object
- #find_button(locator) ⇒ Object
- #find_field(locator, *kinds) ⇒ Object (also: #field_labeled)
- #find_link(locator) ⇒ Object
- #has_content?(content) ⇒ Boolean
- #has_css?(path, options = {}) ⇒ Boolean
- #has_xpath?(path, options = {}) ⇒ Boolean
-
#initialize(mode, app) ⇒ Session
constructor
A new instance of Session.
- #save_and_open_page ⇒ Object
- #select(value, options = {}) ⇒ Object
- #uncheck(locator) ⇒ Object
- #visit(path) ⇒ Object
- #within(kind, scope = nil) ⇒ Object
- #within_fieldset(locator) ⇒ Object
- #within_table(locator) ⇒ Object
Constructor Details
#initialize(mode, app) ⇒ Session
Returns a new instance of Session.
26 27 28 29 |
# File 'lib/capybara/session.rb', line 26 def initialize(mode, app) @mode = mode @app = app end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
24 25 26 |
# File 'lib/capybara/session.rb', line 24 def app @app end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
24 25 26 |
# File 'lib/capybara/session.rb', line 24 def mode @mode end |
Instance Method Details
#attach_file(locator, path) ⇒ Object
76 77 78 |
# File 'lib/capybara/session.rb', line 76 def attach_file(locator, path) find_field(locator, :file_field).set(path) end |
#body ⇒ Object
80 81 82 |
# File 'lib/capybara/session.rb', line 80 def body driver.body end |
#check(locator) ⇒ Object
64 65 66 |
# File 'lib/capybara/session.rb', line 64 def check(locator) find_field(locator, :checkbox).set(true) end |
#choose(locator) ⇒ Object
60 61 62 |
# File 'lib/capybara/session.rb', line 60 def choose(locator) find_field(locator, :radio).set(true) end |
#click_button(locator) ⇒ Object
52 53 54 |
# File 'lib/capybara/session.rb', line 52 def (locator) (locator).click end |
#click_link(locator) ⇒ Object
48 49 50 |
# File 'lib/capybara/session.rb', line 48 def click_link(locator) find_link(locator).click end |
#driver ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/capybara/session.rb', line 31 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
56 57 58 |
# File 'lib/capybara/session.rb', line 56 def fill_in(locator, ={}) find_field(locator, :text_field, :text_area, :password_field).set([:with]) end |
#find_button(locator) ⇒ Object
144 145 146 147 148 149 |
# File 'lib/capybara/session.rb', line 144 def (locator) = find("//input[@type='submit' or @type='image'][@id='#{locator}' or @value='#{locator}']").first \ || find("//button[@id='#{locator}' or @value='#{locator}' or contains(.,'#{locator}')]").first raise Capybara::ElementNotFound, "no button with value or id or text '#{locator}' found" unless end |
#find_field(locator, *kinds) ⇒ Object Also known as: field_labeled
130 131 132 133 134 135 |
# File 'lib/capybara/session.rb', line 130 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 |
#find_link(locator) ⇒ Object
138 139 140 141 142 |
# File 'lib/capybara/session.rb', line 138 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
84 85 86 |
# File 'lib/capybara/session.rb', line 84 def has_content?(content) has_xpath?("//*[contains(.,#{sanitized_xpath_string(content)})]") end |
#has_css?(path, options = {}) ⇒ Boolean
100 101 102 |
# File 'lib/capybara/session.rb', line 100 def has_css?(path, ={}) has_xpath?(css_to_xpath(path), ) end |
#has_xpath?(path, options = {}) ⇒ Boolean
88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/capybara/session.rb', line 88 def has_xpath?(path, ={}) results = find(path) if [:text] results = filter_by_text(results, [:text]) end if [:count] results.size == [:count] else results.size > 0 end end |
#save_and_open_page ⇒ Object
125 126 127 128 |
# File 'lib/capybara/session.rb', line 125 def save_and_open_page require 'capybara/save_and_open_page' Capybara::SaveAndOpenPage.save_and_open_page(body) end |
#select(value, options = {}) ⇒ Object
72 73 74 |
# File 'lib/capybara/session.rb', line 72 def select(value, ={}) find_field([:from], :select).select(value) end |
#uncheck(locator) ⇒ Object
68 69 70 |
# File 'lib/capybara/session.rb', line 68 def uncheck(locator) find_field(locator, :checkbox).set(false) end |
#visit(path) ⇒ Object
44 45 46 |
# File 'lib/capybara/session.rb', line 44 def visit(path) driver.visit(path) end |
#within(kind, scope = nil) ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lib/capybara/session.rb', line 104 def within(kind, scope=nil) kind, scope = Capybara.default_selector, 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
113 114 115 116 117 |
# File 'lib/capybara/session.rb', line 113 def within_fieldset(locator) within "//fieldset[@id='#{locator}' or contains(legend,'#{locator}')]" do yield end end |
#within_table(locator) ⇒ Object
119 120 121 122 123 |
# File 'lib/capybara/session.rb', line 119 def within_table(locator) within "//table[@id='#{locator}' or contains(caption,'#{locator}')]" do yield end end |