watir-screenshot-stitch 0.6.11 → 0.7.1

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: 77d9d165765e9f0bd0e189c7af282d535be07a5b24630ff5642ae07440e66654
4
+ data.tar.gz: 197c0b0f5df9211f2f1f8f0e8423bf8d7fa774a963c138957837bc82df5b52a5
5
5
  SHA512:
6
- metadata.gz: 8aad381a4e89008008aa6275961db9831c2960cea03fbf1efbd52631271505f3c9af67d8e8e7b8f7c15cf0b153b00b3921842f53ba446494be493547d588f6f7
7
- data.tar.gz: e6f0ffbea07ae0e507fb29a428f86951c48666224d43c92ab3658f9394ff15c1570fa268d3832cfbf75e10212664d5b17d4e5b647dd024c91797f4f8f2f0be15
6
+ metadata.gz: 55a78fe53008cb3e464d5fb947e66c3fd208ad0bee3545007450195a7158e6bc6f079feb02263263ddf7d7b256818145fa1ada4e3334ff3e5f6d7b86199b4472
7
+ data.tar.gz: 67bea36811afe3044532f31f58b2750e5c06f03685bfdd2d8effa18d8ff7080b5a27be6546b3643b23f1ef80e79e4072ce1f8d16b902813e6c7193d755f4a5ac
data/README.md CHANGED
@@ -1,10 +1,21 @@
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
+ [Mozilla's decision not to (yet?) expose Firefox's full page screenshot
5
+ functionality](https://github.com/mozilla/geckodriver/issues/570)
6
+ via geckodriver.
7
+
8
+ The canonical screenshot stitching is done by paging down a given URL
9
+ by the size of the viewport, capturing the entire page in the process,
10
+ up to the maximum height — see below.
11
+
12
+ Alternatively, it also bundles the
13
+ [html2canvas](https://github.com/niklasvh/html2canvas)
14
+ script and applies it to the page. This method of screenshotting
15
+ is less likely to have issues with stitching the images together,
16
+ and running out of memory but has limitations with certain element
17
+ types not being properly displayed. See their documentation for
18
+ more information.
8
19
 
9
20
  ## Installation
10
21
 
@@ -34,18 +45,84 @@ parts of this stack, you're a better Googler than me.
34
45
 
35
46
  ## Usage
36
47
 
37
- ### WARNING: Browser passing will soon no longer be required
48
+ ### Special note: Upgrading from <= 0.6.11
49
+
50
+ As warned in version 0.6.6 and beyond, the Watir::Screenshot
51
+ class will have access to the browser in watir-screenshot-stitch
52
+ version 0.7.0 and beyond, and it will not need to be
53
+ passed to the public methods. Previous implementations will break.
54
+
55
+ To adapt, change your function calls like so:
56
+
57
+ <table>
58
+ <thead>
59
+ <tr>
60
+ <th>
61
+ <= 0.6.5
62
+ </th>
63
+ <th>
64
+ >= 0.6.6 && <= 0.6.11
65
+ </th>
66
+ <th>
67
+ >= 0.7.0
68
+ </th>
69
+ </tr>
70
+ </thead>
71
+ <tbody>
72
+ <tr>
73
+ <td>
74
+ `save_stitch(path, browser, opts)`
75
+ </td>
76
+ <td>
77
+ `save_stitch(path, nil, opts)` or
78
+ `save_stitch(path, browser, opts)`
79
+ </td>
80
+ <td>
81
+ `save_stitch(path, opts)`
82
+ </td>
83
+ </tr>
84
+ <tr>
85
+ <td>
86
+ `base64_canvas(browser)`
87
+ </td>
88
+ <td>
89
+ `base64_canvas(browser)` or `base64_canvas`
90
+ </td>
91
+ <td>
92
+ `base64_canvas`
93
+ </td>
94
+ </tr>
95
+ </tbody>
96
+ </table>
97
+
98
+ ### Using geckodriver
99
+
100
+ watir-screenshot-stitch can employ a special function of geckodriver >= 0.24.0
101
+ while driving Firefox. This
38
102
 
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.
103
+ ```ruby
104
+ require 'watir-screenshot-stitch'
105
+ b = Watir::Browser.new :firefox
106
+ b.goto "https://github.com/mozilla/geckodriver/issues/570"
107
+ b.base64_geckodriver
108
+ ```
109
+ will return a base64 encoded image blob of the given site.
110
+
111
+ In can be saved as a PNG by doing:
112
+ ```ruby
113
+ png = b.screenshot.base64_canvas
114
+ path = "/my/path/image.png"
115
+ File.open(path, 'wb') { |f| f.write(Base64.decode64(png)) }
116
+ ```
45
117
 
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.
118
+ This is the option with the fewest complications, and should be used
119
+ if possible.
120
+
121
+ #### User geckodriver vs. webdrivers geckodriver
122
+
123
+ If using the webdrivers gem, watir-screenshot-stitch will attempt to
124
+ use the geckodriver included there, since that's likely to be
125
+ the driver employed by watir. If not, it falls back to the the system user's geckodriver.
49
126
 
50
127
  ### Stitching
51
128
 
@@ -58,7 +135,7 @@ opts = { :page_height_limit => 5000 }
58
135
 
59
136
  b = Watir::Browser.new :firefox
60
137
  b.goto "https://github.com/mozilla/geckodriver/issues/570"
61
- b.screenshot.save_stitch(path, b, opts)
138
+ b.screenshot.save_stitch(path, opts)
62
139
  ```
63
140
 
64
141
  will stitch together and save a full-page screenshot, up to 5000 pixels tall,
@@ -68,21 +145,21 @@ to `/my/path/image.png`.
68
145
 
69
146
  html2canvas is a JavaScript library watir-screenshot-stitch can employ to
70
147
  try to create a canvas element of the entire page and covert it to a blob.
71
- For instance,
148
+ For instance, this
72
149
 
73
150
  ```ruby
74
151
  require 'watir-screenshot-stitch'
75
152
 
76
153
  b = Watir::Browser.new :firefox
77
154
  b.goto "https://github.com/watir/watir/issues/702"
78
- b.screenshot.base64_canvas(b)
155
+ b.screenshot.base64_canvas
79
156
  ```
80
157
 
81
158
  will return a base64 encoded image blob of the given site.
82
159
 
83
160
  In can be saved as a PNG by doing:
84
161
  ```ruby
85
- png = b.screenshot.base64_canvas(b)
162
+ png = b.screenshot.base64_canvas
86
163
  path = "/my/path/image.png"
87
164
  File.open(path, 'wb') { |f| f.write(Base64.decode64(png)) }
88
165
  ```
@@ -97,14 +174,6 @@ logic to determine how to stitch together images.
97
174
  This means that moving the browser window while it is be driven by
98
175
  Watir can cause unpredictable results.
99
176
 
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
177
  ### Options
109
178
 
110
179
  A hash of key value pairs.
@@ -119,11 +188,13 @@ to avoid errors.
119
188
 
120
189
  ## Development
121
190
 
122
- TODO: This.
191
+ Use `rspec` to run the tests, and see the
192
+ (contributing)[#Contributing] section below &mdash;
193
+ all are welcome.
123
194
 
124
195
  ## Contributing
125
196
 
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.
197
+ 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
198
 
128
199
  ## License
129
200
 
@@ -14,6 +14,33 @@ RANGE_MOD = 0.02
14
14
  module Watir
15
15
  class Screenshot
16
16
 
17
+ #
18
+ # Employs a cutting edge feature in geckodriver version 0.24.0
19
+ # to produce a Base64 encoded string of a full page screenshot.
20
+ #
21
+ # @warning
22
+ # This will fail if geckodriver is less than version 0.24.0.
23
+ #
24
+ # @info
25
+ # This is only a patch until this is baked into Selenium/Watir.
26
+ #
27
+ # @example
28
+ # browser.screenshot.base64_geckodriver
29
+ # #=> '7HWJ43tZDscPleeUuPW6HhN3x+z7vU/lufmH0qNTtTum94IBWMT46evImci1vnFGT'
30
+ #
31
+ # @return [String]
32
+ #
33
+
34
+ def base64_geckodriver
35
+ ensure_geckodriver
36
+
37
+ resource_url = build_driver_url
38
+
39
+ raw = request_payload(resource_url)
40
+
41
+ parse_gecko(raw)
42
+ end
43
+
17
44
  #
18
45
  # Represents stitched together screenshot and writes to file.
19
46
  #
@@ -27,11 +54,10 @@ module Watir
27
54
  # @param [Hash] opts
28
55
  #
29
56
 
30
- def save_stitch(path, browser = @browser, opts = {})
31
- return browser.screenshot.save(path) if base64_capable?
57
+ def save_stitch(path, opts = {})
58
+ return @browser.screenshot.save(path) if base64_capable?
32
59
  @options = opts
33
60
  @path = path
34
- deprecate_browser(browser, (__LINE__-3))
35
61
  calculate_dimensions
36
62
 
37
63
  return self.save(@path) if (one_shot? || bug_shot?)
@@ -57,9 +83,8 @@ module Watir
57
83
  # @return [String]
58
84
  #
59
85
 
60
- def base64_canvas(browser = @browser)
86
+ def base64_canvas
61
87
  return self.base64 if base64_capable?
62
- deprecate_browser(browser, (__LINE__-1))
63
88
  output = nil
64
89
 
65
90
  return self.base64 if one_shot? || bug_shot?
@@ -77,10 +102,56 @@ module Watir
77
102
  end
78
103
 
79
104
  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
105
+ def parse_gecko(raw = '')
106
+ JSON.parse(raw, symbolize_names: true)[:value]
107
+ rescue JSON::ParserError => e
108
+ raise "geckodriver response '#{raw}' was malformed"
109
+ end
110
+
111
+ def request_payload(request_url)
112
+ url = URI.parse(request_url)
113
+ req = Net::HTTP::Get.new(request_url)
114
+ Net::HTTP.start(url.host, url.port) {|http| http.request(req) }.body
115
+ rescue Errno::ECONNREFUSED => e
116
+ raise "geckodriver could not be accessed at '#{request_url}'"
117
+ end
118
+
119
+ def build_driver_path
120
+ bridge = @browser.driver.session_storage.instance_variable_get(:@bridge)
121
+ sid = bridge.instance_variable_get(:@session_id)
122
+
123
+ raise "Unable to get geckodriver session ID." unless sid
124
+
125
+ "session/#{sid}/moz/screenshot/full"
126
+ end
127
+
128
+ def build_driver_url
129
+ bridge = @browser.driver.session_storage.instance_variable_get(:@bridge)
130
+ server_uri = bridge.instance_variable_get(:@http).instance_variable_get(:@server_url)
131
+
132
+ raise "Unable to get geckodriver server URI." unless server_uri
133
+
134
+ request_url = server_uri.to_s + build_driver_path
135
+ end
136
+
137
+ def ensure_geckodriver
138
+ raise "base64_geckodriver only works on Firefox" unless @browser.name == :firefox
139
+
140
+ if webdrivers_defined?
141
+ current_version = Webdrivers::Geckodriver.current_version
142
+ else
143
+ current_version = Gem::Version.new(%x{geckodriver --version}.match(/geckodriver (\d+\.\d+\.\d+)/)[1])
144
+ end
145
+
146
+ correct_version = (current_version >= Gem::Version.new("0.24.0"))
147
+
148
+ raise "base64_geckodriver requires version 0.24.x or greater" unless correct_version
149
+ end
150
+
151
+ def webdrivers_defined?
152
+ 'Webdrivers'.constantize
153
+ rescue
154
+ nil
84
155
  end
85
156
 
86
157
  # in IE & Safari a regular screenshot is a full page screenshot only
@@ -1,4 +1,4 @@
1
1
  module WatirScreenshotStitch
2
2
  NAME = 'watir-screenshot-stitch'
3
- VERSION = "0.6.11"
3
+ VERSION = "0.7.1"
4
4
  end
@@ -37,7 +37,7 @@ Gem::Specification.new do |spec|
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,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.11
4
+ version: 0.7.1
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-10-15 00:00:00.000000000 Z
12
+ date: 2019-03-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -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
@@ -181,7 +181,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
181
181
  version: '0'
182
182
  requirements: []
183
183
  rubyforge_project:
184
- rubygems_version: 2.6.14
184
+ rubygems_version: 2.7.7
185
185
  signing_key:
186
186
  specification_version: 4
187
187
  summary: Extends Watir to take stitched-together screenshots of full web pages.