auto_screenshot 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -5,3 +5,7 @@ gemspec
5
5
 
6
6
  gem 'capybara'
7
7
  gem 'json'
8
+
9
+ group :development do
10
+ gem 'pry'
11
+ end
data/README.md CHANGED
@@ -1,42 +1,47 @@
1
1
  Auto Screenshot
2
2
  ===============
3
3
 
4
- A small library designed to help document web products.
4
+ A small library designed to help the document web products.
5
5
 
6
6
  ## Usage
7
7
 
8
- From console, within the /auto_screenshot directory:
9
-
10
- irb
11
- load auto_screenshot.rb
8
+ gem install auto_screenshot
12
9
 
13
- This works in 2 ways:
10
+ ### Basics
14
11
 
15
- Method 1: Pass in an array of URL's
12
+ Pass in an array of URL's,
16
13
 
17
- urls = ["http://ryanwold.net", "http://afomi.com", "http://www.granicus.com"]
18
- s = Screenshot.new(:urls => urls) # => an array of URL's as strings
14
+ urls = ["http://domain.lvh.me:3000", "http://domain.lvh.me:3000/link/1"]
15
+ s = Screenshot.new(:urls => urls)
19
16
 
20
- Or
17
+ read from a [.json](/links.json) file,
21
18
 
22
- Method 2: Read from a .json file
19
+ s = Screenshot.new(:file => "test.json") # an array of URL's as string
23
20
 
24
- s = Screenshot.new(:file => "test.json") # => an array of URL's as strings
25
- s.go # => screenshots are saved to the /screenshots directory as .png files
21
+ or, read a [.rb](/links.rb) file.
26
22
 
27
- gem install auto_screenshot
28
- rake screenshots --all
29
-
30
- screenshot based on a .rb file or pass arguments
23
+ s = Screenshot.new(:file => "/users/name/path/to/links.rb")
24
+
25
+ Run Capybara (Selenium) and Firefox.
26
+
27
+ s.go # screenshots are saved to the /screenshots directory as .png files.
28
+
29
+ ### Logging in and custom stuff
30
+
31
+ [Action Map](/action_map.rb) - an action map allows you to specify certain URL's that call some ruby code that you write. I built this to accomodate logins. It can be used for whatever.
32
+
33
+ s = Screenshot.new({ :file => "/some/links/in/ruby.rb",
34
+ :action_map => "/users/name/path/to/action_map.rb" })
31
35
 
32
- load 'auto_screenshot.rb'
33
- s = Screenshot.new(:file => "test.json")
34
- s.go
36
+ load 'lib/auto_screenshot.rb'
35
37
 
36
- Alternatively, use a ruby file to generate a @links array of URL strings for each page (see dmap.rb as an example)
38
+ s = AutoScreenshot::Screenshot.new({
39
+ :links => "/users/ryanw/Desktop/ci.rb",
40
+ :action_mappings => "/path/to/action_mappings.json",
41
+ :action_map => "/path/to/action_map.rb",
42
+ :folder => "/path/to/screenshot_images"
43
+ })
37
44
 
38
- load 'dmap.rb'
39
- s = Screenshot.new(:urls => $links)
40
45
 
41
46
  #### Remember
42
47
 
@@ -7,39 +7,45 @@ require 'capybara/dsl'
7
7
  module AutoScreenshot
8
8
 
9
9
  class Screenshot
10
-
11
10
  include Capybara::DSL
12
11
  Capybara.default_driver = :selenium
13
12
 
14
- attr_accessor :urls, :folder, :action_map_path, :action_map
15
-
16
- def initialize(opts = { :urls => [], :file => "", :rb_file => nil, :action_map_path => "", :folder => "" })
17
- raise Exception, "please pass in an array of urls like {:urls => []}, or a file, like {:file => \"test.json\"}" unless opts[:urls] or opts[:file] or opts[:rb_file]
18
-
19
- if opts[:urls]
20
- @urls = opts[:urls]
21
- elsif opts[:rb_file]
22
- require_relative opts[:rb_file]
23
- @urls = $links
24
- elsif opts[:file]
25
- @urls = JSON.parse(File.open(opts[:file], "r").read)
13
+ attr_accessor :links, :folder, :action_mappings, :action_map
14
+
15
+ # Pass in an array of URL's, with http(s)
16
+ # or
17
+ # Pass in a .json or .rb file of links
18
+ #
19
+ # pass in an action_map
20
+ # pass in a folder path to where to store the screenshots
21
+ def initialize(opts = { :links => "", :action_mappings => [], :action_map => "", :folder => "" })
22
+ raise Exception, "please pass in an array of urls like {:urls => []}, or a file, like {:file => \"test.json\"}" unless opts[:links]
23
+
24
+ @links = opts[:links]
25
+ if @links.class == Array
26
+ @links = links
27
+ elsif links.class == String
28
+ ext = File.extname(@links)
29
+ if ext == ".rb"
30
+ load @links
31
+ @links = $links
32
+ elsif ext == ".json"
33
+ @links = JSON.parse(File.open(@links, "r").read)
34
+ end
26
35
  end
27
36
 
28
- @action_map_path = opts[:action_map_path]
37
+ @action_mappings = set_action_mappings(opts[:action_mappings])
38
+ load opts[:action_map].to_s
29
39
  @folder = opts[:folder] ||= "screenshots"
30
- @action_map = action_map
31
40
  end
32
41
 
33
42
  def go
34
43
  errors = []
35
- @urls.each do |url|
44
+ @links.each do |url|
36
45
  begin
37
46
  puts "Getting #{url}"
38
47
  visit "#{url}"
39
48
 
40
- page.wait_until do
41
- page.evaluate_script 'jQuery.active == 0'
42
- end
43
49
  snap
44
50
  actions(url)
45
51
  rescue => err
@@ -52,38 +58,28 @@ module AutoScreenshot
52
58
  puts errors
53
59
  end
54
60
 
55
- def action_map
56
- json = File.open(@action_map_path).read
61
+ # Load an action map .json file
62
+ # see /action_mappings.json as an example
63
+ # {
64
+ # "url":<method in action_map.rb>
65
+ # }
66
+ def set_action_mappings(path)
67
+ return {} if path && path.empty?
68
+
69
+ json = File.open(path).read
57
70
  hash = JSON.parse(json)
58
71
  end
59
72
 
60
-
61
- # custom actions for Granicus
62
- # something custom based on a page. pages must be ordered in the URL array though
73
+ # do a specific action for a url, like login
63
74
  def actions(url)
64
- # do a specific action for a url, like login
65
-
66
- if action_map.has_key?(url)
67
- self.send(action_map[url])
75
+ if action_mappings.has_key?(url)
76
+ sleep 10.0
77
+ # self.send(action_mappings[url])
68
78
  else
69
79
  nil
70
80
  end
71
81
  end
72
82
 
73
- def login
74
- click_on "Sign in / up"
75
- sleep 2.0
76
- fill_in "user_email", :with => "ryanw@granicus.com"
77
- fill_in "user_password", :with => ""
78
- click_on "Sign in"
79
- sleep 2.0
80
- wait_for_ajax
81
- end
82
-
83
- def wait
84
- sleep 15.0
85
- end
86
-
87
83
  def grab_links(url)
88
84
  links = []
89
85
 
@@ -99,7 +95,7 @@ module AutoScreenshot
99
95
  page.execute_script('$(window).width(1200)')
100
96
  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", "")
101
97
  Capybara.current_session.driver.browser.manage.window.resize_to(1000, 800)
102
- Capybara.current_session.driver.browser.save_screenshot("#{File.dirname(__FILE__)}/#{@folder}/#{name}.png")
98
+ Capybara.current_session.driver.browser.save_screenshot("#{@folder}/#{name}.png")
103
99
  end
104
100
 
105
101
  end
@@ -1,3 +1,3 @@
1
1
  module AutoScreenshot
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auto_screenshot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-19 00:00:00.000000000Z
12
+ date: 2013-03-20 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: capybara
16
- requirement: &70132291619560 !ruby/object:Gem::Requirement
16
+ requirement: &70237450785980 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70132291619560
24
+ version_requirements: *70237450785980
25
25
  description: ! '''Automatically screenshot webpages'''
26
26
  email:
27
27
  - wold@afomi.com