cucumber-formatter 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/cucumber_formatter.rb +192 -0
  3. metadata +59 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6e476c346672c51f036ddf89abf4632d9d27579a
4
+ data.tar.gz: 58184c841540ac038ea79602b0a84e0aa27e97c4
5
+ SHA512:
6
+ metadata.gz: 7af94bb19f29b63edc9cc7ea3a9dd17f217530d26e8bb6ce9518de3f2da52f896a85898db22eb344f30211fc759c4112d15c60632756ee69af72eea12e107bcf
7
+ data.tar.gz: 5ea75aab1eb0be1d22cadd4f6a66b1ce4170a4cbe6b170ae367666e7ae46263f37a4fe6c1bc70e01a51b24c16e6b79f343c7087d3819a20e12e5f0eee316d04d
@@ -0,0 +1,192 @@
1
+ =begin
2
+ Copyright (c) 2015, Salesforce.com, Inc.
3
+ All rights reserved.
4
+
5
+ Redistribution and use in source and binary forms, with or without modification,
6
+ are permitted provided that the following conditions are met:
7
+
8
+ * Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ * Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation and/or
13
+ other materials provided with the distribution.
14
+
15
+ * Neither the name of Salesforce.com nor the names of its contributors may be
16
+ used to endorse or promote products derived from this software without specific
17
+ prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
+ IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23
+ INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24
+ BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27
+ OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28
+ OF THE POSSIBILITY OF SUCH DAMAGE.
29
+ =end
30
+
31
+ class CucumberFormatter
32
+
33
+ def initialize(step_mother, io, options)
34
+ puts "Initializing CucumberMetrics ...\n\n\n"
35
+ end
36
+
37
+ def before_feature_element(scenario)
38
+ @scenario = scenario
39
+ @scenario_time_start = Time.now
40
+
41
+ @start_time = Time.now
42
+ @end_time = Time.now
43
+ machine_name = Socket.gethostname
44
+
45
+ @dbm ||= dbm
46
+
47
+ if scenario == nil
48
+ fail "The scenario did not run"
49
+ end
50
+
51
+ get_scenario_id(scenario)
52
+ get_environment_id
53
+ get_browser_id
54
+
55
+ # save the test run
56
+ sql = "INSERT INTO scenario_test_runs (scenario_id, test_run_at, test_environment_id,
57
+ browser_id, machine_name, created_at, updated_at)
58
+ VALUES (#{@scenario_id}, now(), #{@env_id}, #{@browser_id}, \'#{machine_name}\', now(), now())"
59
+ @dbm.query(sql)
60
+ @str_id = @dbm.last_id
61
+
62
+ # extract and save the tags
63
+ tags = extract_tags(scenario)
64
+ tags.each do |t|
65
+ sql = "INSERT INTO scenario_tags (scenario_test_run_id, tag_name, created_at, updated_at)
66
+ VALUES (#{@str_id}, \'#{t}\', now(), now())"
67
+ @dbm.query(sql)
68
+ end
69
+ end
70
+
71
+ #save the step
72
+ def after_step(step)
73
+ step_name = get_step_name(@scenario).strip[0..255].gsub('\'', '')
74
+ @start_time = @end_time
75
+ @end_time = Time.now
76
+
77
+ sql = "INSERT INTO scenario_steps (scenario_test_run_id, name, elapsed_time, created_at, updated_at)
78
+ VALUES (#{@str_id}, \'#{step_name}\', #{(@end_time - @start_time).round}, now(), now())"
79
+ @dbm.query sql
80
+ end
81
+
82
+ #finish saving the test run
83
+ def after_feature_element(scenario)
84
+ scenario.failed? ? passed = 0 : passed = 1
85
+
86
+ sql = "UPDATE scenario_test_runs SET elapsed_time = #{Time.now - @scenario_time_start}, passed = #{passed},
87
+ updated_at = now() WHERE id = #{@str_id}"
88
+ @dbm.query sql
89
+
90
+ if scenario.failed?
91
+ save_links(scenario)
92
+ end
93
+ #reset step counter when scenario is finished
94
+ @step_counter = 0
95
+ end
96
+
97
+ private
98
+
99
+ # get the step name; keep track of the counter through the scenario
100
+ def get_step_name(scenario)
101
+ @step_counter ||= 0
102
+ steps = scenario.instance_eval {@steps}
103
+ step_name = (steps.instance_eval {@steps})[@step_counter]
104
+ @step_counter += 1
105
+ return step_name.name
106
+ end
107
+
108
+ def get_scenario_id(scenario)
109
+ # make sure the scenario title will fit in the database
110
+ trimmed_scenario_title = scenario.title.strip[0...255]
111
+ trimmed_scenario_title = trimmed_scenario_title.gsub('\'', '')
112
+
113
+ @scenario_id = 0
114
+ sql = "SELECT id FROM scenarios WHERE scenario_name LIKE \'#{trimmed_scenario_title}\'"
115
+ results = @dbm.query(sql)
116
+ results.each do |r|
117
+ @scenario_id = r["id"]
118
+ end
119
+
120
+ # if the scenario isn't in the database, then we need to save it and get its ID
121
+ if @scenario_id == 0
122
+ sql = "INSERT INTO scenarios (scenario_name, created_at, updated_at)
123
+ VALUES (\'#{trimmed_scenario_title}\', now(), now())"
124
+ @dbm.query sql
125
+
126
+ @scenario_id = @dbm.last_id
127
+ end
128
+ end
129
+
130
+ def get_environment_id
131
+ @env_id = 0
132
+ sql = "SELECT id FROM test_environments WHERE env_name like \'#{TESTENV}\'"
133
+ results = @dbm.query(sql)
134
+ results.each do |r|
135
+ @env_id = r["id"]
136
+ end
137
+
138
+ if @env_id == 0
139
+ sql = "INSERT INTO test_environments (env_name, created_at, updated_at) VALUES(\'#{TESTENV}\', now(), now())"
140
+ @dbm.query sql
141
+
142
+ @env_id = @dbm.last_id
143
+ end
144
+ end
145
+
146
+ def get_browser_id
147
+ @browser_id = 0
148
+ sql = "SELECT id FROM browsers WHERE browser_name like \'#{BROWSER}\'"
149
+ results = @dbm.query(sql)
150
+ results.each do |r|
151
+ @browser_id = r["id"]
152
+ end
153
+
154
+ if @browser_id == 0
155
+ sql = "INSERT INTO browsers (browser_name, created_at, updated_at) VALUES(\'#{BROWSER}\', now(), now())"
156
+ @dbm.query sql
157
+
158
+ @browser_id = @dbm.last_id
159
+ end
160
+ end
161
+
162
+ # save the links for failed scenarios; feature file, scenario, and failed step
163
+ def save_links(scenario)
164
+ failed_step = ""
165
+ ((scenario.instance_eval {@steps}).instance_eval {@steps}).each do |step|
166
+ if step.status == :failed
167
+ failed_step = step.name
168
+ end
169
+ end
170
+
171
+ sql = "INSERT INTO scenario_failed_links (scenario_test_run_id, failed_step,
172
+ feature, scenario, scenario_file, created_at, updated_at)
173
+ VALUES(#{@str_id}, \'#{failed_step.gsub('\'', '')}\', \'#{scenario.feature.title.gsub('\'', '')}\',
174
+ \'#{scenario.title.gsub('\'', '')}\', \'#{scenario.feature.file.gsub('\'', '')}\', now(), now())"
175
+
176
+ @dbm.query(sql)
177
+ end
178
+
179
+ # connect to database with credentials in metrics yml file
180
+ def dbm
181
+ database = OpenStruct.new(YAML.load_file(METRICS_CONFIG_FILE))
182
+ Mysql2::Client.new(:host => database.metrics_db['host'],
183
+ :username => database.metrics_db['username'],
184
+ :password => database.metrics_db['password'],
185
+ :database => database.metrics_db['database'])
186
+ end
187
+
188
+ def extract_tags(scenario)
189
+ scenario.source_tag_names
190
+ end
191
+
192
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cucumber-formatter
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Eric Hartill
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-03-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mysql2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Save data collected from Cucumber test to the database
28
+ email: ehartill@salesforce.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/cucumber_formatter.rb
34
+ homepage: https://github.com/SalesforceEng/cucumber-metrics
35
+ licenses:
36
+ - BSD 3-clause
37
+ metadata: {}
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ requirements: []
53
+ rubyforge_project:
54
+ rubygems_version: 2.4.3
55
+ signing_key:
56
+ specification_version: 4
57
+ summary: Collect metrics from Cucumber tests
58
+ test_files: []
59
+ has_rdoc: