Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Watir data storage with yaml. pseudo persistence layer

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.


# for WATIR scripts by marekj testr.us
module TestRun

    #     TestRun.record(__FILE__,{:1, 'Bacon', :2, 'Apple', :3, 'Cheese'})
    #     
    #     writes or appends a yaml doc to file named filename as the file that invoked the method
    #     ---
    #     :1: Bacon
    #     :2: Apple
    #     :3: Cheese
    #     
    #     example: if __FILE__ is c:\foofoo\away\in\windowz\land\scirptname.rb then
    #     creates 'logs' subdir (if it doesn't exist) and appends (or writes a new yaml file)
    #     c:\foofoo\away\in\windowz\land\logs\scirptname.yaml
    # 
    # Dump yaml doc with no particular structure. it can be hash of hashes, array of arrays or
    # whatever you collect during the run. We want an automatic persistence of data and a history of
    # script execution.
    def self.record(scriptFile,run_record)
        $log.debug("Recording: #{run_record.inspect}")
        script_logpath = File.join(File.dirname(scriptFile),'logs')
        script_log = File.join(script_logpath,File.basename(scriptFile,'.rb') << '.yaml')#$log.info this_log
        make_logs_dir(script_log) unless File.exist?(File.expand_path(script_logpath))
        File.open(script_log,'a') {|y| YAML.dump(run_record, y)}
    end
  
    # Make logs subdirectory of the script file being passed
    #  TestRun.make_logs_dir("c:\bla\blabbla\script.rb") creates "c:\bla\blabbla\llogs" subdirectory if there is not one there
    #  TestRun.make_logs_dir(__FILE__) creates "/logs" subdirectory of the current filepath
    # this is needed because we can't write to an empty directory so this should be called when record files are created.
    def self.make_logs_dir(scriptFile)
        require 'fileutils'
        dirPath = File.expand_path(File.dirname(scriptFile))
        FileUtils.mkdir_p(dirPath) #make only if it does not exist
    end

  
    #     TestRun.get_last_record(scriptname)
    # gets last record for the script file execution
    # returns yaml
    def self.get_last_record(scriptFile)
        ydocs = get_records(scriptFile)
        $log.debug("get_last_record: #{ydocs.last.inspect}")
        return ydocs.last
    end
  
    #returns all the records in yaml file for the script file passed
    def self.get_records(scriptFile)
        script_logpath = File.join(File.dirname(scriptFile),'logs')
        $log.info script_logpath
        yf = File.join(script_logpath,File.basename(scriptFile, '.rb') << '.yaml')
        $log.info "Reading Yaml: #{yf.inspect}"
        #read the yaml record
        ydocs = []
        out = File.open(yf,'r')
        YAML.each_document(out) do |yd|
            ydocs << yd
        end
        return ydocs
    end
  
end

You need to create an account or log in to post comments to this site.


Click here to browse all 7285 code snippets