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() {}
@@donot_close_browser =
false

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


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/taza/site.rb', line 53

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.



37
38
39
# File 'lib/taza/site.rb', line 37

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


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

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

.donot_close_browserObject



34
35
36
# File 'lib/taza/site.rb', line 34

def self.donot_close_browser
  @@donot_close_browser = true
end

.settingsObject

:nodoc:



83
84
85
# File 'lib/taza/site.rb', line 83

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

Instance Method Details

#base_pathObject

:nodoc:



146
147
148
# File 'lib/taza/site.rb', line 146

def base_path # :nodoc:
  '.'
end

#close_browser_and_raise_if(original_error) ⇒ Object

:nodoc:



87
88
89
90
91
92
93
# File 'lib/taza/site.rb', line 87

def close_browser_and_raise_if original_error # :nodoc:
  begin
    @browser.close if (@i_created_browser && !@@donot_close_browser)
  ensure
    raise original_error if original_error
  end
end

#define_flowsObject

:nodoc:



111
112
113
114
115
# File 'lib/taza/site.rb', line 111

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

#define_site_pagesObject

:nodoc:



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/taza/site.rb', line 95

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 <<-EOS
    def #{page_name}(page_module = nil)
      page = '#{page_class}'.constantize.new(page_module)
      page.browser = @browser
      yield page if block_given?
      page
    end
    EOS
  end
end

#execute_block_and_close_browser(browser) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/taza/site.rb', line 69

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:



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

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


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

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

#pathObject

:nodoc:



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

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