teamcapybara--capybara/lib/webcat/session.rb

144 lines
3.2 KiB
Ruby
Raw Normal View History

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