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

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

Separate Test Models and add Behaviours at runtime in Watir Tests

// description of your code here


module DefaultBehavior
  def wag
    puts 'default wag left to right'
  end
  def bark
    puts 'woof woof default bark'
  end
end

module SpecialBehavior
  include DefaultBehavior
  def bark
    puts 'raah raah special bark'
  end
end

class DogAsTestModel
  include DefaultBehavior
end

DogAsTestModel.new.wag 
DogAsTestModel.new.bark

class DogAsTestModel
  include SpecialBehavior
end

DogAsTestModel.new.wag 
DogAsTestModel.new.bark

class DogAsTestModel
  include SpecialBehavior
  def bark
    puts 'overwrite bark. Woooooof Woooooof'
  end
end


DogAsTestModel.new.wag 
DogAsTestModel.new.bark

Watir Logging

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

#Logging infrastructure
require 'logger'
# hack ruby logger to format and output only the minimum needed for testing scripts
# Logger is configured to write 
class Logger
  # hack format of logger
  class Formatter
    #original: Format = "%s, [%s#%d] %5s -- %s: %s\n"
    #Change the name of constant to avoid redefining the super which raises exception and bugs us.
    Formato = "[%s] [%5s] : %s\n" 
    
    #keep the original signature but alter implementation to change formatting
    def call(severity, time, progname, msg)
      #add logging to stdout
      puts output_message = msg2str(msg)
      STDOUT.flush
      #original: Format % [severity[0..0], format_datetime(time), $$, severity, progname, msg2str(msg)]
      Formato % [format_datetime(time),severity, output_message]
    end
  end
end

logfile = File.join(File.dirname(__FILE__),'..','log', 'logger.log')
$log = Logger.new(logfile,'daily')
$log.level = Logger::DEBUG
$log.datetime_format = "%H:%M:%S"
$log.info("Watir Execution Starts")
$log.debug("Halllow Worled!")
$log.info("Achtung Achtung. The Train will leave from platform 9")
$log.warn("no no no no no. you can't do this")
$log.error("error error Danger Danger")
$log.fatal("game over game over game over")
# example of output
#[13:02:00] [DEBUG] : Watir Execution Starts
#[13:02:00] [DEBUG] : Halllow Worled!
#[13:02:00] [ INFO] : Achtung Achtung. The Train will leave from platform 9
#[13:02:00] [ WARN] : no no no no no. you can't do this
#[13:02:00] [ERROR] : error error Danger Danger
#[13:02:00] [FATAL] : game over game over game over


Empty a Gmail label with FireWatir

When you have a huge label with thousands of messages, Gmail can't handle deleting all of them at a time (it says it can, but for me it's never worked). This solves the problem.

I wrote it more as a practice to get familiar with FireWatir, so it's not very pretty or anything and it's a bit slow. If you have doubts on how to use or suggestions on how to improve it please feel free to contact me.

By the way, it assumes Gmail is in German. Replace that string with the one that comes up for your language.

def empty_label(label)
  # you need a Firefox instance already running with JSSh installed and listening
  ff = Firefox.new
  # Goes to Gmail in HTML
  ff.goto('http://mail.google.com/mail/h/x4odcwnm5f5v/?s=l&l=#{label}')
  a = []
  # doing 'c.set' didn't work for me here so I had to do this hack of getting the value
  # and then acquiring the element thorugh ff.checkbox(:value,value)
  ff.checkboxes.each {|c| a << c.value}

  while a.length > 0 do
    c = []
    a.each {|v| c << ff.checkbox(:value,v)}
    # checks all checkboxes
    c.each {|e| e.set}
    # clicks the drop-down entry for deleting
    ff.select_list(:name,'tact').select('In den Papierkorb verschieben')
    ff.button(:name,'nvp_tbu_go').click
    a = []
    ff.checkboxes.each {|c| a << c.value}
  end
end
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS