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.
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
#checker ⇒ Object (readonly)
Returns the value of attribute checker.
13 14 15 |
# File 'lib/prism_checker/node/base.rb', line 13 def checker @checker end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
13 14 15 |
# File 'lib/prism_checker/node/base.rb', line 13 def error @error end |
#expectation ⇒ Object (readonly)
Returns the value of attribute expectation.
13 14 15 |
# File 'lib/prism_checker/node/base.rb', line 13 def expectation @expectation end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'lib/prism_checker/node/base.rb', line 13 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
13 14 15 |
# File 'lib/prism_checker/node/base.rb', line 13 def parent @parent end |
#status ⇒ Object (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 |
#check ⇒ Object
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 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
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_wrapper ⇒ Object
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 |
#element ⇒ Object
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
44 45 46 |
# File 'lib/prism_checker/node/base.rb', line 44 def failure? @status == 'Fail' || @status == 'Error' end |
#path ⇒ Object
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
40 41 42 |
# File 'lib/prism_checker/node/base.rb', line 40 def root? parent.is_a?(Checker) end |
#success? ⇒ 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 |