Class: MobyUtil::Scripter

Inherits:
Object show all
Defined in:
lib/tdriver/util/recorder/scripter.rb

Instance Method Summary collapse

Constructor Details

#initialize(sut_id, object_identificators) ⇒ Scripter

Returns a new instance of Scripter.



24
25
26
27
28
29
30
31
# File 'lib/tdriver/util/recorder/scripter.rb', line 24

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

Instance Method Details

#write_fragment(xml_as_object, app_name) ⇒ Object



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
# File 'lib/tdriver/util/recorder/scripter.rb', line 33

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