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(opts = { :links => "", :action_mappings => [], :action_map => "", :folder => "" }) ⇒ Screenshot

Pass in an array of URL’s, with http(s)

or

Pass in a .json or .rb file of links

pass in an action_map pass in a folder path to where to store the screenshots

Raises:

  • (Exception)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/auto_screenshot.rb', line 21

def initialize(opts = { :links => "", :action_mappings => [], :action_map => "", :folder => "" })
  raise Exception, "please pass in an array of urls like {:urls => []}, or a file, like {:file => \"test.json\"}" unless opts[:links]

  @links = opts[:links]
  if @links.class == Array
    @links = links
  elsif links.class == String
    ext = File.extname(@links)
    if ext == ".rb"
      load @links
      @links = $links
    elsif ext == ".json"
      @links = JSON.parse(File.open(@links, "r").read)
    end
  end

  @action_mappings = set_action_mappings(opts[:action_mappings])
  load opts[:action_map].to_s
  @folder = opts[:folder] ||= "screenshots"
end

Instance Attribute Details

#action_mapObject

Returns the value of attribute action_map.



13
14
15
# File 'lib/auto_screenshot.rb', line 13

def action_map
  @action_map
end

#action_mappingsObject

Returns the value of attribute action_mappings.



13
14
15
# File 'lib/auto_screenshot.rb', line 13

def action_mappings
  @action_mappings
end

#folderObject

Returns the value of attribute folder.



13
14
15
# File 'lib/auto_screenshot.rb', line 13

def folder
  @folder
end

Returns the value of attribute links.



13
14
15
# File 'lib/auto_screenshot.rb', line 13

def links
  @links
end

Instance Method Details

#actions(url) ⇒ Object

do a specific action for a url, like login



74
75
76
77
78
79
80
81
# File 'lib/auto_screenshot.rb', line 74

def actions(url)
  if action_mappings.has_key?(url)
    sleep 10.0
    # self.send(action_mappings[url])
  else
    nil
  end
end

#goObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/auto_screenshot.rb', line 42

def go
  errors = []
  @links.each do |url|
    begin
      puts "Getting #{url}"
      visit "#{url}"

      snap
      actions(url)
    rescue => err
      puts err.inspect
      puts "error on #{url}"
      errors << url
    end
  end

  puts errors
end


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

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

#set_action_mappings(path) ⇒ Object

Load an action map .json file see /action_mappings.json as an example

"url":<method in action_map.rb>



66
67
68
69
70
71
# File 'lib/auto_screenshot.rb', line 66

def set_action_mappings(path)
  return {} if path && path.empty?

  json = File.open(path).read
  hash = JSON.parse(json)
end

#snapObject



94
95
96
97
98
99
# File 'lib/auto_screenshot.rb', line 94

def snap
  page.execute_script('$(window).width(1200)')
  name = page.current_url.gsub("https:\/\/", "").gsub("http:\/\/", "").gsub(":", "").gsub("\/", "-").gsub("&", "-").gsub("?", "_").gsub("dev.lvh.me3000-", "").gsub("admin.lvh.me3000-", "").gsub("localhost3001-", "").gsub("dev.dev.lvh.me3000-", "").gsub("test.civicideasstaging.com", "")
  Capybara.current_session.driver.browser.manage.window.resize_to(1000, 800)
  Capybara.current_session.driver.browser.save_screenshot("#{@folder}/#{name}.png")
end