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

About this user

marekj http://marekj.com/

« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS 

watir element examine all attributes

// description of your code here
patch to watir for examining all attributes of an element
   1  
   2  module Watir
   3    class Element
   4      # for watir element returns array of arrays where each element is a [name, value] as long as value is other than null or blank
   5      def get_attributes
   6        attrs = []
   7        self.document.attributes.each do |atr|
   8          k= []
   9          next if (atr.value == 'null') || (atr.value == '')
  10          k << atr.name << atr.value
  11          attrs << k
  12        end
  13        return attrs.sort
  14      end
  15    end
  16  end
  17  

Separate Test Models and add Behaviours at runtime in Watir Tests

// description of your code here

   1  
   2  
   3  module DefaultBehavior
   4    def wag
   5      puts 'default wag left to right'
   6    end
   7    def bark
   8      puts 'woof woof default bark'
   9    end
  10  end
  11  
  12  module SpecialBehavior
  13    include DefaultBehavior
  14    def bark
  15      puts 'raah raah special bark'
  16    end
  17  end
  18  
  19  class DogAsTestModel
  20    include DefaultBehavior
  21  end
  22  
  23  DogAsTestModel.new.wag 
  24  DogAsTestModel.new.bark
  25  
  26  class DogAsTestModel
  27    include SpecialBehavior
  28  end
  29  
  30  DogAsTestModel.new.wag 
  31  DogAsTestModel.new.bark
  32  
  33  class DogAsTestModel
  34    include SpecialBehavior
  35    def bark
  36      puts 'overwrite bark. Woooooof Woooooof'
  37    end
  38  end
  39  
  40  
  41  DogAsTestModel.new.wag 
  42  DogAsTestModel.new.bark
  43  

Watir Logging

// setup logging infrastructure for Watir tests using ruby Logger class

   1  
   2  #Logging infrastructure
   3  require 'logger'
   4  # hack ruby logger to format and output only the minimum needed for testing scripts
   5  # Logger is configured to write 
   6  class Logger
   7    # hack format of logger
   8    class Formatter
   9      #original: Format = "%s, [%s#%d] %5s -- %s: %s\n"
  10      #Change the name of constant to avoid redefining the super which bugs us a bit. So let it be Formato then.
  11      Formato = "[%s] [%5s] : %s\n" 
  12      
  13      #keep the original signature but alter implementation to change formatting
  14      def call(severity, time, progname, msg)
  15        #add logging to stdout
  16        puts output_message = msg2str(msg)
  17        STDOUT.flush
  18        #original: Format % [severity[0..0], format_datetime(time), $$, severity, progname, msg2str(msg)]
  19        Formato % [format_datetime(time),severity, output_message]
  20      end
  21    end
  22  end
  23  
  24  logfile = File.join(File.dirname(__FILE__),'..','log', 'logger.log')
  25  $log = Logger.new(logfile,'daily')
  26  $log.level = Logger::DEBUG
  27  $log.datetime_format = "%H:%M:%S"
  28  $log.info("Watir Execution Starts")
  29  $log.debug("Halllow Worled!")
  30  $log.info("Achtung Achtung. The Train will leave from platform 9")
  31  $log.warn("no no no no no. you can't do this")
  32  $log.error("error error Danger Danger")
  33  $log.fatal("game over game over game over")
  34  # example of output
  35  #[13:02:00] [DEBUG] : Watir Execution Starts
  36  #[13:02:00] [DEBUG] : Halllow Worled!
  37  #[13:02:00] [ INFO] : Achtung Achtung. The Train will leave from platform 9
  38  #[13:02:00] [ WARN] : no no no no no. you can't do this
  39  #[13:02:00] [ERROR] : error error Danger Danger
  40  #[13:02:00] [FATAL] : game over game over game over
  41  
  42  # alternative is to wrap a method around this thing and avoid the global $log which would be cleaner
  43  
  44  def log
  45    @log |= Logger.new(logfile, 'daily')
  46    # make the method in scope 'main'. it can live in a Module but do include the Module in main
  47  end
  48  
  49  

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.


   1  
   2  # for WATIR scripts by marekj testr.us
   3  module TestRun
   4  
   5      #     TestRun.record(__FILE__,{:1, 'Bacon', :2, 'Apple', :3, 'Cheese'})
   6      #     
   7      #     writes or appends a yaml doc to file named filename as the file that invoked the method
   8      #     ---
   9      #     :1: Bacon
  10      #     :2: Apple
  11      #     :3: Cheese
  12      #     
  13      #     example: if __FILE__ is c:\foofoo\away\in\windowz\land\scirptname.rb then
  14      #     creates 'logs' subdir (if it doesn't exist) and appends (or writes a new yaml file)
  15      #     c:\foofoo\away\in\windowz\land\logs\scirptname.yaml
  16      # 
  17      # Dump yaml doc with no particular structure. it can be hash of hashes, array of arrays or
  18      # whatever you collect during the run. We want an automatic persistence of data and a history of
  19      # script execution.
  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')#$log.info this_log
  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      # Make logs subdirectory of the script file being passed
  29      #  TestRun.make_logs_dir("c:\bla\blabbla\script.rb") creates "c:\bla\blabbla\llogs" subdirectory if there is not one there
  30      #  TestRun.make_logs_dir(__FILE__) creates "/logs" subdirectory of the current filepath
  31      # this is needed because we can't write to an empty directory so this should be called when record files are created.
  32      def self.make_logs_dir(scriptFile)
  33          require 'fileutils'
  34          dirPath = File.expand_path(File.dirname(scriptFile))
  35          FileUtils.mkdir_p(dirPath) #make only if it does not exist
  36      end
  37  
  38    
  39      #     TestRun.get_last_record(scriptname)
  40      # gets last record for the script file execution
  41      # returns yaml
  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      #returns all the records in yaml file for the script file passed
  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          #read the yaml record
  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
« Newer Snippets
Older Snippets »
Showing 1-4 of 4 total  RSS