Class: Taza::Site

Inherits:
Object show all
Defined in:
lib/taza/site.rb

Overview

An abstraction of a website, but more really a container for a sites pages.

You can generate a site by performing the following command:

$ ./script/generate site google

This will generate a site file for google, a flows folder, and a pages folder in lib

Example:

require 'taza'

class Google < Taza::Site

end

Constant Summary collapse

@@before_browser_closes =
Proc.new() {}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}, &block) ⇒ Site

A site can be called a few different ways

The following example creates a new browser object and closes it:

Google.new do
  google.search.set "taza"
  google.submit.click
end

This example will create a browser object but not close it:

Google.new.search.set "taza"

Sites can take a couple of parameters in the constructor:

:browser => a browser object to act on instead of creating one automatically
:url => the url of where to start the site


48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/taza/site.rb', line 48

def initialize(params={},&block)
  @module_name = self.class.parent.to_s
  @class_name  = self.class.to_s.split("::").last
  define_site_pages
  define_flows
  config = Settings.config(@class_name)
  if params[:browser]
    @browser = params[:browser]
  else
    @browser = Browser.create(config)
    @i_created_browser = true
  end
  @browser.goto(params[:url] || config[:url])
  execute_block_and_close_browser(browser,&block) if block_given?
end

Instance Attribute Details

#browserObject

Returns the value of attribute browser.



32
33
34
# File 'lib/taza/site.rb', line 32

def browser
  @browser
end

Class Method Details

.before_browser_closes(&block) ⇒ Object

Use this to do something with the browser before it closes, but note that it is a class method which means that this will get called for any instance of a site.

Here’s an example of how you might use it to print the DOM output of a browser before it closes:

Taza::Site.before_browser_closes do |browser|
  puts browser.html
end


29
30
31
# File 'lib/taza/site.rb', line 29

def self.before_browser_closes(&block)
  @@before_browser_closes = block
end

.settingsObject

:nodoc:



78
79
80
# File 'lib/taza/site.rb', line 78

def self.settings # :nodoc:
  Taza::Settings.site_file(self.name.to_s.split("::").last)
end

Instance Method Details

#base_pathObject

:nodoc:



141
142
143
# File 'lib/taza/site.rb', line 141

def base_path # :nodoc:
  '.'
end

#close_browser_and_raise_if(original_error) ⇒ Object

:nodoc:



82
83
84
85
86
87
88
# File 'lib/taza/site.rb', line 82

def close_browser_and_raise_if original_error # :nodoc:
  begin
    @browser.close if @i_created_browser
  ensure
    raise original_error if original_error
  end
end

#define_flowsObject

:nodoc:



106
107
108
109
110
# File 'lib/taza/site.rb', line 106

def define_flows # :nodoc:
  Dir.glob(flows_path) do |file|
    require file
  end
end

#define_site_pagesObject

:nodoc:



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/taza/site.rb', line 90

def define_site_pages # :nodoc:
  Dir.glob(pages_path) do |file|
    require file
    page_name = File.basename(file,'.rb')
    page_class = "#{@module_name}::#{page_name.camelize}"
    self.class.class_eval "    def \#{page_name}\n      page = '\#{page_class}'.constantize.new\n      page.browser = @browser\n      yield page if block_given?\n      page\n    end\n    EOS\n  end\nend\n"

#execute_block_and_close_browser(browser) ⇒ Object



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

def execute_block_and_close_browser(browser)
  begin
    yield self
  rescue => site_block_exception
  ensure
    begin
      @@before_browser_closes.call(browser)
    rescue => before_browser_closes_block_exception
      "" # so basically rcov has a bug where it would insist this block is uncovered when empty
    end
    close_browser_and_raise_if site_block_exception || before_browser_closes_block_exception
  end
end

#flows_pathObject

:nodoc:



133
134
135
# File 'lib/taza/site.rb', line 133

def flows_path # :nodoc:
  File.join(path,'flows','*.rb')
end

#pages_pathObject

This is used to call a flow belonging to the site

Example:

Google.new do |google|
  google.flow(:perform_search, :query => "taza")
end

Where the flow would be defined under lib/sites/google/flows/perform_search.rb and look like:

class PerformSearch < Taza::Flow
  alias :google :site

  def run(params={})
    google.search.set params[:query]
    google.submit.click
  end
end


129
130
131
# File 'lib/taza/site.rb', line 129

def pages_path # :nodoc:
  File.join(path,'pages','**','*.rb')
end

#pathObject

:nodoc:



137
138
139
# File 'lib/taza/site.rb', line 137

def path # :nodoc:
  File.join(base_path,'lib','sites',@class_name.underscore)
end