watir-screenshot-stitch 0.6.10 → 0.7.4

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: '0985890117543c5ed7884e494c015439abe061ea'
4
- data.tar.gz: 37a9faf7f9e21dee30810044322d63ccc9193061
2
+ SHA256:
3
+ metadata.gz: adaad65e17883bc1bd5f9b7a05e9528a5c0bd732a7c3684905629d9ed398660e
4
+ data.tar.gz: bfd6668b82eda083a0a4fa0b08b3f512c5455b712f7a99db4dc4777f5122df30
5
5
  SHA512:
6
- metadata.gz: 63717c4834cdb6016b1000440b0fd3af5c1663c59d1eed651b205120e306223819b14a6de38471fabc8a6c9b80f5038458aa0db09c50cd45a58bc1042dbabe76
7
- data.tar.gz: 7101edc1f9aebdfd17bf9ecb3d2d233d091bd53f8b5d1b394e97da9d210aa1b715b8dd75fa4ce7b78a615d78416590f36543b729a03b553217acc01367958fa1
6
+ metadata.gz: 864f883e66f464dff46e91a79917a7cdd4e234e3893b15277ce2ab5f6f0fbf5cc8d038380e4dba2bf79f38846a09f6f340939761f8b2c9ddc485fbeaba06540c
7
+ data.tar.gz: b711cd36eefc07ffb408ecbac0e42670b8c2ffdebca7dfe7a94afe5c65b581cbdbc7d1a53a7c176899c903a8ee04e14b1a0e37e292bc6f064219bd5502fd5b32
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
- source "https://rubygems.org"
1
+ source 'https://rubygems.org'
2
2
 
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  # Specify your gem's dependencies in watir-screenshot-stitch.gemspec
6
6
  gemspec
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
@@ -1,4 +1,4 @@
1
1
  module WatirScreenshotStitch
2
2
  NAME = 'watir-screenshot-stitch'
3
- VERSION = "0.6.10"
3
+ VERSION = "0.7.4"
4
4
  end
@@ -1,4 +1,3 @@
1
-
2
1
  lib = File.expand_path("../lib", __FILE__)
3
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
3
  require "watir-screenshot-stitch/version"
@@ -31,12 +30,14 @@ Gem::Specification.new do |spec|
31
30
  spec.require_paths = ["lib"]
32
31
 
33
32
  spec.add_development_dependency "bundler", "~> 1.16"
34
- spec.add_development_dependency "rake", "~> 10.0"
33
+ spec.add_development_dependency "rake", ">= 12.3.3"
35
34
  spec.add_development_dependency "rspec", "~> 3.0"
36
35
  spec.add_development_dependency "chunky_png", "~> 1.3"
37
36
 
38
- spec.add_dependency "rubyzip", "~> 1.2.2"
39
- spec.add_dependency "watir", "~> 6.4"
37
+ spec.required_ruby_version = '>= 2.3.0'
38
+
39
+ spec.add_dependency "rubyzip", "~> 1.2"
40
+ spec.add_dependency "watir", "~> 6.12"
40
41
  spec.add_dependency "mini_magick", "~> 4.0"
41
42
  spec.add_dependency "os", "~> 1.0"
42
43
  spec.add_dependency "binding_of_caller", "~> 0.7"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: watir-screenshot-stitch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.10
4
+ version: 0.7.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Nissen
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2018-09-07 00:00:00.000000000 Z
12
+ date: 2020-06-26 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
@@ -73,28 +73,28 @@ dependencies:
73
73
  requirements:
74
74
  - - "~>"
75
75
  - !ruby/object:Gem::Version
76
- version: 1.2.2
76
+ version: '1.2'
77
77
  type: :runtime
78
78
  prerelease: false
79
79
  version_requirements: !ruby/object:Gem::Requirement
80
80
  requirements:
81
81
  - - "~>"
82
82
  - !ruby/object:Gem::Version
83
- version: 1.2.2
83
+ version: '1.2'
84
84
  - !ruby/object:Gem::Dependency
85
85
  name: watir
86
86
  requirement: !ruby/object:Gem::Requirement
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
@@ -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
@@ -173,7 +172,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
173
172
  requirements:
174
173
  - - ">="
175
174
  - !ruby/object:Gem::Version
176
- version: '0'
175
+ version: 2.3.0
177
176
  required_rubygems_version: !ruby/object:Gem::Requirement
178
177
  requirements:
179
178
  - - ">="
@@ -181,7 +180,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
181
180
  version: '0'
182
181
  requirements: []
183
182
  rubyforge_project:
184
- rubygems_version: 2.5.2.3
183
+ rubygems_version: 2.7.8
185
184
  signing_key:
186
185
  specification_version: 4
187
186
  summary: Extends Watir to take stitched-together screenshots of full web pages.
@@ -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