Class: Opal::CliRunners::Safari

Inherits:
Object
  • Object
show all
Defined in:
opal/lib/opal/cli_runners/safari.rb

Constant Summary collapse

EXECUTION_TIMEOUT =

seconds

600
DEFAULT_SAFARI_DRIVER_HOST =
'localhost'
DEFAULT_SAFARI_DRIVER_PORT =

in addition safari_driver_port + 1 is used for the http server

9444

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Safari

Returns a new instance of Safari.



24
25
26
27
28
29
30
31
32
33
# File 'opal/lib/opal/cli_runners/safari.rb', line 24

def initialize(data)
  argv = data[:argv]
  if argv && argv.any?
    warn "warning: ARGV is not supported by the Safari runner #{argv.inspect}"
  end

  options  = data[:options]
  @output  = options.fetch(:output, $stdout)
  @builder = data[:builder].call
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



35
36
37
# File 'opal/lib/opal/cli_runners/safari.rb', line 35

def builder
  @builder
end

#exit_statusObject (readonly)

Returns the value of attribute exit_status.



35
36
37
# File 'opal/lib/opal/cli_runners/safari.rb', line 35

def exit_status
  @exit_status
end

#outputObject (readonly)

Returns the value of attribute output.



35
36
37
# File 'opal/lib/opal/cli_runners/safari.rb', line 35

def output
  @output
end

Class Method Details

.call(data) ⇒ Object



19
20
21
22
# File 'opal/lib/opal/cli_runners/safari.rb', line 19

def self.call(data)
  runner = new(data)
  runner.run
end

Instance Method Details

#runObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'opal/lib/opal/cli_runners/safari.rb', line 37

def run
  mktmpdir do |dir|
    with_http_server(dir) do |http_port, server_thread|
      with_safari_driver do
        prepare_files_in(dir)

        # Safaridriver commands are very limitied, for supported commands see:
        # https://developer.apple.com/documentation/webkit/macos_webdriver_commands_for_safari_12_and_later
        Net::HTTP.start(safari_driver_host, safari_driver_port) do |con|
          con.read_timeout = EXECUTION_TIMEOUT
          res = con.post('/session', { capabilities: { browserName: 'Safari' } }.to_json, 'Content-Type' => 'application/json')
          session_id = JSON.parse(res.body).dig('value', 'sessionId')
          if session_id
            session_path = "/session/#{session_id}"
            con.post("#{session_path}/url", { url: "http://#{safari_driver_host}:#{http_port}/index.html" }.to_json, 'Content-Type' => 'application/json')
            server_thread.join(EXECUTION_TIMEOUT)
          else
            STDERR.puts "Could not create session: #{res.body}"
          end
        end
        0
      end
    end
  end
end