Class: Webdriver::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/webdriver/connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(endpoint) ⇒ Connection

Returns a new instance of Connection.



3
4
5
6
# File 'lib/webdriver/connection.rb', line 3

def initialize endpoint
  uri = URI(endpoint)
  @http = Net::HTTP.new uri.hostname, uri.port
end

Instance Method Details

#call(method, path, headers = {}, body = {}) ⇒ Object



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
59
60
61
62
# File 'lib/webdriver/connection.rb', line 20

def call method, path, headers={}, body={}
  path = "/#{path}"
  body_json = body.to_json if body
  Webdriver.debug [method, path, headers, body_json]

  response = case method
  when :get
    @http.get path
  when :post
    @http.post path, body_json
  when :delete
    @http.delete path, body_json
  else
    raise "err"
  end

  response_body = JSON.parse response.body

  status = response_body.dig "status"
  session_id = response_body.dig "sessionId"
  value = response_body.dig "value"

  case status
  when 0
    # POST /session has different response structure than other calls
    if method == :post && path == "/session"
      value.merge("id" => session_id)
    else # everything else works like this
      value
    end
  when 1..nil
    # 10: stale element reference: element is not attached to the page document
    # 11: element not interactable
    error_message = value.dig("message")
    raise "#{status}: #{error_message}"
  else
    if method == :get && path == "/status"
      value
    else
      raise [:unknown, response_body]
    end
  end
end

#get(path, headers = {}) ⇒ Object



8
9
10
# File 'lib/webdriver/connection.rb', line 8

def get path, headers={}
  call :get, path, headers
end

#post(path, headers = {}, body = nil) ⇒ Object



12
13
14
# File 'lib/webdriver/connection.rb', line 12

def post path, headers={}, body=nil
  call :post, path, headers, body
end