Class: Jasmine::Runners::ChromeHeadless

Inherits:
Object
  • Object
show all
Defined in:
lib/jasmine/runners/chrome_headless.rb

Instance Method Summary collapse

Constructor Details

#initialize(formatter, jasmine_server_url, config) ⇒ ChromeHeadless

Returns a new instance of ChromeHeadless.



7
8
9
10
11
12
13
14
# File 'lib/jasmine/runners/chrome_headless.rb', line 7

def initialize(formatter, jasmine_server_url, config)
  @formatter = formatter
  @jasmine_server_url = jasmine_server_url
  @config = config
  @show_console_log = @config.show_console_log
  @show_full_stack_trace = @config.show_full_stack_trace
  @cli_options = @config.chrome_cli_options || {}
end

Instance Method Details

#boot_jsObject



118
119
120
# File 'lib/jasmine/runners/chrome_headless.rb', line 118

def boot_js
  File.expand_path('chromeheadless_boot.js', File.dirname(__FILE__))
end

#chrome_binaryObject



59
60
61
# File 'lib/jasmine/runners/chrome_headless.rb', line 59

def chrome_binary
  config.chrome_binary || find_chrome_binary
end

#cli_options_stringObject



75
76
77
78
79
# File 'lib/jasmine/runners/chrome_headless.rb', line 75

def cli_options_string
  @cli_options.
      map {|(k, v)| if v then "--#{k}=#{v}" else "--#{k}" end }.
      join(' ')
end

#find_chrome_binaryObject



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/jasmine/runners/chrome_headless.rb', line 63

def find_chrome_binary
  path = [
    "/usr/bin/google-chrome",
    "/usr/bin/google-chrome-stable",
    "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
  ].detect { |path|
    File.file?(path)
  }
  raise "No Chrome binary found" if path.nil?
  path
end

#runObject



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
# File 'lib/jasmine/runners/chrome_headless.rb', line 16

def run
  chrome_server = IO.popen("\"#{chrome_binary}\" #{cli_options_string}")
  wait_for_chrome_to_start_debug_socket

  begin
    require "chrome_remote"
  rescue LoadError => e
    raise 'Add "chrome_remote" you your Gemfile. To use chromeheadless we require this gem.'
  end

  chrome = wait_for { ChromeRemote.client }
  chrome.send_cmd "Runtime.enable"
  chrome.send_cmd "Page.navigate", url: jasmine_server_url
  result_recived = false
  run_details = { 'random' => false }
  chrome.on "Runtime.consoleAPICalled" do |params|
    if params["type"] == "log"
      if params["args"][0] && params["args"][0]["value"] == "jasmine_spec_result"
        results = JSON.parse(params["args"][1]["value"], :max_nesting => false)
                      .map { |r| Result.new(r.merge!("show_full_stack_trace" => @show_full_stack_trace)) }
        formatter.format(results)
      elsif params["args"][0] && params["args"][0]["value"] == "jasmine_suite_result"
        results = JSON.parse(params["args"][1]["value"], :max_nesting => false)
                      .map { |r| Result.new(r.merge!("show_full_stack_trace" => @show_full_stack_trace)) }
        failures = results.select(&:failed?)
        if failures.any?
          formatter.format(failures)
        end
      elsif params["args"][0] && params["args"][0]["value"] == "jasmine_done"
        result_recived = true
        run_details = JSON.parse(params["args"][1]["value"], :max_nesting => false)
      elsif show_console_log
        puts params["args"].map { |e| e["value"] }.join(' ')
      end
    end
  end

  chrome.listen_until {|msg| result_recived }
  formatter.done(run_details)
  chrome.send_cmd "Browser.close"
  Process.kill("INT", chrome_server.pid)
end

#try_toObject



97
98
99
100
101
# File 'lib/jasmine/runners/chrome_headless.rb', line 97

def try_to
  yield
rescue
  nil
end

#wait_forObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/jasmine/runners/chrome_headless.rb', line 81

def wait_for


  puts "new logic"


  time = Time.now.to_i
  result = try_to { yield }

  while !result && Time.now.to_i - time < config.chrome_startup_timeout
    sleep(0.1)
    result = try_to { yield }
  end
  result
end

#wait_for_chrome_to_start_debug_socketObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/jasmine/runners/chrome_headless.rb', line 103

def wait_for_chrome_to_start_debug_socket
  open_socket = -> do
    begin
      conn = TCPSocket.new('localhost', 9222)
      conn.close
      true
    rescue
      nil
    end
  end

  message = "Chrome didn't seem to start the webSocketDebugger at port: 9222, timeout #{config.chrome_startup_timeout}sec"
  raise message unless wait_for(&open_socket)
end