Class: Watir::AfterHooks
- Inherits:
-
Object
- Object
- Watir::AfterHooks
- Includes:
- Enumerable
- Defined in:
- lib/watir-webdriver/after_hooks.rb
Overview
After hooks are blocks that run after certain browser events. They are generally used to ensure application under test does not encounter any error and are automatically executed after following events:
1. Open URL.
2. Refresh page.
3. Click, double-click or right-click on element.
4. Alert closing.
Instance Method Summary collapse
-
#[](index) ⇒ #call
Gets the after hook at the given index.
-
#add(after_hook = nil) {|| ... } ⇒ Object
(also: #<<)
Adds new after hook.
-
#delete(after_hook) ⇒ Object
Deletes after hook.
-
#each {|after_hook| ... } ⇒ Object
Yields each after hook.
-
#initialize(browser) ⇒ AfterHooks
constructor
A new instance of AfterHooks.
-
#length ⇒ Fixnum
(also: #size)
Returns number of after hooks.
-
#run ⇒ Object
Runs after hooks.
-
#without {|| ... } ⇒ Object
Executes a block without running error after hooks.
Constructor Details
#initialize(browser) ⇒ AfterHooks
Returns a new instance of AfterHooks.
16 17 18 19 |
# File 'lib/watir-webdriver/after_hooks.rb', line 16 def initialize(browser) @browser = browser @after_hooks = [] end |
Instance Method Details
#[](index) ⇒ #call
Gets the after hook at the given index.
127 128 129 |
# File 'lib/watir-webdriver/after_hooks.rb', line 127 def [](index) @after_hooks[index] end |
#add(after_hook = nil) {|| ... } ⇒ Object Also known as: <<
Adds new after hook.
36 37 38 39 40 41 42 43 44 |
# File 'lib/watir-webdriver/after_hooks.rb', line 36 def add(after_hook = nil, &block) if block_given? @after_hooks << block elsif after_hook.respond_to? :call @after_hooks << after_hook else raise ArgumentError, "expected block or object responding to #call" end end |
#delete(after_hook) ⇒ Object
Deletes after hook.
60 61 62 |
# File 'lib/watir-webdriver/after_hooks.rb', line 60 def delete(after_hook) @after_hooks.delete(after_hook) end |
#each {|after_hook| ... } ⇒ Object
Yields each after hook.
100 101 102 |
# File 'lib/watir-webdriver/after_hooks.rb', line 100 def each @after_hooks.each { |after_hook| yield after_hook } end |
#length ⇒ Fixnum Also known as: size
Returns number of after hooks.
115 116 117 |
# File 'lib/watir-webdriver/after_hooks.rb', line 115 def length @after_hooks.length end |
#run ⇒ Object
Runs after hooks.
68 69 70 71 72 |
# File 'lib/watir-webdriver/after_hooks.rb', line 68 def run if @after_hooks.any? && @browser.window.present? each { |after_hook| after_hook.call(@browser) } end end |
#without {|| ... } ⇒ Object
Executes a block without running error after hooks.
86 87 88 89 90 91 92 |
# File 'lib/watir-webdriver/after_hooks.rb', line 86 def without current_after_hooks = @after_hooks @after_hooks = [] yield(@browser) ensure @after_hooks = current_after_hooks end |