Class: Browser

Inherits:
Object
  • Object
show all
Includes:
Test::Unit::Assertions
Defined in:
lib/rwda.rb

Instance Method Summary collapse

Constructor Details

#initialize(enable_debugger = false) ⇒ Browser

Returns a new instance of Browser.



9
10
11
# File 'lib/rwda.rb', line 9

def initialize (enable_debugger = false)
  @enable_debugger = enable_debugger
end

Instance Method Details

#background_appObject



175
176
177
178
179
# File 'lib/rwda.rb', line 175

def background_app
  url = @url.gsub('/session', '')
  request_route = url+'/wda/homescreen'
  wait_for_response(request_route, 'post', '', lambda { |val| return val.to_s.include? @session_id })
end

#clean_keychain(device) ⇒ Object



76
77
78
# File 'lib/rwda.rb', line 76

def clean_keychain(device)
  system(COMMAND_PATH + ' ' + DEVICE_INFO[device] +' clear_keychain ' + BUNDLE_ID)
end

#find_elements_by_class(class_val, label = 'null', recall = false, exception = 'Element was not found!') ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rwda.rb', line 88

def find_elements_by_class(class_val, label='null', recall=false, exception='Element was not found!')
  wait_for_network_connection_done
  body = {:using => 'class name', :value => class_val}
  request_route = @url+'/'+@session_id+'/elements'
  if recall
    response = wait_for_response(request_route, 'post', body, lambda { |val| return (val.include? class_val) && (val.include? label) })
  else
    response = send_request(request_route, 'post', body)
  end

  response = JSON.parse(response)
  p "Finding elements by class. The response is #{response}" if @enable_debugger

  if label == 'null'
    response = response['value'].find_all { |item| item['label'] == nil }
  else
    response = response['value'].find_all { |item| item['label'] == label }
  end

  response
end

#find_elements_by_xpath(xpath, recall = false, exception = 'Element was not found!') ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/rwda.rb', line 110

def find_elements_by_xpath(xpath, recall=false, exception='Element was not found!')
  wait_for_network_connection_done
  body = {:using => 'xpath', :value => xpath}
  request_route = @url+'/'+@session_id+'/elements'
  if recall
    response = wait_for_response(request_route, 'post', body, lambda { |val| return (JSON.parse(val)['value'].length > 0) && !(val.include? 'Cannot evaluate results for XPath expression') })
  else
    response = send_request(request_route, 'post', body)
  end
  response = JSON.parse(response)
  p "Finding elements by xpath. The response is #{response}" if @enable_debugger
  response = response['value']
  raise exception if response == nil
  response
end

#get_element_rect(element_id) ⇒ Object



170
171
172
173
# File 'lib/rwda.rb', line 170

def get_element_rect(element_id)
  request_route = @url+'/'+@session_id+'/element/'+element_id+'/attribute/rect'
  wait_for_response(request_route, 'get', '', lambda { |val| return val.to_s.include? @session_id })
end

#get_element_value(element_id) ⇒ Object



165
166
167
168
# File 'lib/rwda.rb', line 165

def get_element_value(element_id)
  request_route = @url+'/'+@session_id+'/element/'+element_id+'/attribute/value'
  wait_for_response(request_route, 'get', '', lambda { |val| return val.to_s.include? @session_id })
end

#install_app(device) ⇒ Object



80
81
82
# File 'lib/rwda.rb', line 80

def install_app(device)
  system(COMMAND_PATH + ' ' + DEVICE_INFO[device] +' install ' + APP_PATH)
end

#open_app(device, bundle_id) ⇒ Object



69
70
71
72
73
74
# File 'lib/rwda.rb', line 69

def open_app(device, bundle_id)
  app_body = {:desiredCapabilities => {:bundleId => bundle_id}}
  response = HTTParty.post(DEVICE_URL[device], :body => app_body.to_json)
  @url = DEVICE_URL[device]
  @session_id = response['sessionId']
end

#scroll_to_element(name, element_id) ⇒ Object



145
146
147
148
149
# File 'lib/rwda.rb', line 145

def scroll_to_element(name, element_id)
  request_route = @url+'/'+@session_id+'/wda/Element/'+element_id+'/scroll'
  body = {:name => name}
  send_request(request_route, 'post', body)
end

#send_request(request_route, request_type, body) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rwda.rb', line 46

def send_request(request_route, request_type, body)
  case request_type
    when 'post'
      val = HTTParty.post(request_route, :body => body.to_json)
    when 'get'
      val = HTTParty.get(request_route)
    when 'delete'
      val = HTTParty.delete(request_route, :body => body.to_json)
  end
  p "The sending request is #{val}" if @enable_debugger
  return val.to_json
end

#swipe_element_with_direction(element_id, direction) ⇒ Object



159
160
161
162
163
# File 'lib/rwda.rb', line 159

def swipe_element_with_direction(element_id, direction)
  request_route = @url+'/'+@session_id+'/wda/element/'+element_id+'/swipe'
  body = {:direction => direction}
  send_request(request_route, 'post', body)
end

#take_screenshot(to_file: './screenshot.png') ⇒ Object



151
152
153
154
155
156
157
# File 'lib/rwda.rb', line 151

def take_screenshot(to_file: './screenshot.png')
  request_route = @url+'/screenshot'
  response = wait_for_response(request_route, 'get', '', lambda { |val| return val.to_s.include? @session_id })
  File.write to_file, Base64.decode64(response['value'])
  response['output'] = to_file
  response
end

#tap_element(element_id) ⇒ Object



126
127
128
129
# File 'lib/rwda.rb', line 126

def tap_element(element_id)
  request_route = @url+'/'+@session_id+'/element/'+element_id+'/click'
  send_request(request_route, 'post', '')
end

#type_text(element_id, val) ⇒ Object



131
132
133
134
135
136
137
# File 'lib/rwda.rb', line 131

def type_text(element_id, val)
  request_route_clear = @url+'/'+@session_id+'/element/'+element_id+'/clear'
  send_request(request_route_clear, 'post', '')
  request_route_type = @url+'/'+@session_id+'/element/'+element_id+'/value'
  body = {:value => val}
  send_request(request_route_type, 'post', body)
end

#type_text_without_clear(element_id, val) ⇒ Object



139
140
141
142
143
# File 'lib/rwda.rb', line 139

def type_text_without_clear(element_id, val)
  request_route_type = @url+'/'+@session_id+'/element/'+element_id+'/value'
  body = {:value => val}
  send_request(request_route_type, 'post', body)
end

#uninstall_app(device) ⇒ Object



84
85
86
# File 'lib/rwda.rb', line 84

def uninstall_app(device)
  system(COMMAND_PATH + ' ' + DEVICE_INFO[device] +' uninstall ' + BUNDLE_ID)
end

#wait_element_display(element) ⇒ Object



64
65
66
67
# File 'lib/rwda.rb', line 64

def wait_element_display(element)
  request_route = @url+'/'+@session_id+'/element/'+element+'/displayed'
  wait_for_response(request_route, 'get', '', lambda { |val| return val.to_json.include? 'true' })
end

#wait_element_enable(element) ⇒ Object



59
60
61
62
# File 'lib/rwda.rb', line 59

def wait_element_enable(element)
  request_route = @url+'/'+@session_id+'/element/'+element+'/enabled'
  wait_for_response(request_route, 'get', '', lambda { |val| return val.to_json.include? 'true' })
end

#wait_for_network_connection_done(timeout = 30) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rwda.rb', line 31

def wait_for_network_connection_done(timeout=30)
  request_route = @url+'/'+@session_id+'/elements'
  body = {:using => 'xpath', :value => "XCUIElementTypeOther[@label='Network connection in progress']"}
  sleep 1
  start = Time.now
  while Time.now - start < timeout
    val = HTTParty.post(request_route, :body => body.to_json)
    if JSON.parse(val.to_json)['value'].size == 0
      return
    end
    sleep 1
  end
  sleep 1
end

#wait_for_response(request_route, request_type, body, lamb, timeout = 10) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rwda.rb', line 13

def wait_for_response(request_route, request_type, body, lamb, timeout=10)
  start = Time.now
  while Time.now - start < timeout
    case request_type
      when 'post'
        val = HTTParty.post(request_route, :body => body.to_json)
      when 'get'
        val = HTTParty.get(request_route)
      when 'delete'
        val = HTTParty.delete(request_route, :body => body.to_json)
    end
    if lamb.call(val.to_json)
      return val.to_json
    end
    sleep 1
  end
end