Module: CukeHelper

Includes:
Watirmark::Model::CucumberHelper
Defined in:
lib/watirmark/cucumber/cuke_helper.rb

Instance Method Summary collapse

Methods included from Watirmark::Model::CucumberHelper

#format_value, #insert_model, #merge_cucumber_table

Instance Method Details

#call_model_methods(hash) ⇒ Object

calls the models method if of the pattern <name>.method



27
28
29
30
# File 'lib/watirmark/cucumber/cuke_helper.rb', line 27

def call_model_methods(hash)
  hash.each { |key, value| hash[key] = eval(value[1..value.length]) if value[0, 1].eql?("=") }
  hash
end

#colonize(hash) ⇒ Object

return :bar=2 from =>1, ‘bar’ =>2



33
34
35
36
37
# File 'lib/watirmark/cucumber/cuke_helper.rb', line 33

def colonize(hash)
  newhash = {}
  hash.each { |k, v| newhash[k.to_sym] = v }
  newhash
end

#compose_string_from(record_hash, key_list) ⇒ Object

Given a list of keys or strings (call it key), return a string composed of record_hash or the string is key isn’t really a key. Yea, screwy I know



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/watirmark/cucumber/cuke_helper.rb', line 65

def compose_string_from(record_hash, key_list)
  result = ''
  key_list.each do |key|
    if record_hash[key]
      result += record_hash[key]
    elsif key.is_a? String
      result += key
    end
  end
  result
end

#create_ts(with_separators = true) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/watirmark/cucumber/cuke_helper.rb', line 140

def create_ts(with_separators=true)
  if with_separators
    Time.now.strftime "%Y:%m:%d:%H:%M:%S"
  else
    Time.now.strftime "%Y%m%d%H%M%S"
  end
end

#eval_keywords(hash) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/watirmark/cucumber/cuke_helper.rb', line 10

def eval_keywords(hash)
  newhash = {}
  hash.each do |key, value|
    newhash[key] = format_value(value)
  end
  newhash
end

#eval_raw_record(row) ⇒ Object

when the input is an array, eval each element



19
20
21
22
23
24
# File 'lib/watirmark/cucumber/cuke_helper.rb', line 19

def eval_raw_record(row)
  row.each_index do |col|
    row[col] = format_value(row[col])
  end
  row
end

#floatify(record_hash, key_list) ⇒ Object

given a hash and list of keys, make the values into a float if they exist FIXME: this is a work around until the Cucumber tests use a new matcher in convio_watir by making strings into floats

record==> “10.00”, :probability => “100” floatify(record, [:amountreceived, :probability]) changes record into record==> 10.0, :probability => 100.0



128
129
130
131
132
133
# File 'lib/watirmark/cucumber/cuke_helper.rb', line 128

def floatify(record_hash, key_list)
  key_list.each do |key|
    record_hash[key] = record_hash[key].to_f if record_hash[key]
  end
  record_hash
end

#hash_record(table) ⇒ Object

returns :bar=2 hash record from a key, value 2 column table | foo | 1 | | bar | 2 |



54
55
56
# File 'lib/watirmark/cucumber/cuke_helper.rb', line 54

def hash_record table
  colonize eval_keywords(table.rows_hash)
end

#hash_record_list(table) ⇒ Object

returns [:bar=2, :bar=4] list of hash records from table table where first row headers are keys and each row values | foo | bar | | 1 | 2 | | 3 | 4 |



44
45
46
47
48
# File 'lib/watirmark/cucumber/cuke_helper.rb', line 44

def hash_record_list table
  table.map_headers! { |h| h.to_sym }
  records = table.hashes
  records.map { |record| eval_keywords(record) }
end

#logObject



6
7
8
# File 'lib/watirmark/cucumber/cuke_helper.rb', line 6

def log
  Watirmark::Configuration.instance.logger
end

#multimerge(*hashes) ⇒ Object

merges sequentially a list of hashes



136
137
138
# File 'lib/watirmark/cucumber/cuke_helper.rb', line 136

def multimerge *hashes
  hashes.inject({}) { |memo, hash| hash.nil? ? memo : memo.merge(hash) }
end

#raw_record(table) ⇒ Object

returns .raw but still handles eval-ing the keywords



59
60
61
# File 'lib/watirmark/cucumber/cuke_helper.rb', line 59

def raw_record table
  table.raw.map { |record| eval_raw_record(record) }
end

#syncmode(expected_bus, expected_datasync) ⇒ Object

Allows expected values for a cucumber table field to be different based on whether you are using Service Bus or DataSync. Use case is for a bug in DataSync that was not replicated in Service Bus In this example: DataSync incorrectly assigns ‘Donation’ and Service Bus correctly assigns ‘TeamRaiser Gift’ to transaction type field

# VERIFY ---------------+-------------------------------------------+
| accountname           | Tr01_Hellraiser Eventdonor Household      |
| type                  | Individual Gift                           |
| transactiontype       | =syncmode('TeamRaiser Gift','Donation')   |


86
87
88
89
90
91
92
# File 'lib/watirmark/cucumber/cuke_helper.rb', line 86

def syncmode(expected_bus, expected_datasync)
  if ENV['SYNCMODE'] == 'DataSync'
    expected_datasync
  else
    expected_bus
  end
end

#verify_failure(tries_count = 6, seconds_between_tries = 30) ⇒ Object

if a block provided raises VerificationException we verify again this method acts as a gateway to prevent false negatives in testing CRM objects that we expect to change their values after being updated by the service bus motivation: After serivce bus sends a message to a CRM it takes a while sometimes for the updates to the object to occur. When we verify the state of the object we may still look at old values. We want to introduce a pooling mechanism that would re-verify one or more times after a duration of time and only fail after the specified count of tries. usage:

verify_failure { controller.verify } #=>  provide block that can fail with VerificationException


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/watirmark/cucumber/cuke_helper.rb', line 104

def verify_failure(tries_count=6, seconds_between_tries=30)
  counter = 0
  loop do
    counter += 1
    begin
      yield
    rescue Watirmark::VerificationException => e
      Watirmark.logger.warn "*** reverifying failure: #{e}"
      raise e if counter >= tries_count
      sleep seconds_between_tries
    else
      break
    end
  end
end