Class: Capybara::Session
- Inherits:
-
Object
- Object
- Capybara::Session
- Defined in:
- lib/kimurai/capybara_ext/session.rb
Instance Attribute Summary collapse
-
#spider ⇒ Object
Returns the value of attribute spider.
Instance Method Summary collapse
- #current_response(response_type = :html) ⇒ Object
- #destroy_driver! ⇒ Object
- #original_visit ⇒ Object
- #restart! ⇒ Object
- #scroll_to_bottom ⇒ Object
- #visit(visit_uri, delay: config.before_request[:delay], skip_request_options: false, max_retries: 3) ⇒ Object
-
#within_new_window_by(action: nil, url: nil) ⇒ Object
Handy method to perform some processing in the new tab within block and then automatically close this tab: Usage (url): browser.within_new_window_by(url: “google.com”) do do some stuff and then automatically close this tab and return back to the first tab end Usage (action) (when new tab opening by some action, for example by clicking on a particular element): action = -> { browser.find(“//some/element/path”).click } browser.within_new_window_by(action: action) do do some stuff and then automatically close this tab and return back to the first tab end.
Instance Attribute Details
#spider ⇒ Object
Returns the value of attribute spider.
8 9 10 |
# File 'lib/kimurai/capybara_ext/session.rb', line 8 def spider @spider end |
Instance Method Details
#current_response(response_type = :html) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/kimurai/capybara_ext/session.rb', line 84 def current_response(response_type = :html) case response_type when :html if config.encoding if config.encoding == :auto charset = body.force_encoding('ISO-8859-1').encode('UTF-8')[/<meta.*?charset="?([\w+\d+-]*)/i, 1] Nokogiri::HTML(body, nil, charset) else Nokogiri::HTML(body, nil, config.encoding) end else Nokogiri::HTML(body) end when :json JSON.parse(body) end end |
#destroy_driver! ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/kimurai/capybara_ext/session.rb', line 61 def destroy_driver! if @driver begin @driver.quit # handle Net::ReadTimeout error for Selenium like drivers rescue Net::ReadTimeout @driver.quit end @driver = nil logger.info "Browser: driver #{mode} has been destroyed" else logger.warn "Browser: driver #{mode} is not present" end end |
#original_visit ⇒ Object
10 |
# File 'lib/kimurai/capybara_ext/session.rb', line 10 alias original_visit visit |
#restart! ⇒ Object
77 78 79 80 81 82 |
# File 'lib/kimurai/capybara_ext/session.rb', line 77 def restart! destroy_driver! driver logger.info "Browser: driver has been restarted: name: #{mode}, pid: #{driver.pid}, port: #{driver.port}" end |
#scroll_to_bottom ⇒ Object
134 135 136 |
# File 'lib/kimurai/capybara_ext/session.rb', line 134 def scroll_to_bottom execute_script('window.scrollBy(0,10000)') end |
#visit(visit_uri, delay: config.before_request[:delay], skip_request_options: false, max_retries: 3) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/kimurai/capybara_ext/session.rb', line 11 def visit(visit_uri, delay: config.before_request[:delay], skip_request_options: false, max_retries: 3) if spider process_delay(delay) if delay retries = 0 sleep_interval = 0 begin (visit_uri) unless driver.requests += 1 and logger.info "Browser: started get request to: #{visit_uri}" spider.class.update(:visits, :requests) if spider.with_info original_visit(visit_uri) rescue StandardError => e if match_error?(e, type: :to_skip) logger.error "Browser: skip request error: #{e.inspect}, url: #{visit_uri}" spider.add_event(:requests_errors, e.inspect) if spider.with_info false elsif match_error?(e, type: :to_retry) logger.error "Browser: retry request error: #{e.inspect}, url: #{visit_uri}" spider.add_event(:requests_errors, e.inspect) if spider.with_info if (retries += 1) <= max_retries logger.info "Browser: sleep #{sleep_interval += 15} seconds and process retry № #{retries} to the url: #{visit_uri}" sleep sleep_interval and retry else logger.error "Browser: all retries (#{retries - 1}) to the url #{visit_uri} are gone" raise e unless skip_error_on_failure?(e) end else raise e end else driver.responses += 1 and logger.info "Browser: finished get request to: #{visit_uri}" spider.class.update(:visits, :responses) if spider.with_info driver.visited = true unless driver.visited true ensure if spider.with_info logger.info "Info: visits: requests: #{spider.class.visits[:requests]}, responses: #{spider.class.visits[:responses]}" end if (memory = driver.current_memory) logger.debug "Browser: driver.current_memory: #{memory}" end end else original_visit(visit_uri) end end |
#within_new_window_by(action: nil, url: nil) ⇒ Object
Handy method to perform some processing in the new tab within block and then automatically close this tab: Usage (url): browser.within_new_window_by(url: “google.com”) do do some stuff and then automatically close this tab and return back to the first tab end Usage (action) (when new tab opening by some action, for example by clicking on a particular element): action = -> { browser.find(“//some/element/path”).click } browser.within_new_window_by(action: action) do do some stuff and then automatically close this tab and return back to the first tab end
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/kimurai/capybara_ext/session.rb', line 115 def within_new_window_by(action: nil, url: nil) if action opened_window = window_opened_by { action.call } within_window(opened_window) do yield current_window.close end elsif url within_window(open_new_window) do visit(url) yield current_window.close end end end |