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.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/qTestScenarioRuby.rb', line 36

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
  HookUtils.generate_hook(@support_folder, HOOK_VERSION)
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



34
35
36
# File 'lib/qTestScenarioRuby.rb', line 34

def api_key
  @api_key
end

#destination_dirObject

Returns the value of attribute destination_dir.



34
35
36
# File 'lib/qTestScenarioRuby.rb', line 34

def destination_dir
  @destination_dir
end

#jqlObject

Returns the value of attribute jql.



34
35
36
# File 'lib/qTestScenarioRuby.rb', line 34

def jql
  @jql
end

#keysObject

Returns the value of attribute keys.



34
35
36
# File 'lib/qTestScenarioRuby.rb', line 34

def keys
  @keys
end

#serverObject

Returns the value of attribute server.



34
35
36
# File 'lib/qTestScenarioRuby.rb', line 34

def server
  @server
end

#support_folderObject

Returns the value of attribute support_folder.



34
35
36
# File 'lib/qTestScenarioRuby.rb', line 34

def support_folder
  @support_folder
end

Instance Method Details

#after_scenario(scenario) ⇒ Object



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

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



216
217
218
219
# File 'lib/qTestScenarioRuby.rb', line 216

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

#before_scenario(scenario) ⇒ Object



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

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



159
160
161
162
163
164
165
166
167
168
# File 'lib/qTestScenarioRuby.rb', line 159

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

#get_scenario_log(scenariosLog, featureMeta, scenario) ⇒ Object



93
94
95
96
97
98
99
100
101
# File 'lib/qTestScenarioRuby.rb', line 93

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



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

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



86
87
88
89
90
91
# File 'lib/qTestScenarioRuby.rb', line 86

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