Module: CUKES::FileLibrary

Includes:
DataMagic
Included in:
BrowserSettings, CreateLog, CustomHtmlReport, Read_From_YML, RubyGems
Defined in:
lib/friendly/cukes/framework/library/generic/file_library.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.delete_object(object) ⇒ Object

Description : deletes an object Author : Chandra sekaran Arguments :

object        : object to be deleted


94
95
96
# File 'lib/friendly/cukes/framework/library/generic/file_library.rb', line 94

def self.delete_object(object)
  object = nil
end

Instance Method Details

#close_file(file_object) ⇒ Object

Description : closes the given file object Author : Chandra sekaran Arguments :

file_object    : object of the file to be closed


65
66
67
68
69
70
71
72
# File 'lib/friendly/cukes/framework/library/generic/file_library.rb', line 65

def close_file(file_object)
  file_object.close
  #$log.success("File #{file_object} closed successfully.")
rescue Exception => ex
  #file_object = nil
  $log.error("Error while closing #{file_object} : #{ex}")
  exit
end

#create_directory(str_directory_path) ⇒ Object

Description : creates a new directory under the given path Author : Chandra sekaran Arguments :

str_directory_path : absolute path of directory


79
80
81
82
83
84
85
86
87
# File 'lib/friendly/cukes/framework/library/generic/file_library.rb', line 79

def create_directory(str_directory_path)
  unless File.directory?(str_directory_path)
    FileUtils.mkdir_p(str_directory_path)
  end
  #$log.success("New directory created : #{str_directory_path}")
rescue Exception => ex
  $log.error("Error in creating directory #{str_directory_path} : #{ex}")
  exit
end

#create_html_reportObject

Description : Renames the created html/json report file and moves it to the current log directory Author : Chandra sekaran



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/friendly/cukes/framework/library/generic/file_library.rb', line 127

def create_html_report
  str_timestamp = $log_env.get_formatted_datetime($end_time)
  # rename and move html report file into current test report directory
  File.rename("#{$REPORT_FILE_NAME}.html", "report_#{str_timestamp}.html")
  FileUtils.mv("report_#{str_timestamp}.html", $current_log_dir)
  # rename and move json report file into current test report directory
  File.rename("#{$REPORT_FILE_NAME}.json", "report_#{str_timestamp}.json")
  FileUtils.mv("report_#{str_timestamp}.json", $current_log_dir)
  $log.info("Html/JSON report files created and saved in '#{$current_log_dir}'")
rescue Exception => ex
  $log.error("Error in creating html report : #{ex}")
  exit
end

#execute_command(str_command) ⇒ Object

Description : execute the kernel command Author : Chandra sekaran Arguments :

str_command         : command string

Return value : a boolean value



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/friendly/cukes/framework/library/generic/file_library.rb', line 291

def execute_command(str_command)
  str_stdout, str_stderr = '', ''
  Open3.popen3(str_command) do |i,o,e|
    i.close
    while((line = o.gets))
      str_stdout << line
    end
    while((line = e.gets))
      str_stderr << line
    end
  end
  $log.success("STDOUT : #{str_stdout.strip}") if !(str_stdout.nil? || str_stdout.empty?)
  raise str_stderr.strip if !(str_stderr.nil? || str_stderr.empty?)
  return true
rescue Exception => ex
  $log.error("Error while executing the command (#{str_command}) : #{ex}")
  exit
end

#format_file_path(str_fileabs_path) ⇒ Object

Description : formats the file path by replacing “\” with “/” Author : Chandra sekaran Arguments :

str_fileabs_path : absolute path of file

Return value :

str_file_path    : formatted absolute path of file


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
215
216
217
218
219
220
221
222
223
# File 'lib/friendly/cukes/framework/library/generic/file_library.rb', line 185

def format_file_path(str_fileabs_path)
  str_file_path = str_fileabs_path
  str_file_path.each_char do |letter|     # replace all the escape sequences
    case letter
      when /[\a]/
        str_file_path[letter] = "/a"
      when /[\e]/
        str_file_path[letter] = "/e"
      when /[\b]/
        str_file_path[letter] = "/b"
      when /[\cx]/
        str_file_path[letter] = "/cx"
      when /[\f]/
        str_file_path[letter] = "/f"
      when /[\n]/
        str_file_path[letter] = "/n"
      when /[\nnn]/
        #str_file_path[letter] = "/nnn" # not required as \n is given
      when /[\r]/
        str_file_path[letter] = "/r"
      when /[\s]/
        str_file_path[letter] = "/t"   # it is taking "\t" as "\s"
      when /[\t]/
        str_file_path[letter] = "/t"
      when "\\"
        str_file_path[letter] = "/"
      #when /[\v]/                       #  not required due to expression error
        #str_file_path[letter] = "/v"    #  not required due to expression error
      #when /[\x]/                       #  not required due to expression error
        #str_file_path[letter] = "/x"    #  not required due to expression error
      #when /[\xnn]/                     #  not required due to expression error
        #str_file_path[letter] = "/xnn"  #  not required due to expression error
    end
  end
  return str_file_path
rescue Exception => ex
  $log.error("Error in formatting file path (#{str_file_path}) : #{ex}")
  exit
end

#get_feature_module_name(str_file_path) ⇒ Object

Description : extracts the features module and submodule names from a given file path Author : Chandra sekaran Arguments :

str_file_path : absolute path of the feature file

Return value :

str_feature_dir: string value of module/submodule name


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/friendly/cukes/framework/library/generic/file_library.rb', line 105

def get_feature_module_name(str_file_path)
  str_file_path = format_file_path(str_file_path)
  str_file_path = str_file_path.split("features/").last   # extracts all strings after 'features\'
                                                          #puts str_file_path
  arr_file_dirs = str_file_path.split("/")    # split the strings into array based on '\'
  str_feature_dir = ""

  arr_file_dirs.pop             # removes the last element i.e., feature file
  # arr_file_dirs.pop             # removes the second last element i.e., test case folder
  (0..arr_file_dirs.size - 1).each do |num_counter|
    str_feature_dir << "_" if num_counter > 0
    str_feature_dir << "#{arr_file_dirs[num_counter]}"   # form a new string from array values
  end
  str_feature_dir
rescue Exception => ex
  $log.error("Error in extracting features directory : #{ex}")
  exit
end

#get_files_absolute_path(str_file_path, str_file_type, obj_time_stamp) ⇒ Object

Description : get files which are all created under given file path with extension and after given timestamp Author : Gomathi Arguments :

str_file_path    : absolute path of file
str_file_type    : file extension
obj_time_stamp   : execution start time as time object

Return argument :

arr_files        : array of file path(s)


166
167
168
169
170
171
172
173
174
175
176
# File 'lib/friendly/cukes/framework/library/generic/file_library.rb', line 166

def get_files_absolute_path(str_file_path, str_file_type, obj_time_stamp)
  arr_abs_path = Dir["#{str_file_path}/*/*.#{str_file_type}"]
  arr_files = []
  arr_abs_path.each do |file_path|
    arr_files << file_path if File.ctime(file_path) > obj_time_stamp
  end
  arr_files
rescue Exception => ex
  $log.error("Error in getting '#{str_file_type}' file(s) under '#{str_file_path}' for '#{obj_time_stamp}' : #{ex}")
  exit
end

#is_file_exists(str_file_path) ⇒ Object

Description : checks for file existance and returns the boolean result Author : Chandra sekaran Arguments :

str_file_path   : absolute path of file

Return values :

bool_file       : boolean variable that hold the result of the file existence


20
21
22
23
24
25
26
# File 'lib/friendly/cukes/framework/library/generic/file_library.rb', line 20

def is_file_exists(str_file_path)
  bool_file = File.exists?(str_file_path)#get_absolute_path(str_file_name) )
  bool_file
rescue Exception => ex
  $log.error("Error while checking for existence of #{str_file_path} : #{ex}")
  exit
end

#open_excel_file(str_file_name) ⇒ Object

Description : opens the excel file Author : Chandra sekaran Arguments :

str_file_name   : absolute path of excel file

Return values :

obj_file        : file object of the workbook


50
51
52
53
54
55
56
57
58
# File 'lib/friendly/cukes/framework/library/generic/file_library.rb', line 50

def open_excel_file(str_file_name)
  Spreadsheet.client_encoding = 'UTF-8'
  obj_file = Spreadsheet.open(get_absolute_path(str_file_name))
  $log.success("File #{str_file_name} opened successfully.")
  obj_file
rescue Exception => ex
  $log.error("Error while opening #{str_file_name} : #{ex}")
  exit
end

#open_yml_file(str_file_path) ⇒ Object

Description : opens the yml file Author : Chandra sekaran Arguments :

str_file_path   : absolute path of yml file

Return values :

obj_file        : hash value of the yml file content


35
36
37
38
39
40
41
# File 'lib/friendly/cukes/framework/library/generic/file_library.rb', line 35

def open_yml_file(str_file_path)
  obj_file = YAML.load_file(File.open(str_file_path)) #get_absolute_path(str_file_name))  # obj_file = YAML.load_file(format_filepath(str_file_name))
  obj_file
rescue Exception => ex
  $log.error("Error while opening #{str_file_path} : #{ex}")
  exit
end

#rename_file_type(str_file_path, str_file_type) ⇒ Object

Description : renames all files extension under given file path Author : Chandra sekaran Arguments :

str_file_path    : absolute path of file
str_file_type    : new file extension to be created


147
148
149
150
151
152
153
154
155
# File 'lib/friendly/cukes/framework/library/generic/file_library.rb', line 147

def rename_file_type(str_file_path, str_file_type)
  Dir.glob(str_file_path).each do |file|
    FileUtils.mv file, "#{File.dirname(file)}/#{File.basename(file,'.*')}.#{str_file_type}"
  end
  $log.info("File type(s) under '#{str_file_path}' renamed successfully to '#{str_file_type}'")
rescue Exception => ex
  $log.error("Error in renaming '#{str_file_path}' to type '#{str_file_type}': #{ex}")
  exit
end

#set_datafile_path(str_local_file_name, str_global_file_name) ⇒ Object

Description : sets the directory path for DataMagic to load the data file for the current scenario/step Author : Chandra sekaran Arguments :

str_local_file_name : name of local data file
str_global_file_name: name of global data file

Return value :

actual_file        : name of the required file


256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/friendly/cukes/framework/library/generic/file_library.rb', line 256

def set_datafile_path(str_local_file_name, str_global_file_name)
  str_feature_file_path = format_file_path($str_feature_file_path)
  arr_temp = str_feature_file_path.split("/")
  arr_temp.pop
  arr_temp.push(str_local_file_name)
  str_datafile_dir = arr_temp * "/"
  actual_file = ""
  if File.exists?(str_datafile_dir)
    arr_temp_dir = str_datafile_dir.split('/')
    arr_temp_dir.pop
    @str_temp_dir =  arr_temp_dir * "/"
    DataMagic.yml_directory = @str_temp_dir
    actual_file = str_local_file_name
  else
    arr_temp_dir = str_datafile_dir.split('/')
    arr_temp_dir.pop
    arr_temp_dir.pop
    arr_temp_dir.push("test_data")
    @str_temp_dir =  arr_temp_dir * "/"
    DataMagic.yml_directory = @str_temp_dir
    actual_file = str_global_file_name
  end
  puts "Test data file successfully set to #{@str_temp_dir}"
  return actual_file
rescue Exception => ex
  $log.error("Error in setting data file path (#{str_local_file_name}/#{str_global_file_name}) : #{ex}")
  exit
end

#set_scenario_based_datafile(str_global_file_name) ⇒ Object

Description : extracts local file name and sets the directory path for DataMagic to load the data file for the current scenario/step Author : Chandra sekaran Arguments :

str_datafile_name : name of data file

Return value : hash of the loaded yml file



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/friendly/cukes/framework/library/generic/file_library.rb', line 231

def set_scenario_based_datafile(str_global_file_name)
  str_local_file_name = str_global_file_name
  $scenario_tags.each do |tag|
    if tag.include? SCENARIO_ID_PREFIX.downcase
      tag_name = tag.gsub("@", "_")
      tmp = str_global_file_name.gsub(".", "#{tag_name}.")
      str_local_file_name = tmp
      break
    end
  end
  str_file_name = set_datafile_path(str_local_file_name, str_global_file_name)
  DataMagic.load(str_file_name)   # returns yml content as a hash
rescue Exception => ex
  $log.error("Error in setting scenario based data file for (#{str_global_file_name}) : #{ex}")
  exit
end