Class: Cukunity::IOS::UIAutomationMaster

Inherits:
Object
  • Object
show all
Includes:
Utils, Singleton
Defined in:
lib/cukunity/drivers/iOS/uiautomation_master.rb

Constant Summary collapse

DEFAULT_HOSTNAME =
'127.0.0.1'
DEFAULT_PORT =
9927
UIAUTOMATION_SLAVE_DEFAULT_TIMEOUT =
30

Instance Method Summary collapse

Methods included from Utils

#check_timeout, #merge_options, #restrict_options, #to_options, #wait_connectivity

Constructor Details

#initializeUIAutomationMaster

Returns a new instance of UIAutomationMaster.



16
17
18
19
20
21
22
# File 'lib/cukunity/drivers/iOS/uiautomation_master.rb', line 16

def initialize
  @server = nil
  @client = nil
  @client_mutex = Mutex.new
  @client_acquired = ConditionVariable.new
  @client_finished = ConditionVariable.new
end

Instance Method Details

#addressObject



55
56
57
58
# File 'lib/cukunity/drivers/iOS/uiautomation_master.rb', line 55

def address
  listen
  @server.local_address.ip_address
end

#closeObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/cukunity/drivers/iOS/uiautomation_master.rb', line 65

def close
  unless @server.nil?
    @server.close rescue ::Exception
    @server = nil
  end
  unless @client_mutex.nil?
    @client_mutex.synchronize do
      @client_finished.signal
    end
  end
  unless @thread.nil?
    @thread.join
    @thread = nil
  end
end

#command(name, options = {}) ⇒ Object



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
# File 'lib/cukunity/drivers/iOS/uiautomation_master.rb', line 24

def command(name, options = {})
  options = merge_options(options, { :max_time => UIAUTOMATION_SLAVE_DEFAULT_TIMEOUT })
  max_time = options.delete(:max_time)
  listen
  begin
    client = sync_with_client(max_time)
    # write command to client
    req = options.merge({'command' => name})
    client.puts req.to_json
    close_client(client)
    client = nil
    client = wait_client(max_time)
    # wait response from client
    res = JSON.parse(client.readline.chomp)
    if res.nil? or res.has_key?('error')
      raise Exception::UIAutomationSlaveInvalidResponse.new(res['error']) 
    end
    res
  rescue Errno::ECONNRESET
    close_client(client)
    sleep 0.1
    retry
  rescue Errno::EPIPE
    close_client(client)
    sleep 0.1
    retry
  ensure
    close_client(client)
  end
end

#portObject



60
61
62
63
# File 'lib/cukunity/drivers/iOS/uiautomation_master.rb', line 60

def port
  listen
  @server.local_address.ip_port
end