Class: Watir::AfterHooks
- Inherits:
-
Object
- Object
- Watir::AfterHooks
- Includes:
- Enumerable
- Defined in:
- lib/watir/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 ⇒ Integer
(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.
17 18 19 20 |
# File 'lib/watir/after_hooks.rb', line 17 def initialize(browser) @browser = browser @after_hooks = [] end |
Instance Method Details
#[](index) ⇒ #call
Gets the after hook at the given index.
131 132 133 |
# File 'lib/watir/after_hooks.rb', line 131 def [](index) @after_hooks[index] end |
#add(after_hook = nil) {|| ... } ⇒ Object Also known as: <<
Adds new after hook.
37 38 39 40 41 42 43 44 45 |
# File 'lib/watir/after_hooks.rb', line 37 def add(after_hook = nil, &block) if block @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.
61 62 63 |
# File 'lib/watir/after_hooks.rb', line 61 def delete(after_hook) @after_hooks.delete(after_hook) end |
#each {|after_hook| ... } ⇒ Object
Yields each after hook.
104 105 106 |
# File 'lib/watir/after_hooks.rb', line 104 def each(&block) @after_hooks.each(&block) end |
#length ⇒ Integer Also known as: size
Returns number of after hooks.
119 120 121 |
# File 'lib/watir/after_hooks.rb', line 119 def length @after_hooks.length end |
#run ⇒ Object
Runs after hooks.
69 70 71 72 73 74 75 76 |
# File 'lib/watir/after_hooks.rb', line 69 def run # We can't just rescue exception because Firefox automatically closes alert when exception raised return unless @after_hooks.any? && !@browser.alert.exists? each { |after_hook| after_hook.call(@browser) } rescue Selenium::WebDriver::Error::NoSuchWindowError => e Watir.logger.info "Could not execute After Hooks because browser window was closed #{e}" end |
#without {|| ... } ⇒ Object
Executes a block without running error after hooks.
90 91 92 93 94 95 96 |
# File 'lib/watir/after_hooks.rb', line 90 def without current_after_hooks = @after_hooks @after_hooks = [] yield(@browser) ensure @after_hooks = current_after_hooks end |