Class: Capybara::Session
- Inherits:
-
Object
- Object
- Capybara::Session
- Defined in:
- lib/capybara/session.rb
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
- #all(locator) ⇒ Object
- #attach_file(locator, path) ⇒ Object
- #body ⇒ Object
- #check(locator) ⇒ Object
- #choose(locator) ⇒ Object
- #click(locator) ⇒ Object
- #click_button(locator) ⇒ Object
- #click_link(locator) ⇒ Object
- #drag(source_locator, target_locator) ⇒ Object
- #driver ⇒ Object
- #evaluate_script(script) ⇒ Object
- #fill_in(locator, options = {}) ⇒ Object
- #find(locator) ⇒ Object
- #find_button(locator) ⇒ Object
- #find_field(locator) ⇒ 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
- #wait_for(locator) ⇒ 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.
6 7 8 9 |
# File 'lib/capybara/session.rb', line 6 def initialize(mode, app) @mode = mode @app = app end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
4 5 6 |
# File 'lib/capybara/session.rb', line 4 def app @app end |
#mode ⇒ Object (readonly)
Returns the value of attribute mode.
4 5 6 |
# File 'lib/capybara/session.rb', line 4 def mode @mode end |
Instance Method Details
#all(locator) ⇒ Object
140 141 142 143 144 |
# File 'lib/capybara/session.rb', line 140 def all(locator) XPath.wrap(locator).scope(current_scope).paths.map do |path| driver.find(path) end.flatten end |
#attach_file(locator, path) ⇒ Object
84 85 86 87 88 |
# File 'lib/capybara/session.rb', line 84 def attach_file(locator, path) field = wait_for(XPath.file_field(locator)) raise ::ElementNotFound, "cannot attach file, no file field with id or label '#{locator}' found" unless field field.set(path) end |
#body ⇒ Object
90 91 92 |
# File 'lib/capybara/session.rb', line 90 def body driver.body end |
#check(locator) ⇒ Object
66 67 68 69 70 |
# File 'lib/capybara/session.rb', line 66 def check(locator) field = wait_for(XPath.checkbox(locator)) raise ::ElementNotFound, "cannot check field, no checkbox with id or label '#{locator}' found" unless field field.set(true) end |
#choose(locator) ⇒ Object
60 61 62 63 64 |
# File 'lib/capybara/session.rb', line 60 def choose(locator) field = wait_for(XPath.(locator)) raise ::ElementNotFound, "cannot choose field, no radio button with id or label '#{locator}' found" unless field field.set(true) end |
#click(locator) ⇒ Object
28 29 30 31 32 |
# File 'lib/capybara/session.rb', line 28 def click(locator) link = wait_for(XPath.link(locator).(locator)) raise ::ElementNotFound, "no link or button '#{locator}' found" unless link link.click end |
#click_button(locator) ⇒ Object
40 41 42 43 44 |
# File 'lib/capybara/session.rb', line 40 def (locator) = wait_for(XPath.(locator)) raise ::ElementNotFound, "no button with value or id or text '#{locator}' found" unless .click end |
#click_link(locator) ⇒ Object
34 35 36 37 38 |
# File 'lib/capybara/session.rb', line 34 def click_link(locator) link = wait_for(XPath.link(locator)) raise ::ElementNotFound, "no link with title, id or text '#{locator}' found" unless link link.click end |
#drag(source_locator, target_locator) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/capybara/session.rb', line 46 def drag(source_locator, target_locator) source = find(source_locator) raise ::ElementNotFound, "drag source '#{source_locator}' not found on page" unless source target = find(target_locator) raise ::ElementNotFound, "drag target '#{target_locator}' not found on page" unless target source.drag_to(target) end |
#driver ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/capybara/session.rb', line 11 def driver @driver ||= case mode when :rack_test ::Driver::RackTest.new(app) when :culerity ::Driver::Culerity.new(app) when :selenium ::Driver::Selenium.new(app) else raise ::DriverNotFoundError, "no driver called #{mode} was found" end end |
#evaluate_script(script) ⇒ Object
173 174 175 176 177 178 179 |
# File 'lib/capybara/session.rb', line 173 def evaluate_script(script) begin driver.evaluate_script(script) rescue NoMethodError raise NotSupportedByDriverError end end |
#fill_in(locator, options = {}) ⇒ Object
54 55 56 57 58 |
# File 'lib/capybara/session.rb', line 54 def fill_in(locator, ={}) field = wait_for(XPath.fillable_field(locator)) raise ::ElementNotFound, "cannot fill in, no text field, text area or password field with id or label '#{locator}' found" unless field field.set([:with]) end |
#find(locator) ⇒ Object
146 147 148 |
# File 'lib/capybara/session.rb', line 146 def find(locator) all(locator).first end |
#find_button(locator) ⇒ Object
169 170 171 |
# File 'lib/capybara/session.rb', line 169 def (locator) find(XPath.(locator)) end |
#find_field(locator) ⇒ Object Also known as: field_labeled
160 161 162 |
# File 'lib/capybara/session.rb', line 160 def find_field(locator) find(XPath.field(locator)) end |
#find_link(locator) ⇒ Object
165 166 167 |
# File 'lib/capybara/session.rb', line 165 def find_link(locator) find(XPath.link(locator)) end |
#has_content?(content) ⇒ Boolean
94 95 96 |
# File 'lib/capybara/session.rb', line 94 def has_content?(content) has_xpath?(XPath.content(content).to_s) end |
#has_css?(path, options = {}) ⇒ Boolean
110 111 112 |
# File 'lib/capybara/session.rb', line 110 def has_css?(path, ={}) has_xpath?(css_to_xpath(path), ) end |
#has_xpath?(path, options = {}) ⇒ Boolean
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/capybara/session.rb', line 98 def has_xpath?(path, ={}) results = all(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
135 136 137 138 |
# File 'lib/capybara/session.rb', line 135 def save_and_open_page require 'capybara/save_and_open_page' ::SaveAndOpenPage.save_and_open_page(body) end |
#select(value, options = {}) ⇒ Object
78 79 80 81 82 |
# File 'lib/capybara/session.rb', line 78 def select(value, ={}) field = wait_for(XPath.select([:from])) raise ::ElementNotFound, "cannot select option, no select box with id or label '#{options[:from]}' found" unless field field.select(value) end |
#uncheck(locator) ⇒ Object
72 73 74 75 76 |
# File 'lib/capybara/session.rb', line 72 def uncheck(locator) field = wait_for(XPath.checkbox(locator)) raise ::ElementNotFound, "cannot uncheck field, no checkbox with id or label '#{locator}' found" unless field field.set(false) end |
#visit(path) ⇒ Object
24 25 26 |
# File 'lib/capybara/session.rb', line 24 def visit(path) driver.visit(path) end |
#wait_for(locator) ⇒ Object
150 151 152 153 154 155 156 157 158 |
# File 'lib/capybara/session.rb', line 150 def wait_for(locator) return find(locator) unless driver.wait? 8.times do result = find(locator) return result if result sleep(0.1) end nil end |
#within(kind, scope = nil) ⇒ Object
114 115 116 117 118 119 120 121 |
# File 'lib/capybara/session.rb', line 114 def within(kind, scope=nil) kind, scope = .default_selector, kind unless scope scope = css_to_xpath(scope) if kind == :css raise ::ElementNotFound, "scope '#{scope}' not found on page" unless wait_for(scope) scopes.push(scope) yield scopes.pop end |
#within_fieldset(locator) ⇒ Object
123 124 125 126 127 |
# File 'lib/capybara/session.rb', line 123 def within_fieldset(locator) within XPath.fieldset(locator) do yield end end |
#within_table(locator) ⇒ Object
129 130 131 132 133 |
# File 'lib/capybara/session.rb', line 129 def within_table(locator) within XPath.table(locator) do yield end end |