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)
|
2009-11-14 09:44:46 +00:00
|
|
|
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={})
|
2009-11-10 22:23:00 +00:00
|
|
|
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:48:33 +00:00
|
|
|
|
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
|
2009-11-13 20:33:40 +00:00
|
|
|
|
|
|
|
def has_content?(content)
|
2009-11-15 12:40:50 +00:00
|
|
|
has_xpath?("//*[contains(child::text(),'#{content}')]")
|
|
|
|
end
|
|
|
|
|
|
|
|
def has_xpath?(path, options={})
|
2009-11-16 19:13:06 +00:00
|
|
|
results = find(path)
|
|
|
|
if options[:text]
|
|
|
|
results = filter_by_text(results, options[:text])
|
|
|
|
end
|
2009-11-15 12:40:50 +00:00
|
|
|
if options[:count]
|
2009-11-16 19:13:06 +00:00
|
|
|
results.size == options[:count]
|
2009-11-15 12:40:50 +00:00
|
|
|
else
|
2009-11-16 19:13:06 +00:00
|
|
|
results.size > 0
|
2009-11-15 12:40:50 +00:00
|
|
|
end
|
2009-11-14 14:11:29 +00:00
|
|
|
end
|
|
|
|
|
2009-11-15 21:54:08 +00:00
|
|
|
def has_css?(path, options={})
|
|
|
|
has_xpath?(Nokogiri::CSS.xpath_for(path).first, options)
|
|
|
|
end
|
|
|
|
|
2009-11-14 14:11:29 +00:00
|
|
|
def within(scope)
|
2009-11-16 19:13:57 +00:00
|
|
|
raise Webcat::ElementNotFound, "scope '#{scope}' not found on page" if find(scope).empty?
|
2009-11-14 14:11:29 +00:00
|
|
|
scopes.push(scope)
|
|
|
|
yield
|
|
|
|
scopes.pop
|
2009-11-13 20:33:40 +00:00
|
|
|
end
|
2009-11-14 15:20:15 +00:00
|
|
|
|
|
|
|
def save_and_open_page
|
|
|
|
require 'webcat/save_and_open_page'
|
|
|
|
Webcat::SaveAndOpenPage.save_and_open_page(body)
|
|
|
|
end
|
2009-11-07 17:56:04 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2009-11-16 19:13:06 +00:00
|
|
|
def filter_by_text(nodes, text)
|
|
|
|
nodes.select do |node|
|
|
|
|
case text
|
|
|
|
when String
|
|
|
|
node.text.include?(text)
|
|
|
|
when Regexp
|
|
|
|
node.text =~ text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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)
|
2009-11-14 14:46:09 +00:00
|
|
|
link = find_element("//a[@id='#{locator}']", "//a[contains(.,'#{locator}')]", "//a[@title='#{locator}']")
|
2009-11-15 22:20:54 +00:00
|
|
|
raise Webcat::ElementNotFound, "no link with title, id or text '#{locator}' found" unless link
|
2009-11-14 14:41:53 +00:00
|
|
|
link
|
2009-11-11 19:34:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def find_button(locator)
|
2009-11-14 14:41:53 +00:00
|
|
|
button = find_element(
|
2009-11-13 09:57:24 +00:00
|
|
|
"//input[@type='submit'][@id='#{locator}']",
|
|
|
|
"//input[@type='submit'][@value='#{locator}']",
|
|
|
|
"//input[@type='image'][@id='#{locator}']",
|
|
|
|
"//input[@type='image'][@value='#{locator}']"
|
|
|
|
)
|
2009-11-15 22:20:54 +00:00
|
|
|
raise Webcat::ElementNotFound, "no button with value or id '#{locator}' found" unless button
|
2009-11-14 14:41:53 +00:00
|
|
|
button
|
2009-11-11 19:34:36 +00:00
|
|
|
end
|
|
|
|
|
2009-11-10 21:48:31 +00:00
|
|
|
def find_field(locator, *kinds)
|
2009-11-14 14:41:53 +00:00
|
|
|
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 21:48:31 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
FIELDS_PATHS = {
|
|
|
|
:text_field => proc { |id| "//input[@type='text'][@id='#{id}']" },
|
2009-11-10 22:23:00 +00:00
|
|
|
:text_area => proc { |id| "//textarea[@id='#{id}']" },
|
2009-11-11 20:37:48 +00:00
|
|
|
:password_field => proc { |id| "//input[@type='password'][@id='#{id}']" },
|
2009-11-11 20:48:33 +00:00
|
|
|
: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:46:09 +00:00
|
|
|
label = find("//label[contains(.,'#{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-14 14:41:53 +00:00
|
|
|
return nil
|
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
|