lib/docapurl/browser.rb in docapurl-0.2.1 vs lib/docapurl/browser.rb in docapurl-0.2.2

- old
+ new

@@ -2,54 +2,57 @@ require 'logger' module Docapurl class Browser SYSTEM_MAX_PAGE_DOWN_TO_BOTTOM = 50 - attr_accessor :browser, :save_path, :logger + attr_accessor :browser, :save_path, :logger, :context def initialize(options) @save_path = options[:save_path] options[:timeout] ||= 30 options[:slowmo] = 0.5 @logger = options[:logger] || Logger.new(STDOUT) @browser = Ferrum::Browser.new options + @context = browser.contexts.create + yield(self) if block_given? end def cap(url, options) - + page = @context.create_page options[:quality] ||= 90 options[:full] = true if options[:full].nil? options[:path] ||= @save_path host = fetch_domain(url) options[:path] ||= "screenshot-#{host.to_s == '' ? '' : "#{host}-"}#{Time.now.strftime('%F-%T')}.jpg" logger.info "browser begin to visit url #{url}" set_callback("before_visit_func", options) - browser.go_to(url) + page.go_to(url) set_callback("after_visit_func", options) logger.info 'visited' max_pagedown = options[:max_pagedown] || 5 pagedown_to_bottom = options.delete :pagedown_to_bottom - visit_whole_page(browser, max_pagedown: max_pagedown, pagedown_to_bottom: pagedown_to_bottom) + visit_whole_page(browser, page: page, max_pagedown: max_pagedown, pagedown_to_bottom: pagedown_to_bottom) sleep_before_screen = options.delete :sleep_before_screen logger.info "sleep #{sleep_before_screen.to_i} second before screenshot" sleep(sleep_before_screen.to_i) set_callback("before_screenshot_func", options) - browser.screenshot(**options) + page.screenshot(**options) set_callback("after_screenshot_func", options) logger.info "screenshot ended, path = #{options[:path]}" end def close return if browser.nil? + context.dispose unless context.nil? logger.info 'close browser' browser.quit end @@ -67,13 +70,13 @@ end page_down_count.times do logger.info "press PageDown .." - browser.keyboard.type(:PageDown) + page.keyboard.type(:PageDown) end logger.info "press HOME .." - browser.keyboard.type(:Home) + page.keyboard.type(:Home) end private def set_callback(name, options)