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
- #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
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
#app ⇒ Object (readonly)
Returns the value of attribute app.
2 3 4 |
# File 'lib/capybara/session.rb', line 2 def app @app end |
#mode ⇒ Object (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 |
#body ⇒ Object
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 (locator) (locator).click end |
#click_link(locator) ⇒ Object
26 27 28 |
# File 'lib/capybara/session.rb', line 26 def click_link(locator) find_link(locator).click end |
#driver ⇒ Object
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, ={}) find_field(locator, :text_field, :text_area, :password_field).set([:with]) end |
#find_button(locator) ⇒ Object
110 111 112 113 114 115 116 117 118 119 |
# File 'lib/capybara/session.rb', line 110 def (locator) = 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 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 |
#find_link(locator) ⇒ Object
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
62 63 64 |
# File 'lib/capybara/session.rb', line 62 def has_content?(content) has_xpath?("//*[contains(.,'#{content}')]") end |
#has_css?(path, options = {}) ⇒ Boolean
78 79 80 |
# File 'lib/capybara/session.rb', line 78 def has_css?(path, ={}) has_xpath?(css_to_xpath(path), ) end |
#has_xpath?(path, options = {}) ⇒ Boolean
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/capybara/session.rb', line 66 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
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, ={}) find_field([: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 |