-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestdroid_cloud_remote.rb
More file actions
executable file
·174 lines (155 loc) · 4.03 KB
/
testdroid_cloud_remote.rb
File metadata and controls
executable file
·174 lines (155 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/ruby
require 'rubygems'
require 'stomp'
require 'json'
class TestdroidCloudRemote
def initialize(username, password, url, port)
# Instance variables
@username = username
@password = password
@url = url
@port = port
end
#Open connection to remote server
def open
puts "Connecting #{@url}:#{@port}"
@remoteClient = Stomp::Client.new(@username, @password, @url, @port, true)
end
#End session - free to device for other use
def close
send_command("END");
sleep 5
@remoteClient.close
end
# wait until device is available
def wait_for_connection(build_id, device_id, time_out=0)
puts "Waiting for device #{device_id}"
queue_name = "/queue/#{build_id}.REMOTE.#{device_id}"
@remoteClient.subscribe(queue_name, { :ack =>"auto" }, &method(:receiveMsg))
begin
Timeout::timeout(time_out) do
while @cmdDestination.nil? do
sleep 0.3
end
end
rescue Timeout::Error
$stderr.puts "Timeout when waiting device to connect"
return nil
end
end
#Show device connection
def display
puts "Device(#{@deviceId}) is connected: #{@deviceConnected} reply queue: #{@cmdDestination} "
end
#Touch device screen on coordinates
def touch(x,y)
return if !checkConn
send_command("TOUCH #{x}, #{y}");
end
#Drag from position to other with steps and duration
def drag(startx,starty,endx,endy,steps = 10, duration=500)
return if !checkConn
send_command("DRAG #{startx}, #{starty}, #{endx}, #{endy}, #{steps}, #{duration}");
end
#Reboot device
def reboot
return if !checkConn
send_command("REBOOT");
end
#am start -n command: am start -n com.bitbar.testdroid/.BitbarSampleApplicationActivity
def start_activity(activity)
return if !checkConn
send_command("START_ACTIVITY #{activity}");
end
#Get device properties from device
def device_properties()
return if !checkConn
send_command("REQUEST_PROPERTIES")
return get_response
end
#Take screenshot and store into file system
def take_screenshot(filename = "screenshot1.png")
return if !checkConn
@screenshotFilename = filename
send_command("SCREENSHOT")
get_response
end
private
def send_command(monkeyCommand)
@remoteClient.publish(@cmdDestination, monkeyCommand ,{'persistent'=>'false', 'amq-msg-type'=>'text'})
end
def receiveMsg(msg)
if !@cmdDestination.nil?
if !msg.headers["content-length"].nil?
puts "Saving binary message #{@screenshotFilename}"
a_file = File.open(@screenshotFilename, "wb")
a_file.write(msg.body)
a_file.close
@response = @screenshotFilename
return
end
@response = JSON.parse( msg.body )
return;
end
if msg.body =~ /^DEVICE_CONNECTED\s\w*/
@deviceConnected = true
match1 = msg.body.match /^DEVICE_CONNECTED\s(\w*)/
@deviceId = match1[1]
puts "device connected #{match1[1]}"
@cmdDestination = msg.headers["reply-to"]
return
end
end
def checkConn
if @deviceId.nil?
$stderr.puts "Not connected to device"
return false
end
if @cmdDestination.nil?
$stderr.puts "Not connected to device - no reply destination"
return false
end
if @remoteClient.closed?
$stderr.puts "Client is not connected"
return false
end
return true
end
def get_response
begin
# Don't take longer than 20 seconds to retrieve content
Timeout::timeout(20) do
while @response.nil? do
sleep 0.3
end
end
rescue Timeout::Error
$stderr.puts "Timeout when receiving response"
return nil
end
lastResponse = @response.clone
@response = nil
return lastResponse
end
end
if __FILE__ == $0
remote = TestdroidCloudRemote.new('','', 'localhost', 61613)
remote.open
remote.wait_for_connection('12345','016B732C1900701A')
remote.display
dev_prop = remote.device_properties
if dev_prop.nil?
remote.close
abort "Error receiving"
end
puts "X: #{dev_prop['display.height']}"
puts "Y: #{dev_prop['display.width']}"
remote.touch(22,34)
sleep 5
remote.drag(22,134, 323,133)
sleep 5
remote.drag(240,134, 1,134, 9, 200)
remote.take_screenshot("screenshot12.png")
remote.close
puts "End"
end