Class: PrismChecker::Node::Base
- Inherits:
-
Object
- Object
- PrismChecker::Node::Base
- Defined in:
- lib/prism_checker/node/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#checker ⇒ Object
readonly
Returns the value of attribute checker.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#expectation ⇒ Object
readonly
Returns the value of attribute expectation.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #build_expectation(expectation) ⇒ Object
- #check ⇒ Object
- #check_absence ⇒ Object
- #check_wrapper ⇒ Object
- #element ⇒ Object
- #failure? ⇒ Boolean
-
#initialize(checker, parent, name, expectation) ⇒ Base
constructor
A new instance of Base.
- #path ⇒ Object
- #root? ⇒ Boolean
- #success? ⇒ Boolean
- #type(element) ⇒ Object
- #wait_until_true(timeout = @timeout, &block) ⇒ Object
Constructor Details
#initialize(checker, parent, name, expectation) ⇒ Base
Returns a new instance of Base.
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/prism_checker/node/base.rb', line 14 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
#checker ⇒ Object (readonly)
Returns the value of attribute checker.
12 13 14 |
# File 'lib/prism_checker/node/base.rb', line 12 def checker @checker end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
12 13 14 |
# File 'lib/prism_checker/node/base.rb', line 12 def error @error end |
#expectation ⇒ Object (readonly)
Returns the value of attribute expectation.
12 13 14 |
# File 'lib/prism_checker/node/base.rb', line 12 def expectation @expectation end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
12 13 14 |
# File 'lib/prism_checker/node/base.rb', line 12 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
12 13 14 |
# File 'lib/prism_checker/node/base.rb', line 12 def parent @parent end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
12 13 14 |
# File 'lib/prism_checker/node/base.rb', line 12 def status @status end |
Instance Method Details
#build_expectation(expectation) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/prism_checker/node/base.rb', line 27 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 |
#check ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/prism_checker/node/base.rb', line 114 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 value = checker.value(element) checker.check(element, value, expectation) end unless result raise Node::CheckFail, checker.(element, value, expectation) end end @checked_element = element end end |
#check_absence ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/prism_checker/node/base.rb', line 86 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_wrapper ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/prism_checker/node/base.rb', line 73 def check_wrapper yield @status = 'Ok' rescue Node::CheckFail => e @error = e @status = 'Fail' raise rescue StandardError => e @error = e @status = 'Error' raise end |
#element ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/prism_checker/node/base.rb', line 51 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
43 44 45 |
# File 'lib/prism_checker/node/base.rb', line 43 def failure? @status == 'Fail' || @status == 'Error' end |
#path ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/prism_checker/node/base.rb', line 98 def path result = [] p = self while p.is_a? Base result << p.name p = p.parent end result.reverse end |
#root? ⇒ Boolean
39 40 41 |
# File 'lib/prism_checker/node/base.rb', line 39 def root? parent.is_a?(Checker) end |
#success? ⇒ Boolean
47 48 49 |
# File 'lib/prism_checker/node/base.rb', line 47 def success? @status == 'Ok' end |
#type(element) ⇒ Object
110 111 112 |
# File 'lib/prism_checker/node/base.rb', line 110 def type(element) @type ||= ItemClassifier.classify(element) end |
#wait_until_true(timeout = @timeout, &block) ⇒ Object
67 68 69 70 71 |
# File 'lib/prism_checker/node/base.rb', line 67 def wait_until_true(timeout = @timeout, &block) SitePrism::Waiter.wait_until_true(timeout, &block) rescue SitePrism::TimeoutError false end |