Class: MonkeyTester::RandomWalker

Inherits:
Object
  • Object
show all
Includes:
Capybara::DSL
Defined in:
lib/monkey_tester/random_walker.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ RandomWalker

Returns a new instance of RandomWalker.



4
5
6
7
8
9
10
# File 'lib/monkey_tester/random_walker.rb', line 4

def initialize(options)
  @url = options[:url]
  @scheme = URI.parse(@url).scheme
  @host = URI.parse(@url).host
  @login = options[:login]
  @debug = options[:debug]
end

Instance Method Details

#walkObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/monkey_tester/random_walker.rb', line 12

def walk
  visit @url

  while true
    begin
       @login[:url] and next if page.current_url == @login[:url]
      puts [page.status_code, page.current_url, page.title].join("\t")
      next_link = all('a').reject{
        |a| a[:href] !~ /^#{@scheme}:\/\/#{@host}/ && a[:href] =~ /:\/\//
      }.sample
      page.evaluate_script('window.history.back()') and next unless next_link
      puts [next_link, next_link[:href], next_link.text].join("\t") if @debug
      next_link.click
    rescue Capybara::Poltergeist::MouseEventFailed => e
      # just ignore
    rescue => e
      puts e
      puts e.message
    end
  end
end