Class: Browser

Inherits:
Object show all
Defined in:
lib/ramaze/spec/helper/browser.rb

Overview

Copyright © 2008 Michael Fellinger [email protected] All files in this distribution are subject to the terms of the Ruby license.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base = '/', &block) ⇒ Browser

Returns a new instance of Browser.



7
8
9
10
11
12
13
14
# File 'lib/ramaze/spec/helper/browser.rb', line 7

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

  story(&block) if block_given?
end

Instance Attribute Details

Returns the value of attribute cookie.



5
6
7
# File 'lib/ramaze/spec/helper/browser.rb', line 5

def cookie
  @cookie
end

#httpObject (readonly)

Returns the value of attribute http.



5
6
7
# File 'lib/ramaze/spec/helper/browser.rb', line 5

def http
  @http
end

Instance Method Details

#eget(path = '/', hash = {}) ⇒ Object



40
41
42
# File 'lib/ramaze/spec/helper/browser.rb', line 40

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

#epost(path = '/', hash = {}) ⇒ Object



36
37
38
# File 'lib/ramaze/spec/helper/browser.rb', line 36

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

#erequest(method, path, hash = {}) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/ramaze/spec/helper/browser.rb', line 28

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


52
53
54
55
56
# File 'lib/ramaze/spec/helper/browser.rb', line 52

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


58
59
60
# File 'lib/ramaze/spec/helper/browser.rb', line 58

def follow_link(text)
  hget find_link(text)
end


62
63
64
65
66
# File 'lib/ramaze/spec/helper/browser.rb', line 62

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

#get(path = '/', hash = {}) ⇒ Object



20
21
22
# File 'lib/ramaze/spec/helper/browser.rb', line 20

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


84
85
86
87
# File 'lib/ramaze/spec/helper/browser.rb', line 84

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

#hget(*args) ⇒ Object



44
45
46
# File 'lib/ramaze/spec/helper/browser.rb', line 44

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

#hpost(*args) ⇒ Object



48
49
50
# File 'lib/ramaze/spec/helper/browser.rb', line 48

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

#post(path = '/', hash = {}) ⇒ Object



24
25
26
# File 'lib/ramaze/spec/helper/browser.rb', line 24

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

#request(method, path, hash = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ramaze/spec/helper/browser.rb', line 68

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

#story(&block) ⇒ Object



16
17
18
# File 'lib/ramaze/spec/helper/browser.rb', line 16

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