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.
16 17 18 19 |
# File 'lib/capybara/session.rb', line 16 def initialize(mode, app) @mode = mode @app = app end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
14 15 16 |
# File 'lib/capybara/session.rb', line 14 def app @app end |
#mode ⇒ Object (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 |
#body ⇒ Object
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 (locator) (locator).click end |
#click_link(locator) ⇒ Object
38 39 40 |
# File 'lib/capybara/session.rb', line 38 def click_link(locator) find_link(locator).click end |
#driver ⇒ Object
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, ={}) find_field(locator, :text_field, :text_area, :password_field).set([:with]) end |
#find_button(locator) ⇒ Object
134 135 136 137 138 |
# File 'lib/capybara/session.rb', line 134 def (locator) = 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 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 |
#find_link(locator) ⇒ Object
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
74 75 76 |
# File 'lib/capybara/session.rb', line 74 def has_content?(content) has_xpath?("//*[contains(.,'#{content}')]") end |
#has_css?(path, options = {}) ⇒ Boolean
90 91 92 |
# File 'lib/capybara/session.rb', line 90 def has_css?(path, ={}) has_xpath?(css_to_xpath(path), ) end |
#has_xpath?(path, options = {}) ⇒ Boolean
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/capybara/session.rb', line 78 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
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, ={}) find_field([: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 |