class Opal::CliRunners::Safari

def run

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