This is very generica way of writing data to yaml while running watir tests.
it uses convention of storing data for a script named scriptname.rb in logs/scriptname.yaml file.
1
2
3 module TestRun
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 def self.record(scriptFile,run_record)
21 $log.debug("Recording: #{run_record.inspect}")
22 script_logpath = File.join(File.dirname(scriptFile),'logs')
23 script_log = File.join(script_logpath,File.basename(scriptFile,'.rb') << '.yaml')
24 make_logs_dir(script_log) unless File.exist?(File.expand_path(script_logpath))
25 File.open(script_log,'a') {|y| YAML.dump(run_record, y)}
26 end
27
28
29
30
31
32 def self.make_logs_dir(scriptFile)
33 require 'fileutils'
34 dirPath = File.expand_path(File.dirname(scriptFile))
35 FileUtils.mkdir_p(dirPath)
36 end
37
38
39
40
41
42 def self.get_last_record(scriptFile)
43 ydocs = get_records(scriptFile)
44 $log.debug("get_last_record: #{ydocs.last.inspect}")
45 return ydocs.last
46 end
47
48
49 def self.get_records(scriptFile)
50 script_logpath = File.join(File.dirname(scriptFile),'logs')
51 $log.info script_logpath
52 yf = File.join(script_logpath,File.basename(scriptFile, '.rb') << '.yaml')
53 $log.info "Reading Yaml: #{yf.inspect}"
54
55 ydocs = []
56 out = File.open(yf,'r')
57 YAML.each_document(out) do |yd|
58 ydocs << yd
59 end
60 return ydocs
61 end
62
63 end