Class: Capybara::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/tanakai/capybara_ext/session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#spiderObject

Returns the value of attribute spider.



8
9
10
# File 'lib/tanakai/capybara_ext/session.rb', line 8

def spider
  @spider
end

Instance Method Details

#current_response(response_type = :html) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/tanakai/capybara_ext/session.rb', line 88

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/tanakai/capybara_ext/session.rb', line 60

def destroy_driver!
  if @driver
    begin
      @driver.quit
    # handle Net::ReadTimeout error for Selenium like drivers
    rescue Net::ReadTimeout => e
      @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_visitObject



10
# File 'lib/tanakai/capybara_ext/session.rb', line 10

alias_method :original_visit, :visit

#restart!Object



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/tanakai/capybara_ext/session.rb', line 76

def restart!
  if mode.match?(/poltergeist/)
    @driver.browser.restart
    @driver.requests, @driver.responses = 0, 0
  else
    destroy_driver!
    driver
  end

  logger.info "Browser: driver has been restarted: name: #{mode}, pid: #{driver.pid}, port: #{driver.port}"
end

#scroll_to_bottomObject



139
140
141
# File 'lib/tanakai/capybara_ext/session.rb', line 139

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
# File 'lib/tanakai/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, sleep_interval = 0, 0

    begin
      check_request_options(visit_uri) unless skip_request_options
      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 => 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

end



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/tanakai/capybara_ext/session.rb', line 119

def within_new_window_by(action: nil, url: nil)
  case
  when action
    opened_window = window_opened_by { action.call }
    within_window(opened_window) do
      yield
      current_window.close
    end
  when url
    within_window(open_new_window) do
      visit(url)

      yield
      current_window.close
    end
  end
end