Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
---|---|---|---|---|
support/hooks.rb | 273 | 129 | 89.74%
|
78.29%
|
Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.
2 =begin |
3 *Name : hooks.rb |
4 *Description : hooks definition to perform task pre/post a scenario and/or step execution |
5 *Author : Chandra sekaran |
6 *Creation Date : 24/04/2015 |
7 *Updation Date : |
8 =end |
9 |
10 $log = CUKES::CreateLog.new("app_env") # base log to hold the environment details |
11 |
12 $obj_yml = CUKES::Read_From_YML.new("config/config.yml") # read the config file content |
13 |
14 # for resetting config values |
15 if !RESET_CONFIG_VALUES.nil? |
16 $obj_yml.set_value("environment/parallel_execution_count", 0) # reset parallel execution count |
17 $obj_yml.release_all_profiles # release all unused profiles |
18 end |
19 |
20 # this global variable holds the number of parallel executions (also accounts for single execution) |
21 # and generates consolidated custom html report file(s) |
22 $parallel_execution_count = $obj_yml.get_value("environment/parallel_execution_count") |
23 |
24 # set dynamic profile for a specific box or any free box |
25 if ENV["PROFILE"].downcase == "development" |
26 if BOX.nil? |
27 BOX, PROFILE = $obj_yml.get_any_profile("development") |
28 else |
29 PROFILE = $obj_yml.get_specific_profile("development") |
30 end |
31 elsif ENV["PROFILE"].downcase == "test" |
32 if BOX.nil? |
33 BOX, PROFILE = $obj_yml.get_any_profile("test") |
34 else |
35 PROFILE = $obj_yml.get_specific_profile("test") |
36 end |
37 else |
38 raise "Invalid profile name : #{ENV["PROFILE"]}" |
39 end |
40 |
41 GOOGLE_URL = $obj_yml.get_value("application/#{BOX}/#{PROFILE}/url/google") # get the google url to be launched |
42 RUBYGEMS_URL = $obj_yml.get_value("application/#{BOX}/#{PROFILE}/url/rubygems") # get the rubygems url to be launched |
43 |
44 # Since I have used two different applications, I use two different urls, else this is not required and you can use |
45 # the url present in config.yml file (the environment argument APP is just for demonstration only) |
46 # For the present feature files, if you want to run rubygems feature, kindly add APP=GOOGLE in cucumber run command |
47 # and for google feature add APP=GOOGLE in cucumber run command |
48 if ENV["APP"].nil? |
49 launch_url = GOOGLE_URL |
50 elsif ENV["APP"].downcase.include? "google" |
51 launch_url = GOOGLE_URL |
52 elsif ENV["APP"].downcase.include? "rubygems" |
53 launch_url = RUBYGEMS_URL |
54 end |
55 |
56 # Login credentials |
57 USER_NAME = $obj_yml.get_value("application/#{BOX}/#{PROFILE}/login_credentials/user_name") |
58 PASSWORD = $obj_yml.get_value("application/#{BOX}/#{PROFILE}/login_credentials/password") |
59 |
60 # Application framework setting details |
61 LOGGER_LEVEL = $obj_yml.get_value("environment/logger_level") # get the Logger Level |
62 DATETIME_FORMAT = $obj_yml.get_value("environment/datetime_pattern") # get the datetime format |
63 FEATURE_ID_PREFIX = $obj_yml.get_value("environment/feature_id_prefix") # get the feature id prefix |
64 SCENARIO_ID_PREFIX = $obj_yml.get_value("environment/scenario_id_prefix") # get the scenario id prefix |
65 |
66 # for consolidated html report |
67 NO_OF_PARALLEL_THREADS = $obj_yml.get_value("environment/no_of_parallel_threads") # get the parallel threads count |
68 DELAY_BETWEEN_PARALLEL_THREADS = $obj_yml.get_value("environment/delay_between_parallel_threads") # get the delay between parallel threads |
69 |
70 # Performance report |
71 # the cucumber execution details are extracted from report(s) generated and saved into Sybase database |
72 # below are the database details |
73 PERFORMANCE_REPORT = $obj_yml.get_value("environment/performance_report") |
74 if ["yes", "true"].include?(PERFORMANCE_REPORT.to_s.downcase) |
75 DB_SERVER = $obj_yml.get_value("environment/db_server") |
76 DB_NAME = $obj_yml.get_value("environment/db_name") |
77 DB_USER_NAME = $obj_yml.get_value("environment/db_user_name") |
78 DB_PASSWORD = $obj_yml.get_value("environment/db_password") |
79 end |
80 |
81 # log test execution environment details |
82 $log.info("__________________________________________________________") |
83 $log.info("Test Machine : #{ENV["COMPUTERNAME"]}(#{ENV['OS']})") |
84 $log.info("Test Browser : #{BROWSER}") |
85 $log.info("Test URL : #{launch_url}") # the actual url under test can be given here |
86 $log.info("__________________________________________________________") |
87 |
88 $current_log_dir = $log.get_current_log_dir # global variable to hold the base log directory name |
89 |
90 $start_time = $log.get_current_datetime # start time of the execution |
91 |
92 $log_env = $log # to hold base log file object for log entries after test execution |
93 |
94 $current_log_file = nil # hold the current log file name |
95 |
96 PageObject.default_element_wait = $obj_yml.get_value("environment/default_element_wait") # set default timeout for element wait |
97 PageObject.default_page_wait = $obj_yml.get_value("environment/default_page_wait") # set default timeout for page wait |
98 |
99 $browser = CUKES::BrowserSettings.browser_setup(BROWSER) # launches the browser |
100 |
101 # if you want to launch the URL only once (at the beginning), then you can use the below script |
102 $browser.navigate.to(launch_url) |
103 |
104 $scenario_count = 0 # holds scenario count for each feature file |
105 |
106 # Description : called before the execution of a scenario |
107 # Author : Chandra sekaran |
108 # Arguments : |
109 # scenario : scenario object |
110 # |
111 Before do |scenario| |
112 @step_count = nil # step counter used in AfterStep to get current step name |
113 |
114 @scenario_start_time = $log.get_current_datetime # scenario execution start time |
115 |
116 @browser = $browser # passes the browser object to the page class constructor (implicitly) |
117 @browser.manage.timeouts.implicit_wait = 3 # set 3s implicit wait time |
118 $browser_version = @browser.capabilities[:version] # current browser version |
119 |
120 $world = self # for overriding puts method to print the argument into html file and in console as well |
121 |
122 $str_feature_file_path = scenario.file # absolute path of current feature file |
123 |
124 @str_feature_module_name = $log.get_feature_module_name($str_feature_file_path) # extracts module (and/or submodule) name from str_file_path |
125 if $current_log_file.nil? || !($current_log_file.include? @str_feature_module_name) |
126 $log = CUKES::CreateLog.new(@str_feature_module_name) # creates a new log file with the module ame |
127 $current_log_file = $log.get_current_log_file |
128 end |
129 |
130 $log.info("__________________________________________________________") |
131 |
132 # feature name |
133 case scenario |
134 when Cucumber::Ast::Scenario |
135 @feature_name = scenario.feature.name |
136 when Cucumber::Ast::OutlineTable::ExampleRow |
137 @feature_name = scenario.scenario_outline.feature.name |
138 end |
139 $log.info("Test Feature : " + @feature_name) |
140 |
141 # check for a new feature and set $scenario_count accordingly |
142 if $feature_name_old != @feature_name |
143 $scenario_count = 0 |
144 $feature_name_old = @feature_name |
145 end |
146 |
147 # scenario name |
148 case scenario |
149 when Cucumber::Ast::Scenario |
150 @scenario_name = scenario.name |
151 when Cucumber::Ast::OutlineTable::ExampleRow |
152 @scenario_name = scenario.scenario_outline.name |
153 end |
154 $log.info("Test Scenario : " + @scenario_name) |
155 Kernel.puts("\n [#{Time.now.strftime(DATETIME_FORMAT)}] Currently running scenario : \n #{@scenario_name.to_s}") # for logging in console |
156 |
157 # tags name |
158 $scenario_tags = scenario.source_tag_names |
159 $log.info("Test tag(s) : " + $scenario_tags.to_s) |
160 Kernel.puts("\n \tWith tag(s) : #{$scenario_tags.to_s}") # for logging in console |
161 |
162 # get first step name of first scenario |
163 @current_feature = if scenario.respond_to?('scenario_outline') |
164 # execute the following code only for scenarios outline (starting from the second example) |
165 scenario.scenario_outline.feature |
166 else |
167 # execute the following code only for a scenario and a scenario outline (the first example only) |
168 scenario.feature |
169 end |
170 $log.info("__________________________________________________________") |
171 @arr_steps = [] |
172 @arr_steps = get_steps(@current_feature) # get all steps under the current scenario |
173 end |
174 |
175 # Description : called after the execution of a step |
176 # Author : Chandra sekaran |
177 # Arguments : |
178 # scenario : scenario object |
179 # |
180 AfterStep do |scenario| |
181 sleep 1 |
182 @step_count = 0 if @step_count.nil? # @step_count ||= 0 |
183 |
184 begin |
185 $log.info("Test Step (#{scenario.failed? ? 'failed' : 'success'}) : " + @arr_steps[@step_count]) |
186 rescue Exception => e |
187 $log.error("Error in AfterStep hook for step_count (#{@step_count}): #{e}") |
188 $log.info("Test Step (#{scenario.failed? ? 'failed' : 'success'}) : " + @arr_steps[@step_count-1]) rescue Exception |
189 end |
190 Kernel.puts("\n \t\t[#{Time.now.strftime(DATETIME_FORMAT)}] #{@arr_steps[@step_count].to_s}") rescue Exception # for logging in console |
191 |
192 @step_count += 1 # increase step counter |
193 end |
194 |
195 # Description : called after support has been loaded but before features are loaded |
196 # Author : Chandra sekaran |
197 # Arguments : |
198 # config : config object |
199 # |
200 AfterConfiguration do |config| |
201 |
202 end |
203 |
204 # Description : called after the execution of a scenario |
205 # Author : Chandra sekaran |
206 # Arguments : |
207 # scenario : scenario object |
208 # |
209 After do |scenario| |
210 @step_count = 0 if @step_count.nil? |
211 @scenario_end_time = $log.get_current_datetime # scenario execution finish time |
212 |
213 if scenario.failed? |
214 begin |
215 $log.info("Test Step (failed) : " + @arr_steps[@step_count]) |
216 rescue Exception => e |
217 $log.error("Error in After hook for step_count (#{@step_count}): #{e}") |
218 $log.info("Test Step (failed) : " + @arr_steps[@step_count-1]) rescue Exception |
219 end |
220 str_img_path = CUKES::BrowserSettings.capture_screenshot(@str_feature_module_name) # takes the screenshot of webpage |
221 |
222 # attaches a link to html page, on click of which shows the image in the web page |
223 embed(str_img_path, "image/png", "Click to view screenshot") rescue Exception |
224 end |
225 |
226 $scenario_count += 1 # increment scenario count by 1 |
227 |
228 $log.info("__________________________________________________________") |
229 $log.info("Scenario start time : " + $log.get_formatted_datetime(@scenario_start_time)) |
230 $log.info("Scenario end time : " + $log.get_formatted_datetime(@scenario_end_time)) |
231 $log.info("Total elapsed time : " + $log.get_datetime_diff(@scenario_start_time, @scenario_end_time)) |
232 $log.info("__________________________________________________________") |
233 end |
234 |
235 # Description : called after the execution of all features |
236 # Author : Chandra sekaran |
237 # |
238 at_exit do |
239 $obj_yml.set_value("application/#{BOX}/#{PROFILE}/in_use", "no") # releases the current profile |
240 |
241 $obj_yml.change_execution_count("environment/parallel_execution_count", $obj_yml.get_value("application/#{BOX}/#{PROFILE}/in_use")) |
242 |
243 $end_time = $log_env.get_current_datetime |
244 |
245 # logs into the base log file created with the name 'app_env.log' |
246 $log_env.info("__________________________________________________________") |
247 $log_env.info("Execution start time : " + $log_env.get_formatted_datetime($start_time)) |
248 $log_env.info("Execution end time : " + $log_env.get_formatted_datetime($end_time)) |
249 $log_env.info("Total elapsed time : " + $log_env.get_datetime_diff($start_time, $end_time).to_s) |
250 $log_env.info("__________________________________________________________") |
251 |
252 # rename the html report file and move it to respective log report directory |
253 $log_env.create_html_report |
254 |
255 # creates a new custom HTML report based on cucumber report(s) generated only after complete (single or parallel) execution |
256 if $parallel_execution_count == 0 |
257 # get the html report files of current execution |
258 arr_report_file = $log_env.get_files_absolute_path("test_result", "html", $start_time - $world.get_execution_delay_time.seconds) |
259 $log.info("Report directory names (html): #{arr_report_file.to_s}") |
260 |
261 obj = CUKES::CustomHtmlReport.new(arr_report_file) |
262 obj.create_custom_report |
263 |
264 if ["yes", "true"].include?(PERFORMANCE_REPORT.to_s.downcase) |
265 # get the json report files of current execution |
266 arr_report_file = $log_env.get_files_absolute_path("test_result", "json", $start_time - $world.get_execution_delay_time.seconds) |
267 $log.info("Report directory names (json): #{arr_report_file.to_s}") |
268 |
269 obj = CUKES::PerformanceReport.new(arr_report_file) |
270 obj.create_performance_report |
271 end |
272 end |
273 CUKES::BrowserSettings.quit_browser # closes the current browser |
274 end |
Generated on 2015-05-08 10:40:30 +0530 with SimpleCov-RCov 0.2.3