Class: AutoScreenshot::Screenshot

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = { urls: [], folder: nil, action_map: {} }) ⇒ Screenshot

urls: an array of full URL strings folder: where to store the files action_map: a hash where

keys are URL's that are visited
values are symbols for custom actions

see the ‘wait` method for an example of mapping an action



21
22
23
24
25
26
27
28
29
# File 'lib/auto_screenshot.rb', line 21

def initialize(options = {
                urls: [],
                folder: nil,
                action_map: {}
              })
  @urls = options[:urls]
  @folder = options[:folder] ||= "screenshots"
  @action_map = options[:action_map]
end

Instance Attribute Details

#action_mapObject

Returns the value of attribute action_map.



9
10
11
# File 'lib/auto_screenshot.rb', line 9

def action_map
  @action_map
end

#folderObject

Returns the value of attribute folder.



9
10
11
# File 'lib/auto_screenshot.rb', line 9

def folder
  @folder
end

#urlsObject

Returns the value of attribute urls.



9
10
11
# File 'lib/auto_screenshot.rb', line 9

def urls
  @urls
end

Instance Method Details

#actions(url) ⇒ Object

Custom actions, based on a page url do a specific action for a url, like #wait



60
61
62
63
64
65
66
# File 'lib/auto_screenshot.rb', line 60

def actions(url)
  if action_map && action_map.has_key?(url)
    self.send(action_map[url])
  else
    nil
  end
end

#go(dry_run = false) ⇒ Object

Passing ‘dry_run=true` only lists the URL’s that would be processed but does not take screenshots



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/auto_screenshot.rb', line 34

def go(dry_run = false)
  raise 'no URLs specified' if @urls.empty?

  errors = []

  @urls.each do |url|
    p url; next if dry_run

    begin
      puts "Getting #{url}"
      visit "#{url}"
      sleep 3.0 # arbitrary sleep to allow heavily ajax-y pages to load
      snap
      actions(url)
    rescue => err
      puts err.inspect
      puts "error on #{url}"
      errors << url
    end
  end

  errors
end

TODO: Helper Method



81
82
83
84
85
86
87
88
89
90
# File 'lib/auto_screenshot.rb', line 81

def grab_links(url)
  links = []

  visit "#{url}"
  nodes = Nokogiri::HTML(page.html).css("a")
  nodes.each do |link|
    links << link["href"]
  end
  links.uniq
end

#snap(descriptor = "") ⇒ Object

passing a ‘descriptor` adds a string to the filename, so you can name a sub-state for a page



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/auto_screenshot.rb', line 94

def snap(descriptor = "")
  name = clean_url(page.current_url)

  # Descriptor
  name = name + (descriptor.empty? ? "" : "-state-#{descriptor}")
  p "#snap", "name", name unless name.empty?

  set_window_size

  # Ensure @folder exists
  FileUtils.mkdir_p(@folder) unless File.exists?(@folder)
  Capybara.current_session.driver.browser.save_screenshot("#{@folder}/#{name}.png")
end

#waitObject

Define custom actions by monkey-patching. Use the custom actions by passing action_map: { :url => :method }

class AutoScreenshot::Screenshot

def your_custom_action
end

end



76
77
78
# File 'lib/auto_screenshot.rb', line 76

def wait
  sleep 10.0
end