Module: Scrubyt::Navigation::Firewatir

Defined in:
lib/scrubyt/core/navigation/agents/firewatir.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/scrubyt/core/navigation/agents/firewatir.rb', line 15

def self.included(base)
  base.module_eval do
    @@agent = FireWatir::Firefox.new unless defined? @@agent
    @@current_doc_url = nil
    @@current_doc_protocol = nil
    @@base_dir = nil
    @@host_name = nil
    @@history = []
    @@current_form = nil
    @@current_frame = nil

    ##
    #Action to fetch a document (either a file or a http address)
    #
    #*parameters*
    #
    #_doc_url_ - the url or file name to fetch
    def self.fetch(doc_url, *args)
      #Refactor this crap!!! with option_accessor stuff
      if args.size > 0
        mechanize_doc = args[0][:mechanize_doc]
        resolve = args[0][:resolve]
        basic_auth = args[0][:basic_auth]
        #Refactor this whole stuff as well!!! It looks awful...
        parse_and_set_basic_auth(basic_auth) if basic_auth
      else
        mechanize_doc = nil
        resolve = :full
      end

      @@current_doc_url = doc_url
      @@current_doc_protocol = determine_protocol
      if mechanize_doc.nil?
        handle_relative_path(doc_url) unless @@current_doc_protocol == 'xpath'
        handle_relative_url(doc_url, resolve)
        Scrubyt.log :ACTION, "fetching document: #{@@current_doc_url}"
        case @@current_doc_protocol
          when 'file'
		  @@agent.goto("file://"+ @@current_doc_url)
          else
		  @@agent.goto(@@current_doc_url)
        end
        @@mechanize_doc = "<html>#{@@agent.html}</html>"
      else
        @@mechanize_doc = mechanize_doc
      end
      @@hpricot_doc = Hpricot(PreFilterDocument.br_to_newline(@@mechanize_doc))
      store_host_name(@@agent.url)   # in case we're on a new host
    end
    
    def self.use_current_page
      @@mechanize_doc = "<html>#{@@agent.html}</html>"
      @@hpricot_doc = Hpricot(PreFilterDocument.br_to_newline(@@mechanize_doc))
    end
    
    def self.frame(attribute, value)
      if @@current_frame
        @@current_frame.frame(attribute, value)
      else
        @@current_frame = @@agent.frame(attribute, value)
      end
    end

    ##
    #Submit the last form;
    def self.submit(current_form, sleep_time=nil, button=nil, type=nil)
      if @@current_frame
        #BRUTAL hax but FW is such a shitty piece of software
        #this sucks FAIL omg            
        @@current_frame.locate
        form = Document.new(@@current_frame).all.find{|t| t.tagName=="FORM"}
        form.submit
      else
        @@agent.element_by_xpath(@@current_form).submit
      end            
      
      if sleep_time
        sleep sleep_time
        @@agent.wait
      end

      @@current_doc_url = @@agent.url
      @@mechanize_doc = "<html>#{@@agent.html}</html>"
      @@hpricot_doc = Hpricot(PreFilterDocument.br_to_newline(@@mechanize_doc))
    end

    ##
    #Click the link specified by the text
    def self.click_link(link_spec,index = 0,wait_secs=0)
      Scrubyt.log :ACTION, "Clicking link specified by: %p" % link_spec
      if link_spec.is_a?(Hash)
        elem = XPathUtils.generate_XPath(CompoundExampleLookup.find_node_from_compund_example(@@hpricot_doc, link_spec, false, index), nil, true) 
        result_page = @@agent.element_by_xpath(elem).click
      else
        @@agent.link(:innerHTML, Regexp.escape(link_spec)).click
      end            
      sleep(wait_secs) if wait_secs > 0
      @@agent.wait
      
      # evaluate the results
      extractor.evaluate_extractor
      
      @@current_doc_url = @@agent.url
      @@mechanize_doc = "<html>#{@@agent.html}</html>"
      @@hpricot_doc = Hpricot(PreFilterDocument.br_to_newline(@@mechanize_doc))
      Scrubyt.log :ACTION, "Fetching #{@@current_doc_url}"
    end    
    
    def self.click_by_xpath_if_exists(xpath, wait_secs=0)
      begin
        result_page = @@agent.element_by_xpath(xpath).click
        sleep(wait_secs) if wait_secs > 0
        @@agent.wait
        
        extractor.evaluate_extractor
        
        @@current_doc_url = @@agent.url
        @@mechanize_doc = "<html>#{@@agent.html}</html>"
        @@hpricot_doc = Hpricot(PreFilterDocument.br_to_newline(@@mechanize_doc))
        Scrubyt.log :ACTION, "Fetching #{@@current_doc_url}"              
      rescue Watir::Exception::UnknownObjectException
        Scrubyt.log :INFO, "XPath #{xpath} doesn't exist in this document"
      end
    end      

	  def self.click_by_xpath_without_evaluate(xpath, wait_secs=0)
		  Scrubyt.log :ACTION, "Clicking by XPath : %p" % xpath        
		  @@agent.element_by_xpath(xpath).click
		  Scrubyt.log :INFO, "sleeping #{wait_secs}..."
		  sleep(wait_secs) if wait_secs > 0
		  @@agent.wait

		  # does not call evaluate_extractor
		  #extractor.evaluate_extractor

		  @@current_doc_url = @@agent.url
		  @@mechanize_doc = "<html>#{@@agent.html}</html>"
		  @@hpricot_doc = Hpricot(PreFilterDocument.br_to_newline(@@mechanize_doc))
		  Scrubyt.log :ACTION, "Fetching #{@@current_doc_url}"            
	  end
		  

    def self.click_by_xpath(xpath, wait_secs=0)
      Scrubyt.log :ACTION, "Clicking by XPath : %p" % xpath        
      @@agent.element_by_xpath(xpath).click
      Scrubyt.log :INFO, "sleeping #{wait_secs}..."
      sleep(wait_secs) if wait_secs > 0
      @@agent.wait
      
      # evaluate the results
      extractor.evaluate_extractor
      
      @@current_doc_url = @@agent.url
      @@mechanize_doc = "<html>#{@@agent.html}</html>"
      @@hpricot_doc = Hpricot(PreFilterDocument.br_to_newline(@@mechanize_doc))
      Scrubyt.log :ACTION, "Fetching #{@@current_doc_url}"            
    end
    
    def self.click_image_map(index = 0)
      Scrubyt.log :ACTION, "Clicking image map at index: %p" % index
      uri = @@mechanize_doc.search("//area")[index]['href']
      result_page = @@agent.get(uri)
      @@current_doc_url = result_page.uri.to_s
      Scrubyt.log :ACTION, "Fetching #{@@current_doc_url}"
      fetch(@@current_doc_url, :mechanize_doc => result_page)
    end

    def self.store_host_name(doc_url)
      @@host_name = doc_url.match(/.*\..*?\//)[0] if doc_url.match(/.*\..*?\//)
      @@original_host_name ||= @@host_name
    end #end of method store_host_name

    def self.determine_protocol
      old_protocol = @@current_doc_protocol
      new_protocol = case @@current_doc_url
        when /^\/\//
          'xpath'
        when /^https/
          'https'
        when /^http/
          'http'
        when /^www\./
          'http'
        else
          'file'
        end
      return 'http' if ((old_protocol == 'http') && new_protocol == 'file')
      return 'https' if ((old_protocol == 'https') && new_protocol == 'file')
      new_protocol
    end

    def self.parse_and_set_basic_auth(basic_auth)
      , pass = basic_auth.split('@')
      Scrubyt.log :ACTION, "Basic authentication: login=<#{}>, pass=<#{pass}>"
      @@agent.basic_auth(, pass)
    end

    def self.handle_relative_path(doc_url)
      if @@base_dir == nil || doc_url[0..0] == "/"
        @@base_dir = doc_url.scan(/.+\//)[0] if @@current_doc_protocol == 'file'
      else
        @@current_doc_url = ((@@base_dir + doc_url) if doc_url !~ /#{@@base_dir}/)
      end
    end

    def self.handle_relative_url(doc_url, resolve)
      return if doc_url =~ /^(http:|javascript:)/
      if doc_url !~ /^\//
        first_char = doc_url[0..0]
        doc_url = ( first_char == '?'  ? '' : '/'  ) + doc_url
        if first_char == '?' #This is an ugly hack... really have to throw this shit out and go with mechanize's
          current_uri = @@mechanize_doc.uri.to_s
          current_uri = @@agent.history.first.uri.to_s if current_uri =~ /\/popup\//
          if (current_uri.include? '?')
            current_uri = current_uri.scan(/.+\//)[0]
          else
            current_uri += '/' unless current_uri[-1..-1] == '/'
          end
          @@current_doc_url = current_uri + doc_url
          return
        end
      end
      case resolve
        when :full
          @@current_doc_url = (@@host_name + doc_url) if ( @@host_name != nil && (doc_url !~ /#{@@host_name}/))
          @@current_doc_url = @@current_doc_url.split('/').uniq.join('/')
        when :host
          base_host_name = (@@host_name.count("/") == 2 ? @@host_name : @@host_name.scan(/(http.+?\/\/.+?)\//)[0][0])
          @@current_doc_url = base_host_name + doc_url
        else
          #custom resilving
          @@current_doc_url = resolve + doc_url
      end
    end
  
    def self.fill_textfield(textfield_name, query_string, wait_secs, useValue)
      @@current_form = "//input[@name='#{textfield_name}']/ancestor::form"
      target = @@current_frame || @@agent
      if useValue
        target.text_field(:name,textfield_name).value = query_string              
      else
        target.text_field(:name,textfield_name).set(query_string)
      end
      sleep(wait_secs) if wait_secs > 0
      @@mechanize_doc = "<html>#{@@agent.html}</html>"
      @@hpricot_doc = Hpricot(PreFilterDocument.br_to_newline(@@mechanize_doc))
      
    end

    ##
    #Action to fill a textarea with text
    def self.fill_textarea(textarea_name, text)
      @@current_form = "//input[@name='#{textarea_name}']/ancestor::form"
      @@agent.text_field(:name,textarea_name).set(text)
    end

    ##
    #Action for selecting an option from a dropdown box
    def self.select_option(selectlist_name, option)
      if selectlist_name.is_a? Hash
select_args = selectlist_name
unless select_args.size == 1
 raise "select_option only supports using a name or a hash with a single pair, e.g.: {:id => \"foo1\"}" 
end
@@current_form = "//select[@#{select_args.keys.first}='#{select_args.values.first}']/ancestor::form"
	    else
@@current_form = "//select[@name='#{selectlist_name}']/ancestor::form"
	    end
	    list = @@agent.select_list(:name,selectlist_name)
	    #STDOUT.puts "list = #{list.inspect}"
	    begin
error = list.select(option)
	    rescue
list.select_value(option) || error
	    end
    end

    def self.check_checkbox(checkbox_name)
      @@current_form = "//input[@name='#{checkbox_name}']/ancestor::form"
      @@agent.checkbox(:name,checkbox_name).set(true)
    end

    def self.check_radiobutton(checkbox_name, index=0)
      @@current_form = "//input[@name='#{checkbox_name}']/ancestor::form"
      @@agent.elements_by_xpath("//input[@name='#{checkbox_name}']")[index].set
    end

    def self.click_image_map(index=0)
      raise 'NotImplemented'
    end
    
    def self.wait(time=1)
      sleep(time)
      @@agent.wait
    end
    
    def self.close_firefox
      @@agent.close
    end
  end
end