Module: Webrat

Defined in:
lib/webrat/core/xml.rb,
lib/webrat.rb,
lib/webrat/rack.rb,
lib/webrat/rails.rb,
lib/webrat/sinatra.rb,
lib/webrat/selenium.rb,
lib/webrat/core/mime.rb,
lib/webrat/mechanize.rb,
lib/webrat/core/scope.rb,
lib/webrat/core/logging.rb,
lib/webrat/core/methods.rb,
lib/webrat/core/session.rb,
lib/webrat/merb_session.rb,
lib/webrat/core/locators.rb,
lib/webrat/core/xml/rexml.rb,
lib/webrat/core/xml/hpricot.rb,
lib/webrat/core/xml/nokogiri.rb,
lib/webrat/core/configuration.rb,
lib/webrat/core/elements/area.rb,
lib/webrat/core/elements/form.rb,
lib/webrat/core/elements/link.rb,
lib/webrat/core/elements/field.rb,
lib/webrat/core/elements/label.rb,
lib/webrat/core/elements/element.rb,
lib/webrat/core/locators/locator.rb,
lib/webrat/core/matchers/have_tag.rb,
lib/webrat/core/save_and_open_page.rb,
lib/webrat/core/matchers/have_xpath.rb,
lib/webrat/selenium/selenium_session.rb,
lib/webrat/core/locators/area_locator.rb,
lib/webrat/core/locators/form_locator.rb,
lib/webrat/core/locators/link_locator.rb,
lib/webrat/core/matchers/have_content.rb,
lib/webrat/core/elements/select_option.rb,
lib/webrat/core/locators/field_locator.rb,
lib/webrat/core/locators/label_locator.rb,
lib/webrat/core/matchers/have_selector.rb,
lib/webrat/core/locators/button_locator.rb,
lib/webrat/core/locators/field_by_id_locator.rb,
lib/webrat/core/locators/field_named_locator.rb,
lib/webrat/core/locators/field_labeled_locator.rb,
lib/webrat/core/locators/select_option_locator.rb

Overview

HashWithIndifferentAccess = Mash

Defined Under Namespace

Modules: HaveTagMatcher, Locators, Logging, MIME, Matchers, Methods, SaveAndOpenPage, Selenium, XML Classes: Area, ButtonField, CheckboxField, Configuration, DisabledFieldError, Element, Field, FileField, Form, HiddenField, InfiniteRedirectError, Label, Link, MechanizeSession, MerbSession, NotFoundError, PageLoadError, PasswordField, RackSession, RadioField, RailsSession, ResetField, Scope, SelectField, SelectOption, SeleniumResponse, SeleniumSession, Session, SinatraSession, TextField, TextareaField, TimeoutError, WebratError

Constant Summary collapse

VERSION =
'0.4.2'

Class Method Summary collapse

Class Method Details

.configurationObject

:nodoc:



12
13
14
# File 'lib/webrat/core/configuration.rb', line 12

def self.configuration # :nodoc:
  @@configuration ||= Webrat::Configuration.new
end

.configure(configuration = Webrat.configuration) {|configuration| ... } ⇒ Object

Configures Webrat. If this is not done, Webrat will be created with all of the default settings.

Yields:



7
8
9
10
# File 'lib/webrat/core/configuration.rb', line 7

def self.configure(configuration = Webrat.configuration)
  yield configuration if block_given?
  @@configuration = configuration
end

.define_dom_method(object, dom) ⇒ Object

:nodoc:



53
54
55
56
57
# File 'lib/webrat/core/xml/nokogiri.rb', line 53

def self.define_dom_method(object, dom) #:nodoc:
  object.meta_class.send(:define_method, :dom) do
    dom
  end
end

.hpricot_document(stringlike) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/webrat/core/xml/hpricot.rb', line 3

def self.hpricot_document(stringlike)
  return stringlike.dom if stringlike.respond_to?(:dom)

  if Hpricot::Doc === stringlike
    stringlike
  elsif Hpricot::Elements === stringlike
    stringlike
  elsif StringIO === stringlike
    Hpricot(stringlike.string)
  elsif stringlike.respond_to?(:body)
    Hpricot(stringlike.body.to_s)
  else
    Hpricot(stringlike.to_s)
  end
end

.html_nokogiri_document(stringlike) ⇒ Object

:nodoc:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/webrat/core/xml/nokogiri.rb', line 21

def self.html_nokogiri_document(stringlike) #:nodoc:
  return stringlike.dom if stringlike.respond_to?(:dom)
  
  if Nokogiri::HTML::Document === stringlike
    stringlike
  elsif Nokogiri::XML::NodeSet === stringlike
    stringlike
  elsif StringIO === stringlike
    Nokogiri::HTML(stringlike.string)
  elsif stringlike.respond_to?(:body)
    Nokogiri::HTML(stringlike.body.to_s)
  else
    Nokogiri::HTML(stringlike.to_s)
  end
end

.nokogiri_document(stringlike) ⇒ Object

:nodoc:



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/webrat/core/xml/nokogiri.rb', line 5

def self.nokogiri_document(stringlike) #:nodoc:
  return stringlike.dom if stringlike.respond_to?(:dom)
  
  if Nokogiri::HTML::Document === stringlike
    stringlike
  elsif Nokogiri::XML::NodeSet === stringlike
    stringlike
  elsif StringIO === stringlike
    Nokogiri::HTML(stringlike.string)
  elsif stringlike.respond_to?(:body)
    Nokogiri::HTML(stringlike.body.to_s)
  else
    Nokogiri::HTML(stringlike.to_s)
  end
end

.on_java?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/webrat.rb', line 26

def self.on_java?
  RUBY_PLATFORM =~ /java/
end

.pid_fileObject



28
29
30
31
32
33
34
# File 'lib/webrat/selenium.rb', line 28

def self.pid_file
  if File.exists?('config.ru')
    prepare_pid_file(Dir.pwd, 'rack.pid')
  else
    prepare_pid_file("#{RAILS_ROOT}/tmp/pids", "mongrel_selenium.pid")
  end
end

.prepare_pid_file(file_path, pid_file_name) ⇒ Object



79
80
81
82
# File 'lib/webrat/selenium.rb', line 79

def self.prepare_pid_file(file_path, pid_file_name)
  FileUtils.mkdir_p File.expand_path(file_path)
  File.expand_path("#{file_path}/#{pid_file_name}")
end

.require_xmlObject



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/webrat.rb', line 12

def self.require_xml
  gem "nokogiri", ">= 1.0.6"
  
  if on_java?
    # We need Nokogiri's CSS to XPath support, even if using REXML and Hpricot for parsing and searching
    require "nokogiri/css"
    require "hpricot"
    require "rexml/document"
  else
    require "nokogiri"
    require "webrat/core/xml/nokogiri"
  end
end

.rexml_document(stringlike) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/webrat/core/xml/rexml.rb', line 3

def self.rexml_document(stringlike)
  stringlike = stringlike.body.to_s if stringlike.respond_to?(:body)

  case stringlike
  when REXML::Document
    stringlike.root
  when REXML::Node, Array
    stringlike
  else
    begin
      REXML::Document.new(stringlike.to_s).root
    rescue REXML::ParseException => e
      if e.message.include?("second root element")
        REXML::Document.new("<fake-root-element>#{stringlike}</fake-root-element>").root
      else
        raise e
      end
    end
  end
end

.session_classObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/webrat/core/session.rb', line 15

def self.session_class
  case Webrat.configuration.mode
  when :rails
    RailsSession
  when :merb
    MerbSession
  when :selenium
    SeleniumSession
  when :rack
    RackSession
  when :sinatra
    SinatraSession
  when :mechanize
    MechanizeSession
  else
    raise WebratError.new(<<-STR)
Unknown Webrat mode: #{Webrat.configuration.mode.inspect}

Please ensure you have a Webrat configuration block that specifies a mode
in your test_helper.rb, spec_helper.rb, or env.rb (for Cucumber).

This configure block supercedes the need to require "webrat/<framework>".

For example:

Webrat.configure do |config|
  config.mode = :rails
end
    STR
  end
end

.start_app_serverObject

Start the appserver for the underlying framework to test

Sinatra: requires a config.ru in the root of your sinatra app to use this Merb: Attempts to use bin/merb and falls back to the system merb Rails: Calls mongrel_rails to startup the appserver



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/webrat/selenium.rb', line 40

def self.start_app_server
  case Webrat.configuration.application_framework
  when :sinatra
    fork do
      File.open('rack.pid', 'w') { |fp| fp.write Process.pid }
      exec 'rackup', File.expand_path(Dir.pwd + '/config.ru'), '-p', Webrat.configuration.application_port.to_s
    end
  when :merb
    cmd = 'merb'
    if File.exist?('bin/merb')
      cmd = 'bin/merb'
    end
    system("#{cmd} -d -p #{Webrat.configuration.application_port} -e #{Webrat.configuration.application_environment}")
  else # rails
    system("mongrel_rails start -d --chdir='#{RAILS_ROOT}' --port=#{Webrat.configuration.application_port} --environment=#{Webrat.configuration.application_environment} --pid #{pid_file} &")
  end
  TCPSocket.wait_for_service :host => Webrat.configuration.application_address, :port => Webrat.configuration.application_port.to_i
end

.start_selenium_serverObject

:nodoc:



15
16
17
18
19
20
21
22
# File 'lib/webrat/selenium.rb', line 15

def self.start_selenium_server #:nodoc:
  unless Webrat.configuration.selenium_server_address
    remote_control = ::Selenium::RemoteControl::RemoteControl.new("0.0.0.0", Webrat.configuration.selenium_server_port, 5)
    remote_control.jar_file = File.expand_path(__FILE__ + "../../../../vendor/selenium-server.jar")
    remote_control.start :background => true
  end
  TCPSocket.wait_for_service :host => (Webrat.configuration.selenium_server_address || "0.0.0.0"), :port => Webrat.configuration.selenium_server_port
end

.stop_app_serverObject

Stop the appserver for the underlying framework under test

Sinatra: Reads and kills the pid from the pid file created on startup Merb: Reads and kills the pid from the pid file created on startup Rails: Calls mongrel_rails stop to kill the appserver



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/webrat/selenium.rb', line 64

def self.stop_app_server 
  case Webrat.configuration.application_framework
  when :sinatra
    pid = File.read('rack.pid')
    system("kill -9 #{pid}")
    FileUtils.rm_f 'rack.pid'
  when :merb
    pid = File.read("log/merb.#{Webrat.configuration.application_port}.pid")
    system("kill -9 #{pid}")
    FileUtils.rm_f "log/merb.#{Webrat.configuration.application_port}.pid"
  else # rails
    system "mongrel_rails stop -c #{RAILS_ROOT} --pid #{pid_file}"
  end
end

.stop_selenium_serverObject

:nodoc:



24
25
26
# File 'lib/webrat/selenium.rb', line 24

def self.stop_selenium_server #:nodoc:
  ::Selenium::RemoteControl::RemoteControl.new("0.0.0.0", Webrat.configuration.selenium_server_port, 5).stop unless Webrat.configuration.selenium_server_address
end

.with_selenium_serverObject

:nodoc:



9
10
11
12
13
# File 'lib/webrat/selenium.rb', line 9

def self.with_selenium_server #:nodoc:
  start_selenium_server
  yield
  stop_selenium_server
end

.xml_nokogiri_document(stringlike) ⇒ Object

:nodoc:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/webrat/core/xml/nokogiri.rb', line 37

def self.xml_nokogiri_document(stringlike) #:nodoc:
  return stringlike.dom if stringlike.respond_to?(:dom)
  
  if Nokogiri::HTML::Document === stringlike
    stringlike
  elsif Nokogiri::XML::NodeSet === stringlike
    stringlike
  elsif StringIO === stringlike
    Nokogiri::XML(stringlike.string)
  elsif stringlike.respond_to?(:body)
    Nokogiri::XML(stringlike.body.to_s)
  else
    Nokogiri::XML(stringlike.to_s)
  end
end