watir-screenshot-stitch 0.6.11 → 0.7.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 1bf1235cd25a0f41d381177c93ca6a0668d5c116
4
- data.tar.gz: f69d3d620262a96e78f268a3a614f4b8c86f024f
2
+ SHA256:
3
+ metadata.gz: 8d42f5b829d5f32b2a1e9a6a8584568e514a106098062ca6e543c9b918282414
4
+ data.tar.gz: ad58d948a08a88f4193b5dc8c5b561b9ce6d62da98c26555c4001a291cd63140
5
5
  SHA512:
6
- metadata.gz: 8aad381a4e89008008aa6275961db9831c2960cea03fbf1efbd52631271505f3c9af67d8e8e7b8f7c15cf0b153b00b3921842f53ba446494be493547d588f6f7
7
- data.tar.gz: e6f0ffbea07ae0e507fb29a428f86951c48666224d43c92ab3658f9394ff15c1570fa268d3832cfbf75e10212664d5b17d4e5b647dd024c91797f4f8f2f0be15
6
+ metadata.gz: 3b2a5333d6d2909f1d2d7eaf749c3badfb48e5130a728b416dfa44b92685176bf58b527f0af06f0db4332af7f9d958178dc9aecb3f2de3fab3a96a33dc45aa8a
7
+ data.tar.gz: eee00e63702ec36baddad298c8bb8136369b46b1665f6f02d6511c82c6b1002544e879b4c0eea800dd30758d315d9d127e55af41cb7a796094e5833d91d09ec5
data/README.md CHANGED
@@ -1,10 +1,19 @@
1
1
  # watir-screenshot-stitch
2
2
 
3
- watir-screenshot-stitch attempts to compensate for Mozilla's decision
4
- not to (yet?) expose Firefox's full page screenshot functionality
5
- via geckodriver, [as indicated here](https://github.com/mozilla/geckodriver/issues/570),
6
- by paging down a given URL by the size of the viewport, capturing
7
- the entire page in the process.
3
+ watir-screenshot-stitch attempts to compensate for
4
+ the lack of full page screenshot functionality
5
+ in Selenium Webdriver.
6
+
7
+ It does so in three ways:
8
+
9
+ * Directly employing geckodriver's new full page screenshot
10
+ functionality (only on Firefox).
11
+ * Screenshot stitching, paging down a given URL by the size
12
+ of the viewport, capturing screenshots and adjoining them.
13
+ * Employing a bundled
14
+ [html2canvas](https://github.com/niklasvh/html2canvas)
15
+ script against the page to generate a png from a `canvas`
16
+ element.
8
17
 
9
18
  ## Installation
10
19
 
@@ -34,18 +43,84 @@ parts of this stack, you're a better Googler than me.
34
43
 
35
44
  ## Usage
36
45
 
37
- ### WARNING: Browser passing will soon no longer be required
46
+ ### Special note: Upgrading from <= 0.6.11
47
+
48
+ As warned in version 0.6.6 and beyond, the Watir::Screenshot
49
+ class will have access to the browser in watir-screenshot-stitch
50
+ version 0.7.0 and beyond, and it will not need to be
51
+ passed to the public methods. Previous implementations will break.
52
+
53
+ To adapt, change your function calls like so:
54
+
55
+ <table>
56
+ <thead>
57
+ <tr>
58
+ <th>
59
+ <= 0.6.5
60
+ </th>
61
+ <th>
62
+ >= 0.6.6 && <= 0.6.11
63
+ </th>
64
+ <th>
65
+ >= 0.7.0
66
+ </th>
67
+ </tr>
68
+ </thead>
69
+ <tbody>
70
+ <tr>
71
+ <td>
72
+ `save_stitch(path, browser, opts)`
73
+ </td>
74
+ <td>
75
+ `save_stitch(path, nil, opts)` or
76
+ `save_stitch(path, browser, opts)`
77
+ </td>
78
+ <td>
79
+ `save_stitch(path, opts)`
80
+ </td>
81
+ </tr>
82
+ <tr>
83
+ <td>
84
+ `base64_canvas(browser)`
85
+ </td>
86
+ <td>
87
+ `base64_canvas(browser)` or `base64_canvas`
88
+ </td>
89
+ <td>
90
+ `base64_canvas`
91
+ </td>
92
+ </tr>
93
+ </tbody>
94
+ </table>
95
+
96
+ ### Using geckodriver
97
+
98
+ watir-screenshot-stitch can employ a special function of geckodriver >= 0.24.0
99
+ while driving Firefox. This
38
100
 
39
- As of watir-screenshot-stitch version 0.7.0, the Watir::Screenshot
40
- class will have access to the browser, and it will not need to be
41
- passed to the public methods. `save_stitch(path, browser, opts)`
42
- will become `save_stitch(path, opts)`, and `base64_canvas(browser)`
43
- will become `base64_canvas` and existing implementations will
44
- break.
101
+ ```ruby
102
+ require 'watir-screenshot-stitch'
103
+ b = Watir::Browser.new :firefox
104
+ b.goto "https://github.com/mozilla/geckodriver/issues/570"
105
+ b.base64_geckodriver
106
+ ```
107
+ will return a base64 encoded image blob of the given site.
45
108
 
46
- To suppress warnings in the meantime, upgrade to Watir 6.12
47
- and pass `nil` in place of the `browser` for #save_stitch and
48
- use `base64_canvas` with no parameters.
109
+ In can be saved as a PNG by doing:
110
+ ```ruby
111
+ png = b.screenshot.base64_geckodriver
112
+ path = "/my/path/image.png"
113
+ File.open(path, 'wb') { |f| f.write(Base64.decode64(png)) }
114
+ ```
115
+
116
+ This is the option with the fewest complications, and should be used
117
+ if possible.
118
+
119
+ #### User geckodriver vs. webdrivers geckodriver
120
+
121
+ If using the webdrivers gem, watir-screenshot-stitch will attempt to
122
+ use the geckodriver included there, since that's likely to be
123
+ the driver employed by watir. If not, it falls back to the the system user's geckodriver.
49
124
 
50
125
  ### Stitching
51
126
 
@@ -58,7 +133,7 @@ opts = { :page_height_limit => 5000 }
58
133
 
59
134
  b = Watir::Browser.new :firefox
60
135
  b.goto "https://github.com/mozilla/geckodriver/issues/570"
61
- b.screenshot.save_stitch(path, b, opts)
136
+ b.screenshot.save_stitch(path, opts)
62
137
  ```
63
138
 
64
139
  will stitch together and save a full-page screenshot, up to 5000 pixels tall,
@@ -68,25 +143,31 @@ to `/my/path/image.png`.
68
143
 
69
144
  html2canvas is a JavaScript library watir-screenshot-stitch can employ to
70
145
  try to create a canvas element of the entire page and covert it to a blob.
71
- For instance,
146
+ For instance, this
72
147
 
73
148
  ```ruby
74
149
  require 'watir-screenshot-stitch'
75
150
 
76
151
  b = Watir::Browser.new :firefox
77
152
  b.goto "https://github.com/watir/watir/issues/702"
78
- b.screenshot.base64_canvas(b)
153
+ b.screenshot.base64_canvas
79
154
  ```
80
155
 
81
156
  will return a base64 encoded image blob of the given site.
82
157
 
83
158
  In can be saved as a PNG by doing:
84
159
  ```ruby
85
- png = b.screenshot.base64_canvas(b)
160
+ png = b.screenshot.base64_canvas
86
161
  path = "/my/path/image.png"
87
162
  File.open(path, 'wb') { |f| f.write(Base64.decode64(png)) }
88
163
  ```
89
164
 
165
+ This method of screenshotting
166
+ is less likely to have issues with stitching the images together,
167
+ and running out of memory but has limitations with certain element
168
+ types not being properly displayed. See their documentation for
169
+ more information.
170
+
90
171
  ### Doubling resolution calculations, including macOS Retina
91
172
 
92
173
  watir-screenshot-stitch uses CSS selectors to determine whether a
@@ -97,14 +178,6 @@ logic to determine how to stitch together images.
97
178
  This means that moving the browser window while it is be driven by
98
179
  Watir can cause unpredictable results.
99
180
 
100
- ### Passing the browser?
101
-
102
- This is obviously awkward and obtuse. Because watir-screenshot-stitch
103
- patches Watir, it does not change the way Watir calls the Screenshot class,
104
- which does not know about the Browser instance (it instead knows
105
- about the driver). And watir-screenshot-stitch needs the browser to execute
106
- JavaScript on the page.
107
-
108
181
  ### Options
109
182
 
110
183
  A hash of key value pairs.
@@ -113,17 +186,19 @@ A hash of key value pairs.
113
186
  Should refer to a positive Integer greater than the viewport height.
114
187
 
115
188
  ### Maximum height
116
- ImageMagick has a maximum pixel dimension of 65500, and all screenshots
117
- will be capped to a maximum height of 65500 regardless of any options
118
- to avoid errors.
189
+ ImageMagick has a maximum pixel dimension of 65500, and all stitched
190
+ screenshots will be capped to a maximum height of 65500 regardless
191
+ of any options to avoid errors.
119
192
 
120
193
  ## Development
121
194
 
122
- TODO: This.
195
+ Use `rspec` to run the tests, and see the
196
+ (contributing)[#Contributing] section below &mdash;
197
+ all are welcome.
123
198
 
124
199
  ## Contributing
125
200
 
126
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/watir-screenshot-stitch. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
201
+ Bug reports and pull requests are welcome on GitHub at https://github.com/samnissen/watir-screenshot-stitch. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
127
202
 
128
203
  ## License
129
204
 
@@ -1,6 +1,5 @@
1
1
  require "time"
2
2
  require "watir-screenshot-stitch/version"
3
- require "watir-screenshot-stitch/utilities"
4
3
  require "watir"
5
4
  require "mini_magick"
6
5
  require "os"
@@ -14,6 +13,33 @@ RANGE_MOD = 0.02
14
13
  module Watir
15
14
  class Screenshot
16
15
 
16
+ #
17
+ # Employs a cutting edge feature in geckodriver version 0.24.0
18
+ # to produce a Base64 encoded string of a full page screenshot.
19
+ #
20
+ # @warning
21
+ # This will fail if geckodriver is less than version 0.24.0.
22
+ #
23
+ # @info
24
+ # This is only a patch until this is baked into Selenium/Watir.
25
+ #
26
+ # @example
27
+ # browser.screenshot.base64_geckodriver
28
+ # #=> '7HWJ43tZDscPleeUuPW6HhN3x+z7vU/lufmH0qNTtTum94IBWMT46evImci1vnFGT'
29
+ #
30
+ # @return [String]
31
+ #
32
+
33
+ def base64_geckodriver
34
+ ensure_geckodriver
35
+
36
+ resource_url = build_driver_url
37
+
38
+ raw = request_payload(resource_url)
39
+
40
+ parse_gecko(raw)
41
+ end
42
+
17
43
  #
18
44
  # Represents stitched together screenshot and writes to file.
19
45
  #
@@ -27,11 +53,10 @@ module Watir
27
53
  # @param [Hash] opts
28
54
  #
29
55
 
30
- def save_stitch(path, browser = @browser, opts = {})
31
- return browser.screenshot.save(path) if base64_capable?
56
+ def save_stitch(path, opts = {})
57
+ return @browser.screenshot.save(path) if base64_capable?
32
58
  @options = opts
33
59
  @path = path
34
- deprecate_browser(browser, (__LINE__-3))
35
60
  calculate_dimensions
36
61
 
37
62
  return self.save(@path) if (one_shot? || bug_shot?)
@@ -57,9 +82,8 @@ module Watir
57
82
  # @return [String]
58
83
  #
59
84
 
60
- def base64_canvas(browser = @browser)
85
+ def base64_canvas
61
86
  return self.base64 if base64_capable?
62
- deprecate_browser(browser, (__LINE__-1))
63
87
  output = nil
64
88
 
65
89
  return self.base64 if one_shot? || bug_shot?
@@ -77,10 +101,56 @@ module Watir
77
101
  end
78
102
 
79
103
  private
80
- def deprecate_browser(browser, line)
81
- return unless browser
82
- warn "#{DateTime.now.strftime("%F %T")} WARN Watir Screenshot Stitch [DEPRECATION] Passing the browser is deprecated and will no longer work in version 0.7.0 /lib/watir-screenshot-stitch.rb:#{line}"
83
- @browser = browser
104
+ def parse_gecko(raw = '')
105
+ JSON.parse(raw, symbolize_names: true)[:value]
106
+ rescue JSON::ParserError => e
107
+ raise "geckodriver response '#{raw}' was malformed"
108
+ end
109
+
110
+ def request_payload(request_url)
111
+ url = URI.parse(request_url)
112
+ req = Net::HTTP::Get.new(request_url)
113
+ Net::HTTP.start(url.host, url.port) {|http| http.request(req) }.body
114
+ rescue Errno::ECONNREFUSED => e
115
+ raise "geckodriver could not be accessed at '#{request_url}'"
116
+ end
117
+
118
+ def build_driver_path
119
+ bridge = @browser.driver.session_storage.instance_variable_get(:@bridge)
120
+ sid = bridge.instance_variable_get(:@session_id)
121
+
122
+ raise "Unable to get geckodriver session ID." unless sid
123
+
124
+ "session/#{sid}/moz/screenshot/full"
125
+ end
126
+
127
+ def build_driver_url
128
+ bridge = @browser.driver.session_storage.instance_variable_get(:@bridge)
129
+ server_uri = bridge.instance_variable_get(:@http).instance_variable_get(:@server_url)
130
+
131
+ raise "Unable to get geckodriver server URI." unless server_uri
132
+
133
+ request_url = server_uri.to_s + build_driver_path
134
+ end
135
+
136
+ def ensure_geckodriver
137
+ raise "base64_geckodriver only works on Firefox" unless @browser.name == :firefox
138
+
139
+ if webdrivers_defined?
140
+ current_version = Webdrivers::Geckodriver.current_version
141
+ else
142
+ current_version = Gem::Version.new(%x{geckodriver --version}.match(/geckodriver (\d+\.\d+\.\d+)/)[1])
143
+ end
144
+
145
+ correct_version = (current_version >= Gem::Version.new("0.24.0"))
146
+
147
+ raise "base64_geckodriver requires version 0.24.x or greater" unless correct_version
148
+ end
149
+
150
+ def webdrivers_defined?
151
+ Object.const_get("Webdrivers")
152
+ rescue NameError
153
+ nil
84
154
  end
85
155
 
86
156
  # in IE & Safari a regular screenshot is a full page screenshot only
@@ -134,10 +204,10 @@ module Watir
134
204
  def html2canvas_payload
135
205
  case @browser.driver.browser
136
206
  when :firefox
137
- path = File.join(WatirScreenshotStitch::Utilities.directory, "vendor/html2canvas-0.4.1.js")
207
+ path = File.expand_path("../../vendor/html2canvas-0.4.1.js", __FILE__)
138
208
  File.read(path)
139
209
  else
140
- path = File.join(WatirScreenshotStitch::Utilities.directory, "vendor/html2canvas.js")
210
+ path = File.expand_path("../../vendor/html2canvas.js", __FILE__)
141
211
  File.read(path)
142
212
  end
143
213
  end
@@ -169,7 +239,7 @@ module Watir
169
239
  def build_canvas
170
240
  @start = MiniMagick::Image.read(Base64.decode64(self.base64))
171
241
  @combined_screenshot = MiniMagick::Image.new(@path)
172
- @combined_screenshot.run_command(:convert, "-size", "#{ @start.width }x#{ @page_height*@mac_factor }", "xc:white", @combined_screenshot.path)
242
+ @combined_screenshot.run_command(:convert, "-size", "#{ @start.width }x#{ @page_height*@mac_factor }", "xc:white", "-define", "png:color-type=6", @combined_screenshot.path)
173
243
  end
174
244
 
175
245
  def gather_slices
@@ -1,4 +1,4 @@
1
1
  module WatirScreenshotStitch
2
2
  NAME = 'watir-screenshot-stitch'
3
- VERSION = "0.6.11"
3
+ VERSION = "0.7.5"
4
4
  end
@@ -30,14 +30,14 @@ Gem::Specification.new do |spec|
30
30
  spec.require_paths = ["lib"]
31
31
 
32
32
  spec.add_development_dependency "bundler", "~> 1.16"
33
- spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rake", ">= 12.3.3"
34
34
  spec.add_development_dependency "rspec", "~> 3.0"
35
35
  spec.add_development_dependency "chunky_png", "~> 1.3"
36
36
 
37
37
  spec.required_ruby_version = '>= 2.3.0'
38
38
 
39
39
  spec.add_dependency "rubyzip", "~> 1.2"
40
- spec.add_dependency "watir", "~> 6.4"
40
+ spec.add_dependency "watir", "~> 6.12"
41
41
  spec.add_dependency "mini_magick", "~> 4.0"
42
42
  spec.add_dependency "os", "~> 1.0"
43
43
  spec.add_dependency "binding_of_caller", "~> 0.7"
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: watir-screenshot-stitch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.11
4
+ version: 0.7.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Nissen
8
8
  - Sandeep Singh
9
- autorequire:
9
+ autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2018-10-15 00:00:00.000000000 Z
12
+ date: 2020-10-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -29,16 +29,16 @@ dependencies:
29
29
  name: rake
30
30
  requirement: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - "~>"
32
+ - - ">="
33
33
  - !ruby/object:Gem::Version
34
- version: '10.0'
34
+ version: 12.3.3
35
35
  type: :development
36
36
  prerelease: false
37
37
  version_requirements: !ruby/object:Gem::Requirement
38
38
  requirements:
39
- - - "~>"
39
+ - - ">="
40
40
  - !ruby/object:Gem::Version
41
- version: '10.0'
41
+ version: 12.3.3
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: rspec
44
44
  requirement: !ruby/object:Gem::Requirement
@@ -87,14 +87,14 @@ dependencies:
87
87
  requirements:
88
88
  - - "~>"
89
89
  - !ruby/object:Gem::Version
90
- version: '6.4'
90
+ version: '6.12'
91
91
  type: :runtime
92
92
  prerelease: false
93
93
  version_requirements: !ruby/object:Gem::Requirement
94
94
  requirements:
95
95
  - - "~>"
96
96
  - !ruby/object:Gem::Version
97
- version: '6.4'
97
+ version: '6.12'
98
98
  - !ruby/object:Gem::Dependency
99
99
  name: mini_magick
100
100
  requirement: !ruby/object:Gem::Requirement
@@ -137,7 +137,7 @@ dependencies:
137
137
  - - "~>"
138
138
  - !ruby/object:Gem::Version
139
139
  version: '0.7'
140
- description:
140
+ description:
141
141
  email:
142
142
  - scnissen@gmail.com
143
143
  - sandeepnagra@gmail.com
@@ -156,7 +156,6 @@ files:
156
156
  - bin/console
157
157
  - bin/setup
158
158
  - lib/watir-screenshot-stitch.rb
159
- - lib/watir-screenshot-stitch/utilities.rb
160
159
  - lib/watir-screenshot-stitch/version.rb
161
160
  - vendor/html2canvas-0.4.1.js
162
161
  - vendor/html2canvas.js
@@ -165,7 +164,7 @@ homepage: https://github.com/samnissen/watir-screenshot-stitch
165
164
  licenses:
166
165
  - MIT
167
166
  metadata: {}
168
- post_install_message:
167
+ post_install_message:
169
168
  rdoc_options: []
170
169
  require_paths:
171
170
  - lib
@@ -180,9 +179,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
180
179
  - !ruby/object:Gem::Version
181
180
  version: '0'
182
181
  requirements: []
183
- rubyforge_project:
184
- rubygems_version: 2.6.14
185
- signing_key:
182
+ rubygems_version: 3.1.2
183
+ signing_key:
186
184
  specification_version: 4
187
185
  summary: Extends Watir to take stitched-together screenshots of full web pages.
188
186
  test_files: []
@@ -1,11 +0,0 @@
1
- module WatirScreenshotStitch
2
- module Utilities
3
- # Return a directory with the project libraries.
4
- def self.directory
5
- t = ["#{File.dirname(File.expand_path($0))}/../lib/#{WatirScreenshotStitch::NAME}",
6
- "#{Gem.dir}/gems/#{WatirScreenshotStitch::NAME}-#{WatirScreenshotStitch::VERSION}"]
7
- t.each {|i| return i if File.readable?(i) }
8
- raise "watir-screenshot-stitch could not be found in: #{t}"
9
- end # https://stackoverflow.com/a/5805783/1651458
10
- end
11
- end