Class: TestCentricity::ExceptionQueue

Inherits:
Object
  • Object
show all
Includes:
Capybara::DSL
Defined in:
lib/testcentricity_web/exception_queue_helper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#active_ui_elementObject

Returns the value of attribute active_ui_element.



6
7
8
# File 'lib/testcentricity_web/exception_queue_helper.rb', line 6

def active_ui_element
  @active_ui_element
end

#error_queueObject

Returns the value of attribute error_queue.



5
6
7
# File 'lib/testcentricity_web/exception_queue_helper.rb', line 5

def error_queue
  @error_queue
end

#mru_ui_elementObject

Returns the value of attribute mru_ui_element.



7
8
9
# File 'lib/testcentricity_web/exception_queue_helper.rb', line 7

def mru_ui_element
  @mru_ui_element
end

Class Method Details

.enqueue_assert_equal(expected, actual, error_message) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/testcentricity_web/exception_queue_helper.rb', line 9

def self.enqueue_assert_equal(expected, actual, error_message)
  is_equal = if %i[edge safari].include?(Environ.browser) && expected.is_a?(String) && actual.is_a?(String)
               expected.downcase.strip == actual.downcase.strip
             else
               expected == actual
             end
  unless is_equal
    enqueue("#{error_message} to be\n  '#{expected}'\nbut found\n  '#{actual}'")
    enqueue_screenshot
  end
end

.enqueue_assert_not_equal(expected, actual, error_message) ⇒ Object



21
22
23
24
25
26
# File 'lib/testcentricity_web/exception_queue_helper.rb', line 21

def self.enqueue_assert_not_equal(expected, actual, error_message)
  unless expected != actual
    enqueue("#{error_message} to not be equal to '#{expected}'")
    enqueue_screenshot
  end
end

.enqueue_comparison(ui_object, state, actual, error_msg) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/testcentricity_web/exception_queue_helper.rb', line 43

def self.enqueue_comparison(ui_object, state, actual, error_msg)
  @active_ui_element = ui_object
  if state.is_a?(Hash) && state.length == 1
    state.each do |key, value|
      case key
      when :lt, :less_than
        enqueue_exception("#{error_msg} be less than #{value} but found '#{actual}'") unless actual < value
      when :lt_eq, :less_than_or_equal
        enqueue_exception("#{error_msg} be less than or equal to #{value} but found '#{actual}'") unless actual <= value
      when :gt, :greater_than
        enqueue_exception("#{error_msg} be greater than #{value} but found '#{actual}'") unless actual > value
      when :gt_eq, :greater_than_or_equal
        enqueue_exception("#{error_msg} be greater than or equal to  #{value} but found '#{actual}'") unless actual >= value
      when :starts_with
        enqueue_exception("#{error_msg} start with '#{value}' but found '#{actual}'") unless actual.start_with?(value)
      when :ends_with
        enqueue_exception("#{error_msg} end with '#{value}' but found '#{actual}'") unless actual.end_with?(value)
      when :contains
        enqueue_exception("#{error_msg} contain '#{value}' but found '#{actual}'") unless actual.include?(value)
      when :not_contains, :does_not_contain
        enqueue_exception("#{error_msg} not contain '#{value}' but found '#{actual}'") if actual.include?(value)
      when :not_equal
        enqueue_exception("#{error_msg} not equal '#{value}' but found '#{actual}'") if actual == value
      when :like, :is_like
        actual_like = actual.delete("\n")
        actual_like = actual_like.delete("\r")
        actual_like = actual_like.delete("\t")
        actual_like = actual_like.delete(' ')
        actual_like = actual_like.downcase
        expected    = value.delete("\n")
        expected    = expected.delete("\r")
        expected    = expected.delete("\t")
        expected    = expected.delete(' ')
        expected    = expected.downcase
        enqueue_exception("#{error_msg} be like '#{value}' but found '#{actual}'") unless actual_like.include?(expected)
      when :translate
        expected = translate(value)
        enqueue_assert_equal(expected, actual, error_msg)
      when :translate_upcase
        expected = translate(value)
        expected = expected.is_a?(Array) ? expected.map(&:upcase) : expected.upcase
        enqueue_assert_equal(expected, actual, error_msg)
      when :translate_downcase
        expected = translate(value)
        expected = expected.is_a?(Array) ? expected.map(&:downcase) : expected.downcase
        enqueue_assert_equal(expected, actual, error_msg)
      when :translate_capitalize
        expected = translate(value)
        expected = expected.is_a?(Array) ? expected.map(&:capitalize) : expected.capitalize
        enqueue_assert_equal(expected, actual, error_msg)
      when :translate_titlecase
        expected = translate(value)
        expected = if expected.is_a?(Array)
                     result = []
                     expected.each do |item|
                       result.push("#{item.split.each{ |item| item.capitalize! }.join(' ')}")
                     end
                     result
                   else
                     "#{expected.split.each{ |expected| expected.capitalize! }.join(' ')}"
                   end
        enqueue_assert_equal(expected, actual, error_msg)
      else
        raise "#{key} is not a valid comparison key"
      end
    end
  else
    enqueue_assert_equal(state, actual, error_msg)
  end
end

.enqueue_exception(error_message) ⇒ Object



28
29
30
# File 'lib/testcentricity_web/exception_queue_helper.rb', line 28

def self.enqueue_exception(error_message)
  enqueue(error_message)
end

.post_exceptions(preample_message = nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/testcentricity_web/exception_queue_helper.rb', line 32

def self.post_exceptions(preample_message = nil)
  unless @error_queue.nil?
    @error_queue = "#{preample_message} - The following errors were found:\n_______________________________\n#{@error_queue}" unless preample_message.nil?
    raise @error_queue
  end
ensure
  @error_queue = nil
  @active_ui_element = nil
  @mru_ui_element = nil
end