Class: QTestScenario::QTestScenarioPlugin

Inherits:
Object
  • Object
show all
Defined in:
lib/qTestScenarioRuby.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ QTestScenarioPlugin

Returns a new instance of QTestScenarioPlugin.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/qTestScenarioRuby.rb', line 17

def initialize(args)
  args.each do |k, v|
    instance_variable_set("@#{k}", v) unless v.nil?
  end
  if @server.nil? or @api_key.nil? or @destination_dir.nil?
    puts '--------------------------------------------------------------'
    puts 'Must set at least Jira host URI (server), api key (api_key) and destination folder (destination_dir) for feature files'
    puts '--------------------------------------------------------------'
    exit 1
  end
  if @jql.nil? and @keys.nil?
    puts '--------------------------------------------------------------'
    puts 'Must set at least jql or issue key to select feature files'
    puts '--------------------------------------------------------------'
    exit 1
  end

  @setup_is_done = false
  generate_hooks
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



15
16
17
# File 'lib/qTestScenarioRuby.rb', line 15

def api_key
  @api_key
end

#destination_dirObject

Returns the value of attribute destination_dir.



15
16
17
# File 'lib/qTestScenarioRuby.rb', line 15

def destination_dir
  @destination_dir
end

#jqlObject

Returns the value of attribute jql.



15
16
17
# File 'lib/qTestScenarioRuby.rb', line 15

def jql
  @jql
end

#keysObject

Returns the value of attribute keys.



15
16
17
# File 'lib/qTestScenarioRuby.rb', line 15

def keys
  @keys
end

#project_pathObject

Returns the value of attribute project_path.



15
16
17
# File 'lib/qTestScenarioRuby.rb', line 15

def project_path
  @project_path
end

#serverObject

Returns the value of attribute server.



15
16
17
# File 'lib/qTestScenarioRuby.rb', line 15

def server
  @server
end

#support_folderObject

Returns the value of attribute support_folder.



15
16
17
# File 'lib/qTestScenarioRuby.rb', line 15

def support_folder
  @support_folder
end

Instance Method Details

#after_scenario(scenario) ⇒ Object



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
# File 'lib/qTestScenarioRuby.rb', line 151

def after_scenario (scenario)
  feature_meta = @feature_mappings_map[scenario.feature.name]
  feature_log = @feature_logs_map[feature_meta.id]
  scenario_log = get_scenario_log(feature_log.scenarioLogs, feature_meta, scenario)

  if !scenario_log.nil?
    scenario_log.endDate = Time.now.to_ms
    result = ''
    if scenario.failed?
      scenario_log.executionLog = scenario.exception
      result = QspecResult::FAILED
    else
      if 'passed'.eql? scenario.status.to_s
        result = QspecResult::PASSED
      else
        result = QspecResult::UNCOMPLETED
      end
    end

    if !scenario.outline?
      # not scenario outline: update result to scenarioLog

      scenario_log.result = result
      feature_log.addScenarioResult(scenario_log.result)
    else
      # scenario outline: doesn't calculate scenario, defer it until all outline has been executed

      scenario_log.outLineResult.push(result)
      @current_outline_count += 1

      if @current_outline_count.eql? @scenario_id_to_outline_number[scenario_log.scenarioId]
        # put the outline result to scenario log

        scenario_log.result = calculate_result(scenario_log.outLineResult)
        feature_log.addScenarioResult(scenario_log.result)
        @current_outline_count = 0
      end
    end
  end

  if feature_log.done?
    # all scenarios have been executed, updates the qspecFeature log and sends it to scenario server

    feature_log.result = calculate_result(feature_log.resultSet)
    feature_log.endDate = Time.now.to_ms

    QTestScenarioUtils.submit_feature_test_log(feature_log, @server, @api_key)
  end
end

#at_exitObject



197
198
199
200
# File 'lib/qTestScenarioRuby.rb', line 197

def at_exit
  @execution.endDate = Time.now.to_ms
  QTestScenarioUtils.update_execution(@execution, @server, @api_key)
end

#before_scenario(scenario) ⇒ Object



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
# File 'lib/qTestScenarioRuby.rb', line 84

def before_scenario (scenario)

  if (!@setup_is_done)
    @setup_is_done = true
    @feature_mappings_map = QTestScenarioUtils.load_feature_mapping_file(@destination_dir)
    @execution = QTestScenarioUtils.create_execution(@server, @api_key)
    @feature_logs_map = Hash.new
    @scenario_id_to_outline_number = Hash.new
    @current_outline_count = 0
  end

  feature_meta = @feature_mappings_map[scenario.feature.name]
  feature_log = @feature_logs_map[feature_meta.id]
  if feature_log.nil?
    # new feature if not exists

    feature_log = FeatureLog.new
    feature_log.totalScenariosNumber = feature_meta.scenarios.length
    feature_log.featureId = feature_meta.id
    feature_log.versionId = feature_meta.versionId
    feature_log.executionId = @execution.executionId
    feature_log.startDate = Time.now.to_ms
    feature_log.totalScenariosNumber = feature_meta.scenarios.length
    @feature_logs_map[feature_meta.id] = feature_log

    # build outline count for scenarios

    scenario.feature.feature_elements.each { |child|
      if ('Scenario Outline'.eql? child.keyword) || ('scenario outline'.eql? child.keyword)
        outline_example_count = 0
        scenarioId = feature_meta.scenarios[child.name]
        child.examples_tables.each { |table|
          outline_example_count += table.example_rows.length
          table.example_rows.each { |row|
            outLineName = $outline_pattern % [child.name, table.name, row.number]
            feature_meta.scenarios[outLineName] = scenarioId
          }
        }
        @scenario_id_to_outline_number[scenarioId] = outline_example_count
      end
    }
  end
  # get the scenario log, in case Scenario Outline, we will get the same scenarioLog

  scenario_log = get_scenario_log(feature_log.scenarioLogs, feature_meta, scenario)
  scenario_id = feature_meta.scenarios[scenario.name]

  if scenario_log.nil?
    scenario_log = ScenarioLog.new
    scenario_log.featureId = feature_meta.id
    scenario_log.featureName = feature_meta.name
    scenario_log.startDate = Time.now.to_ms
    scenario_log.host = Socket.gethostname
    scenario_log.scenarioName = scenario.name
    scenario_log.scenarioId = scenario_id
    feature_log.addScenarioLog(scenario_log)
  end
end

#calculate_result(results) ⇒ Object



140
141
142
143
144
145
146
147
148
149
# File 'lib/qTestScenarioRuby.rb', line 140

def calculate_result (results)
  if results.include? QspecResult::FAILED
    result = QspecResult::FAILED
  elsif results.include? QspecResult::UNCOMPLETED
    result = QspecResult::UNCOMPLETED
  else
    result = QspecResult::PASSED
  end
  result
end

#generate_hooksObject



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
# File 'lib/qTestScenarioRuby.rb', line 219

def generate_hooks
  support_folder_path = @project_path + '/' + @support_folder + '/hook/'
  if !File.directory? (support_folder_path)
    FileUtils.mkdir(support_folder_path)
  end

  version_file_path = support_folder_path + 'hook_version'
  hook_file_path = support_folder_path + 'hooks.rb'
  generate_hook = generate_hooks?(version_file_path, hook_file_path)

  if (generate_hook)
    File.open(version_file_path, 'wb') do |f|
      f << $hook_version
    end

    File.open(hook_file_path, 'wb') do |f|
      f.puts 'require \'qTestScenarioRuby\''
      f.puts ''
      f.puts '$plugin = QTestScenario::QTestScenarioPlugin.new({'
      f.puts '    \'server\' => ENV[\'server\'],'
      f.puts '    \'api_key\' => ENV[\'api_key\'],'
      f.puts '    \'destination_dir\' => ENV[\'destination_dir\'],'
      f.puts '    \'jql\' => ENV[\'jql\'],'
      f.puts '    \'support_folder\' => ENV[\'support_folder\'],'
      f.puts '    \'project_path\' => Dir.pwd'
      f.puts '    })'
      f.puts ''
      f.puts 'Before do |scenario|'
      f.puts '  $plugin.before_scenario scenario'
      f.puts 'end'
      f.puts ''
      f.puts 'After do |scenario|'
      f.puts '  $plugin.after_scenario scenario'
      f.puts 'end'
      f.puts ''
      f.puts 'at_exit do'
      f.puts '  $plugin.at_exit'
      f.puts 'end'
    end
  end
end

#generate_hooks?(version_file_path, hook_file_path) ⇒ Boolean

Returns:

  • (Boolean)


202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/qTestScenarioRuby.rb', line 202

def generate_hooks? (version_file_path, hook_file_path)
  generate_hook = false
  begin
    File.open(version_file_path, 'r') do |f|
      versionUsed = f.gets
      if !$hook_version.eql? versionUsed
        File.delete(hook_file_path)
        generate_hook = true
      end
    end
  rescue
    # hook_version does not exist, then generate hook

    generate_hook = true
  end
  generate_hook
end

#get_scenario_log(scenariosLog, featureMeta, scenario) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/qTestScenarioRuby.rb', line 74

def get_scenario_log (scenariosLog, featureMeta, scenario)
  scenariosLog.each { |scenarioLog|
    scenarioId = featureMeta.scenarios[scenario.name]
    if scenarioLog.scenarioId.eql? scenarioId
      return scenarioLog
    end
  }
  return nil
end

#prepare_testObject



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
# File 'lib/qTestScenarioRuby.rb', line 38

def prepare_test
  FileUtils.rm_rf(@destination_dir)
  begin
    FileUtils.mkdir(@destination_dir)
  rescue
    # retry if we encounter PermissionDenied exception

    FileUtils.mkdir(@destination_dir)
  end

  response = QTestScenarioUtils.get_feature_files(@keys, @jql, @server, @api_key)
  if response.code === 200
    features = JSON.parse(response.body)
    feature_mapper = Hash.new

    features.each { |feature|
      qspec_feature = QspecFeature.from_json feature

      # Write qspec_feature to file

      write_to_file(@destination_dir, qspec_feature.fileName, qspec_feature.content)

      feature_meta = FeatureMeta.new(qspec_feature.id, qspec_feature.name, qspec_feature.scenarios, qspec_feature.versionId)
      feature_mapper[qspec_feature.name] = feature_meta.to_json
    }

    # Write qspecFeature mapping to file

    write_to_file(@destination_dir, Constants.getMappingFileName, feature_mapper.to_json)
  end
end

#write_to_file(destination_dir, file_name, content) ⇒ Object



67
68
69
70
71
72
# File 'lib/qTestScenarioRuby.rb', line 67

def write_to_file (destination_dir, file_name, content)
  path = destination_dir + file_name
  File.open(path, 'wb') do |f|
    f.puts content
  end
end