Class: Jscov::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/jscov/collector.rb

Instance Method Summary collapse

Constructor Details

#initialize(session, logs) ⇒ Collector

Returns a new instance of Collector.



6
7
8
9
# File 'lib/jscov/collector.rb', line 6

def initialize(session, logs)
  @session = session || Capybara.current_session
  @logs = logs
end

Instance Method Details

#browser_logsObject



34
35
36
37
# File 'lib/jscov/collector.rb', line 34

def browser_logs
  logs = @session.driver.browser.logs.get(:browser)
  (@logs || []) + logs
end

#coveragesObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/jscov/collector.rb', line 11

def coverages
  return [] unless supported_driver?

  dump_coverage

  browser_logs
    .map { |log| parse(log.message) }
    .compact
    .map { |cov| Coverage.new(cov) }
end

#dump_coverageObject



27
28
29
30
31
32
# File 'lib/jscov/collector.rb', line 27

def dump_coverage
  code = <<~JS
    typeof __jscov_dumpCoverage === 'function' && __jscov_dumpCoverage()
  JS
  @session.execute_script(code)
end

#parse(message) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/jscov/collector.rb', line 39

def parse(message)
  json_string = message[/__jscov" (.*)/, 1]
  return if json_string.nil?

  json = JSON.parse(json_string)
  JSON.parse(json)
rescue JSON::ParserError
  nil
end

#supported_driver?Boolean

Returns:

  • (Boolean)


22
23
24
25
# File 'lib/jscov/collector.rb', line 22

def supported_driver?
  @session.driver.is_a?(Capybara::Selenium::Driver) &&
    @session.driver.browser.respond_to?(:logs)
end