Skip to content

Commit 53830e3

Browse files
committed
Fix #137 Implement mhtml method
1 parent 142f10c commit 53830e3

File tree

4 files changed

+99
-38
lines changed

4 files changed

+99
-38
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,18 @@ browser.go_to("https://google.com/")
372372
browser.pdf(path: "google.pdf", paper_width: 1.0, paper_height: 1.0) # => 14983
373373
```
374374

375+
#### mhtml(\*\*options) : `String` | `Integer`
376+
377+
Saves MHTML on a disk or returns it as a string.
378+
379+
* options `Hash`
380+
* :path `String` to save a file on the disk.
381+
382+
```ruby
383+
browser.go_to("https://google.com/")
384+
browser.mhtml(path: "google.mhtml") # => 87742
385+
```
386+
375387

376388
## Network
377389

lib/ferrum/browser.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Browser
2222
body doctype set_content
2323
headers cookies network
2424
mouse keyboard
25-
screenshot pdf viewport_size
25+
screenshot pdf mhtml viewport_size
2626
frames frame_by main_frame
2727
evaluate evaluate_on evaluate_async execute
2828
add_script_tag add_style_tag bypass_csp

lib/ferrum/page/screenshot.rb

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,24 @@ def screenshot(**opts)
2929
options = screenshot_options(path, **opts)
3030
data = capture_screenshot(options, opts[:full])
3131
return data if encoding == :base64
32-
save_file(path, data)
32+
33+
bin = Base64.decode64(data)
34+
save_file(path, bin)
3335
end
3436

3537
def pdf(**opts)
3638
path, encoding = common_options(**opts)
3739
options = pdf_options(**opts)
3840
data = command("Page.printToPDF", **options).fetch("data")
3941
return data if encoding == :base64
42+
43+
bin = Base64.decode64(data)
44+
save_file(path, bin)
45+
end
46+
47+
def mhtml(path: nil)
48+
data = command("Page.captureSnapshot", format: :mhtml).fetch("data")
49+
return data if path.nil?
4050
save_file(path, data)
4151
end
4252

@@ -56,9 +66,8 @@ def document_size
5666
private
5767

5868
def save_file(path, data)
59-
bin = Base64.decode64(data)
60-
return bin unless path
61-
File.open(path.to_s, "wb") { |f| f.write(bin) }
69+
return data unless path
70+
File.open(path.to_s, "wb") { |f| f.write(data) }
6271
end
6372

6473
def common_options(encoding: :base64, path: nil, **_)

spec/screenshot_spec.rb

Lines changed: 73 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ def create_screenshot(**options)
8787
end
8888

8989
after do
90-
FileUtils.rm_f("#{PROJECT_ROOT}/spec/tmp/screenshot.pdf")
9190
FileUtils.rm_f("#{PROJECT_ROOT}/spec/tmp/screenshot.png")
9291
end
9392

@@ -206,6 +205,56 @@ def create_screenshot(**options)
206205
include_examples "when scale is set"
207206
end
208207

208+
include_examples "screenshot screen"
209+
210+
context "when encoding is base64" do
211+
let(:file) { "#{PROJECT_ROOT}/spec/tmp/screenshot.#{format}" }
212+
213+
def create_screenshot(path: file, **options)
214+
image = browser.screenshot(format: format, encoding: :base64, **options)
215+
File.open(file, "wb") { |f| f.write Base64.decode64(image) }
216+
end
217+
218+
it "defaults to base64 when path isn't set" do
219+
browser.go_to
220+
221+
screenshot = browser.screenshot(format: format)
222+
223+
expect(screenshot.length).to be > 100
224+
end
225+
226+
it "supports screenshotting the page in base64" do
227+
browser.go_to
228+
229+
screenshot = browser.screenshot(encoding: :base64)
230+
231+
expect(screenshot.length).to be > 100
232+
end
233+
234+
context "png" do
235+
let(:format) { :png }
236+
after { FileUtils.rm_f(file) }
237+
238+
include_examples "screenshot screen"
239+
end
240+
241+
context "jpeg" do
242+
let(:format) { :jpeg }
243+
after { FileUtils.rm_f(file) }
244+
245+
include_examples "screenshot screen"
246+
end
247+
end
248+
end
249+
250+
describe "#pdf" do
251+
let(:format) { :pdf }
252+
let(:file) { "#{PROJECT_ROOT}/spec/tmp/screenshot.#{format}" }
253+
254+
after do
255+
FileUtils.rm_f("#{PROJECT_ROOT}/spec/tmp/screenshot.pdf")
256+
end
257+
209258
context "when :paper_width and :paper_height are set" do
210259
it "changes pdf size" do
211260
browser.go_to("/ferrum/long_page")
@@ -280,46 +329,37 @@ def create_screenshot(**options)
280329
transfer_mode: "ReturnAsBase64")
281330
end
282331
end
332+
end
283333

284-
include_examples "screenshot screen"
285-
286-
context "when encoding is base64" do
287-
let(:file) { "#{PROJECT_ROOT}/spec/tmp/screenshot.#{format}" }
288-
289-
def create_screenshot(path: file, **options)
290-
image = browser.screenshot(format: format, encoding: :base64, **options)
291-
File.open(file, "wb") { |f| f.write Base64.decode64(image) }
292-
end
293-
294-
it "defaults to base64 when path isn't set" do
295-
browser.go_to
296-
297-
screenshot = browser.screenshot(format: format)
298-
299-
expect(screenshot.length).to be > 100
300-
end
334+
describe "#mhtml" do
335+
let(:format) { :mhtml }
336+
let(:file) { "#{PROJECT_ROOT}/spec/tmp/screenshot.#{format}" }
301337

302-
it "supports screenshotting the page in base64" do
303-
browser.go_to
338+
after do
339+
FileUtils.rm_f("#{PROJECT_ROOT}/spec/tmp/screenshot.mhtml")
340+
end
304341

305-
screenshot = browser.screenshot(encoding: :base64)
342+
it "returns data" do
343+
browser.go_to("/ferrum/simple")
306344

307-
expect(screenshot.length).to be > 100
308-
end
345+
data = browser.mhtml
309346

310-
context "png" do
311-
let(:format) { :png }
312-
after { FileUtils.rm_f(file) }
347+
expect(data).to match(/\/ferrum\/simple/)
348+
expect(data).to match(/mhtml.blink/)
349+
expect(data).to match(/\<\!DOCTYPE html\>/)
350+
expect(data).to match(/Foo\<br\>Bar/)
351+
end
313352

314-
include_examples "screenshot screen"
315-
end
353+
it "saves a file" do
354+
browser.go_to("/ferrum/simple")
316355

317-
context "jpeg" do
318-
let(:format) { :jpeg }
319-
after { FileUtils.rm_f(file) }
356+
browser.mhtml(path: file)
320357

321-
include_examples "screenshot screen"
322-
end
358+
content = File.read(file)
359+
expect(content).to match(/\/ferrum\/simple/)
360+
expect(content).to match(/mhtml.blink/)
361+
expect(content).to match(/\<\!DOCTYPE html\>/)
362+
expect(content).to match(/Foo\<br\>Bar/)
323363
end
324364
end
325365
end

0 commit comments

Comments
 (0)