-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripter.rb
More file actions
294 lines (207 loc) · 9.16 KB
/
Copy pathscripter.rb
File metadata and controls
294 lines (207 loc) · 9.16 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
############################################################################
##
## Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
## All rights reserved.
## Contact: Nokia Corporation (testabilitydriver@nokia.com)
##
## This file is part of Testability Driver.
##
## If you have questions regarding the use of this file, please contact
## Nokia at testabilitydriver@nokia.com .
##
## This library is free software; you can redistribute it and/or
## modify it under the terms of the GNU Lesser General Public
## License version 2.1 as published by the Free Software Foundation
## and appearing in the file LICENSE.LGPL included in the packaging
## of this file.
##
############################################################################
module MobyUtil
class Scripter
def initialize(sut_id, object_identificators)
@_object_identificators = object_identificators
@_tap_max_time = $parameters[sut_id][:record_tap_time_treshold].to_i
@_tap_min_distance = $parameters[sut_id][:record_move_treshold].to_i
end
def write_fragment(xml_as_object, app_name)
script = "# Insert the script fragment below into your test \n"
script << "# Add verification points if needed. \n \n"
script << "# For testing the script! Do not include in your own test scripts. \n"
script << "@app = sut.application(:name =>'" << app_name << "') \n"
script << "# To test the script make sure the application is in the same state as it was when recording was started. \n\n"
script << "#################################### \n"
script << "# Begin recorded script \n"
script << "#################################### \n \n"
event_list = xml_as_object.events
event_count = event_list.attribute('eventCount').to_i
mouse_down = false
points = Array.new
active_target = nil
scripting = false;
mouse_status = 0
previous_time = nil
event = nil
# mouse_status:
# 0 = no press or release
# 1 = press, no release [ will only happen if recording stoped before mouse release]
# 2 = release, no press [ will only happen if recording started after mouse press]
# 3 = press and release
# COLLECT ALL MOUSE EVENTS
mouse_moves = []
mouse_press_events = []
mouse_release_events = []
begin
mouse_moves = xml_as_object.children(:type =>'event', :name=>'MouseMove')
rescue MobyBase::TestObjectNotFoundError
end
begin
mouse_press_events = xml_as_object.children(:type =>'event', :name=>'MouseButtonPress')
rescue MobyBase::TestObjectNotFoundError
end
begin
mouse_release_events = xml_as_object.children(:type =>'event', :name=>'MouseButtonRelease')
rescue MobyBase::TestObjectNotFoundError
end
# STORE MOVE POINTS
move_points = []
mouse_moves.each do |point|
timestamp = point.attribute('timeStamp').to_i
previous_timestamp = ( move_points[-1].nil? ) ? timestamp : move_points[-1]['timestamp'].to_i
interval = get_duration(previous_timestamp, timestamp)
move_points.push({'x' => point.attribute('windowX'), 'y' => point.attribute('windowY'), 'interval' => interval, 'timestamp' => timestamp, 'id' => point.id} )
end
# STORE RELEASE EVENTS
release_events = []
mouse_release_events.each do |event|
release_events.push({'id' => event.id})
end
# FOREACH MouseButtonPress
mouse_press_events.each_index do |index|
active_target = get_target_details( mouse_press_events[index].child(:id => mouse_press_events[index].id) )
# COLLECT MouseMove points until MouseButtonRelease
# If no more MouseButtonRelease or MouseButtonPress then last MoveMouse point id
first_point_index = mouse_press_events[index].id.to_i
if ( !move_points.empty? )
next_mouse_press_index = ( mouse_press_events[ index + 1 ].nil? ) ? move_points.last['id'].to_i : mouse_press_events[ index + 1 ].id.to_i
next_mouse_release = release_events.select{ |event| event['id'].to_i < next_mouse_press_index }
next_mouse_release_index = next_mouse_release.first['id'].to_i unless next_mouse_release.empty?
last_point_index = ( next_mouse_release_index.nil? ) ? next_mouse_press_index : next_mouse_release_index
else
if ( !release_events.empty? )
next_mouse_press_index = ( mouse_press_events[ index + 1 ].nil? ) ? 0 : mouse_press_events[ index + 1 ].id.to_i
next_mouse_release = ( next_mouse_press_index == 0 ) ? [ release_events.last ] : release_events.select{ |event| event['id'].to_i < next_mouse_press_index }
last_point_index = next_mouse_release.first['id'].to_i unless ( next_mouse_release.nil? or next_mouse_release.empty? )
else
last_point_index = 0
end
end
points = move_points.select{ |point| point['id'].to_i > first_point_index and point['id'].to_i <= last_point_index }
points.first['interval'] = 0.to_f unless points.empty? # set first interval to 0
# PROCESS gesture at MouseButtonRelease unless we hit last point and there is no release
last_processed_event_id = ( move_points.empty? ) ? 0 : move_points.last['id'].to_i
if ( last_point_index != last_processed_event_id )
script << generate_command(active_target, points, mouse_status = 3 ) << "\n"
# END EVENTS, MouseButtonRelease truncated or second press without release witch would not make sense
else
script << generate_command(active_target, points, mouse_status = 1 ) << "\n"
end
end
script << "\n"
script << "#################################### \n"
script << "# End recorded script \n"
script << "#################################### \n"
script
end
private
def add_command( mouse_status, active_target, points, duration )
fragment << fragment
end
def get_target_details(test_object)
target = test_object.type
params = Array.new
params.push(":name=>'#{ test_object.name }'")
if test_object.name.empty?
params.clear
@_object_identificators.each do |attribute|
begin
value = test_object.attribute(attribute)
params.push(":#{ attribute } => '#{ value }'") unless value == ""
rescue MobyBase::AttributeNotFoundError
end
end
end
if params.size > 0
target << "( "
until params.size == 0
target << params.pop
target << ", " if params.size > 0
end
target << " )"
end
target
end
def generate_command(target_details, points, mouse_status)
command = "@app."
if valid_gesture?(points)
command << target_details << ".gesture_points(\n\t[\n"
duration = 0
for i in 0...points.size
value = points[ i ]
command << "\t\t{'x' => " << value[ "x" ].to_s << ", 'y' => " << value[ "y" ].to_s << ", 'interval' => " << value[ "interval" ].to_s << " }"
command << ", \n" unless i == points.size - 1
duration = duration + value[ 'interval' ]
end
command << "\n\t], \n\t" << duration.to_s << ", \n\t"
case mouse_status
when 0
command << "{ :press => false, :release => false }"
when 1
command << "{ :press => true, :release => false }"
when 2
command << "{ :press => false, :release => true }"
when 3
command << "{ :press => true, :release => true }"
end
command << "\n)"
command
elsif mouse_status > 0
duration = 0
points.each{|value| duration = duration + value['interval']}
if mouse_status == 1
command << target_details << ".tap_down"
elsif duration < @_tap_max_time
command << target_details << ".tap"
else
command << target_details << ".long_tap( " << duration.to_s << " )"
end
end
end
def valid_gesture?(points)
return false if points.size < 2
min_x = -1
max_x = -1
min_y = -1
max_y = -1
for i in 0...points.size
value = points[i]
x = value['x'].to_i
y = value['y'].to_i
min_x = x if x < min_x or min_x == -1
max_x = x if x > max_x or max_x == -1
min_y = y if y < min_y or min_y == -1
max_y = y if y > max_y or max_y == -1
end
return false if (max_x - min_x).abs < @_tap_min_distance and (max_y - min_y).abs < @_tap_min_distance
true
end
def get_duration(start_time, end_time)
duration_millis = end_time - start_time
#we want this in second
duration_millis = duration_millis.to_f
duration_secs = duration_millis / 1000
duration_secs
end
# enable hooking for performance measurement & debug logging
TDriver::Hooking.hook_methods( self ) if defined?( TDriver::Hooking )
end # Scripter
end # MobyUtil