1
0
Fork 0
mirror of https://github.com/teamcapybara/capybara.git synced 2022-11-09 12:08:07 -05:00
teamcapybara--capybara/lib/webcat/session.rb

167 lines
3.9 KiB
Ruby
Raw Normal View History

2009-11-04 17:05:11 -05:00
class Webcat::Session
attr_reader :mode, :app
def initialize(mode, app)
@mode = mode
@app = app
end
def driver
2009-11-04 17:17:17 -05:00
@driver ||= case mode
when :rack_test
Webcat::Driver::RackTest.new(app)
when :culerity
Webcat::Driver::Culerity.new(app)
when :selenium
Webcat::Driver::Selenium.new(app)
2009-11-04 17:17:17 -05:00
else
raise Webcat::DriverNotFoundError, "no driver called #{mode} was found"
end
2009-11-04 17:05:11 -05:00
end
def visit(path)
driver.visit(path)
end
2009-11-04 18:42:13 -05:00
def click_link(locator)
2009-11-11 14:34:36 -05:00
find_link(locator).click
2009-11-04 18:42:13 -05:00
end
2009-11-04 17:05:11 -05:00
2009-11-07 19:13:16 -05:00
def click_button(locator)
2009-11-11 14:34:36 -05:00
find_button(locator).click
2009-11-07 19:13:16 -05:00
end
2009-11-09 17:51:39 -05:00
def fill_in(locator, options={})
find_field(locator, :text_field, :text_area, :password_field).set(options[:with])
2009-11-09 17:51:39 -05:00
end
2009-11-11 15:37:48 -05:00
def choose(locator)
find_field(locator, :radio).set(true)
end
2009-11-11 15:55:20 -05:00
def check(locator)
find_field(locator, :checkbox).set(true)
end
2009-11-11 16:38:55 -05:00
def uncheck(locator)
find_field(locator, :checkbox).set(false)
end
2009-11-11 16:54:02 -05:00
def select(value, options={})
find_field(options[:from], :select).select(value)
end
2009-11-12 11:07:43 -05:00
def attach_file(locator, path)
find_field(locator, :file_field).set(path)
end
2009-11-09 17:51:39 -05:00
2009-11-04 17:05:11 -05:00
def body
2009-11-04 17:17:17 -05:00
driver.body
2009-11-04 17:05:11 -05:00
end
def has_content?(content)
has_xpath?("//*[contains(child::text(),'#{content}')]")
end
def has_xpath?(path, options={})
if options[:count]
find(path).size == options[:count]
else
find(path).size > 0
end
2009-11-14 09:11:29 -05:00
end
2009-11-15 16:54:08 -05:00
def has_css?(path, options={})
has_xpath?(Nokogiri::CSS.xpath_for(path).first, options)
end
2009-11-14 09:11:29 -05:00
def within(scope)
scopes.push(scope)
yield
scopes.pop
end
2009-11-14 10:20:15 -05:00
def save_and_open_page
require 'webcat/save_and_open_page'
Webcat::SaveAndOpenPage.save_and_open_page(body)
end
2009-11-07 12:56:04 -05:00
private
2009-11-14 09:11:29 -05:00
def current_scope
scopes.join('')
end
def scopes
@scopes ||= []
end
2009-11-11 14:34:36 -05:00
def find_link(locator)
2009-11-14 09:46:09 -05:00
link = find_element("//a[@id='#{locator}']", "//a[contains(.,'#{locator}')]", "//a[@title='#{locator}']")
2009-11-15 17:20:54 -05:00
raise Webcat::ElementNotFound, "no link with title, id or text '#{locator}' found" unless link
link
2009-11-11 14:34:36 -05:00
end
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}']"
)
2009-11-15 17:20:54 -05:00
raise Webcat::ElementNotFound, "no button with value or id '#{locator}' found" unless button
button
2009-11-11 14:34:36 -05:00
end
2009-11-10 16:48:31 -05:00
def find_field(locator, *kinds)
field = find_field_by_id(locator, *kinds) || find_field_by_label(locator, *kinds)
raise Webcat::ElementNotFound, "no field of kind #{kinds.inspect} with id or'#{locator}' found" unless field
field
2009-11-10 16:48:31 -05:00
end
FIELDS_PATHS = {
:text_field => proc { |id| "//input[@type='text'][@id='#{id}']" },
:text_area => proc { |id| "//textarea[@id='#{id}']" },
2009-11-11 15:37:48 -05:00
:password_field => proc { |id| "//input[@type='password'][@id='#{id}']" },
:radio => proc { |id| "//input[@type='radio'][@id='#{id}']" },
2009-11-11 15:55:20 -05:00
:hidden_field => proc { |id| "//input[@type='hidden'][@id='#{id}']" },
2009-11-11 16:54:02 -05:00
:checkbox => proc { |id| "//input[@type='checkbox'][@id='#{id}']" },
2009-11-12 11:07:43 -05:00
:select => proc { |id| "//select[@id='#{id}']" },
:file_field => proc { |id| "//input[@type='file'][@id='#{id}']" }
2009-11-10 16:48:31 -05:00
}
def find_field_by_id(locator, *kinds)
kinds.each do |kind|
path = FIELDS_PATHS[kind]
2009-11-14 09:11:29 -05:00
element = find(path.call(locator)).first
2009-11-10 16:48:31 -05:00
return element if element
end
return nil
end
def find_field_by_label(locator, *kinds)
kinds.each do |kind|
2009-11-14 09:46:09 -05:00
label = find("//label[contains(.,'#{locator}')]").first
2009-11-10 16:48:31 -05:00
if label
element = find_field_by_id(label[:for], kind)
return element if element
end
2009-11-09 18:06:51 -05:00
end
2009-11-10 16:48:31 -05:00
return nil
2009-11-09 18:06:51 -05:00
end
2009-11-07 12:56:04 -05:00
def find_element(*locators)
locators.each do |locator|
2009-11-14 09:11:29 -05:00
element = find(locator).first
2009-11-07 12:56:04 -05:00
return element if element
end
return nil
2009-11-07 12:56:04 -05:00
end
2009-11-14 09:11:29 -05:00
def find(locator)
locator = current_scope.to_s + locator
driver.find(locator)
end
2009-11-07 09:36:58 -05:00
end