Sha256: 1b56126b06dc33e68331e08f04bbc28f717b87f8c6e7eddb1aea793fda1079f6

Contents?: true

Size: 1.84 KB

Versions: 19

Compression:

Stored size: 1.84 KB

Contents

#          Copyright (c) 2008 Michael Fellinger m.fellinger@gmail.com
# All files in this distribution are subject to the terms of the Ruby license.

class Browser
  attr_reader :cookie, :http

  def initialize(base = '/', &block)
    @base     = base
    @history  = []
    @uri      = URI("http://localhost:#{Ramaze::Global.port}")
    @http     = SimpleHttp.new(@uri)

    story(&block) if block_given?
  end

  def story(&block)
    instance_eval(&block) if block_given?
  end

  def get path = '/', hash = {}
    request(:get, path, hash)
  end

  def post path = '/', hash = {}
    request(:post, path, hash)
  end

  def erequest method, path, hash = {}
    response = request(method, path, hash)
    eval(response)
  rescue Object => ex
    p :response => response
    ex.message
  end

  def epost path = '/', hash = {}
    erequest(:post, path, hash)
  end

  def eget path = '/', hash = {}
    erequest(:get, path, hash)
  end

  def hget(*args)
    @page = Hpricot(get(*args))
  end

  def hpost(*args)
    @page = Hpricot(post(*args))
  end

  def find_link(text)
    link = @page.at("a[text()='#{text}']")
    link.should.not.be.nil if link.respond_to?(:should)
    link[:href]
  end

  def follow_link(text)
    hget find_link(text)
  end

  def follow_links(*texts)
    texts.each do |text|
      follow_link text
    end
  end

  def request method, path, hash = {}
    @http.uri = @uri + "/#{@base}/#{path}".squeeze('/')
    @http.request_headers['referer'] = @history.last.path rescue '/'

    if method == :get and not hash.empty?
      @http.uri.query = hash.inject([]){|s,(k,v)| s << "#{k}=#{v}"}.join('&')
      hash.clear
    end

    response = @http.send(method, hash).strip
    @history << @http.uri
    get_cookie

    response
  end

  def get_cookie
    @cookie = @http.response_headers['set-cookie']
    @http.request_headers['cookie'] = @cookie
  end
end

Version data entries

19 entries across 19 versions & 5 rubygems

Version Path
Pistos-ramaze-2008.09 lib/ramaze/spec/helper/browser.rb
Pistos-ramaze-2008.12 lib/ramaze/spec/helper/browser.rb
Pistos-ramaze-2009.01 lib/ramaze/spec/helper/browser.rb
Pistos-ramaze-2009.02 lib/ramaze/spec/helper/browser.rb
clivecrous-ramaze-0.3.9.5 lib/ramaze/spec/helper/browser.rb
manveru-ramaze-2008.07 lib/ramaze/spec/helper/browser.rb
manveru-ramaze-2008.08 lib/ramaze/spec/helper/browser.rb
manveru-ramaze-2008.09 lib/ramaze/spec/helper/browser.rb
manveru-ramaze-2008.10 lib/ramaze/spec/helper/browser.rb
manveru-ramaze-2008.12 lib/ramaze/spec/helper/browser.rb
manveru-ramaze-2009.01 lib/ramaze/spec/helper/browser.rb
ptomato-ramaze-2009.02.1 lib/ramaze/spec/helper/browser.rb
ptomato-ramaze-2009.02 lib/ramaze/spec/helper/browser.rb
ramaze-2009.01 lib/ramaze/spec/helper/browser.rb
ramaze-2008.06 lib/ramaze/spec/helper/browser.rb
ramaze-0.3.9.1 lib/ramaze/spec/helper/browser.rb
ramaze-2008.11 lib/ramaze/spec/helper/browser.rb
ramaze-2009.03 lib/ramaze/spec/helper/browser.rb
ramaze-2009.02 lib/ramaze/spec/helper/browser.rb