Class: PrismChecker::Node::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/prism_checker/node/base.rb

Direct Known Subclasses

Array, Expectation, Hash

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(checker, parent, name, expectation) ⇒ Base

Returns a new instance of Base.



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/prism_checker/node/base.rb', line 15

def initialize(checker, parent, name, expectation)
  @parent = parent
  @checker = checker
  @name = name
  @expectation = build_expectation(expectation)
  @element = nil
  @status = 'Not checked'
  @error = nil
  @timeout = Capybara.default_max_wait_time
  @type = nil
  @checked_element = nil
end

Instance Attribute Details

#checkerObject (readonly)

Returns the value of attribute checker.



13
14
15
# File 'lib/prism_checker/node/base.rb', line 13

def checker
  @checker
end

#errorObject (readonly)

Returns the value of attribute error.



13
14
15
# File 'lib/prism_checker/node/base.rb', line 13

def error
  @error
end

#expectationObject (readonly)

Returns the value of attribute expectation.



13
14
15
# File 'lib/prism_checker/node/base.rb', line 13

def expectation
  @expectation
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/prism_checker/node/base.rb', line 13

def name
  @name
end

#parentObject (readonly)

Returns the value of attribute parent.



13
14
15
# File 'lib/prism_checker/node/base.rb', line 13

def parent
  @parent
end

#statusObject (readonly)

Returns the value of attribute status.



13
14
15
# File 'lib/prism_checker/node/base.rb', line 13

def status
  @status
end

Instance Method Details

#build_expectation(expectation) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/prism_checker/node/base.rb', line 28

def build_expectation(expectation)
  if expectation.is_a?(Symbol) && expectation.start_with?('absent')
    delay = expectation == :absent ? 0 : Integer(expectation.to_s.split('absent').last)

    return AbsenceExpectation.new(delay)
  end

  expectation
rescue ArgumentError
  expectation
end

#checkObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/prism_checker/node/base.rb', line 115

def check
  return check_absence if expectation.is_a?(AbsenceExpectation)

  check_wrapper do
    element = self.element
    element_type = type(element)
    checkers = CheckDispatcher.checkers(self, element, expectation, element_type)
    value = nil
    checkers.each do |checker|
      result = wait_until_true do
        element = self.element if element.is_a?(::Array) || element.is_a?(Capybara::Result)

        value = checker.value(element)
        checker.check(element, value, expectation)
      end

      unless result
        raise Node::CheckFail, checker.error_message(element, value, expectation)
      end
    end

    @checked_element = element
  end
end

#check_absenceObject



87
88
89
90
91
92
93
94
95
96
97
# File 'lib/prism_checker/node/base.rb', line 87

def check_absence
  sleep expectation.delay

  check_wrapper do
    result = wait_until_true(0.1) do
      parent.element.public_send("has_no_#{name}?")
    end

    raise Node::CheckFail, 'Element is present' unless result
  end
end

#check_wrapperObject



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/prism_checker/node/base.rb', line 74

def check_wrapper
  yield
  @status = 'Ok'
rescue Node::CheckFail => e
  @error = e
  @status = 'Fail'
  raise
rescue StandardError => e
  @error = e
  @status = 'Error'
  raise
end

#elementObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/prism_checker/node/base.rb', line 52

def element
  return @checked_element if @checked_element

  if root?
    @checker.item
  elsif parent.is_a?(Node::Hash)
    if parent.element.is_a?(Capybara::Node::Element)
      ElementWrapper.new(parent.element).send(@name)
    else
      parent.element.send(@name)
    end
  elsif parent.is_a?(Node::Array)
    parent.element[@name]
  end
end

#failure?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/prism_checker/node/base.rb', line 44

def failure?
  @status == 'Fail' || @status == 'Error'
end

#pathObject



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/prism_checker/node/base.rb', line 99

def path
  result = []
  p = self

  while p.is_a? Base
    result << p.name
    p = p.parent
  end

  result.reverse
end

#root?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/prism_checker/node/base.rb', line 40

def root?
  parent.is_a?(Checker)
end

#success?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/prism_checker/node/base.rb', line 48

def success?
  @status == 'Ok'
end

#type(element) ⇒ Object



111
112
113
# File 'lib/prism_checker/node/base.rb', line 111

def type(element)
  @type ||= ItemClassifier.classify(element)
end

#wait_until_true(timeout = @timeout, &block) ⇒ Object



68
69
70
71
72
# File 'lib/prism_checker/node/base.rb', line 68

def wait_until_true(timeout = @timeout, &block)
  SitePrism::Waiter.wait_until_true(timeout, &block)
rescue SitePrism::TimeoutError
  false
end