Class: Watir::AfterHooks

Inherits:
Object
  • Object
show all
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

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.

Parameters:

  • index (Integer)

Returns:

  • (#call)

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.

Examples:

browser.after_hooks.add do |browser|
  browser.text.include?("Server Error") and puts "Application exception or 500 error!"
end
browser.goto "watir.com/404"
"Application exception or 500 error!"

Parameters:

  • after_hook (#call) (defaults to: nil)

    Object responding to call

Yields:

  • after_hook block

Yield Parameters:


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.

Examples:

browser.after_hooks.add do |browser|
  browser.text.include?("Server Error") and puts "Application exception or 500 error!"
end
browser.goto "watir.com/404"
"Application exception or 500 error!"
browser.after_hooks.delete browser.after_hooks[0]
browser.refresh

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.

Yield Parameters:

  • after_hook (#call)

    Object responding to call


104
105
106
# File 'lib/watir/after_hooks.rb', line 104

def each(&block)
  @after_hooks.each(&block)
end

#lengthInteger Also known as: size

Returns number of after hooks.

Examples:

browser.after_hooks.add { puts 'Some after_hook.' }
browser.after_hooks.length
#=> 1

Returns:

  • (Integer)

119
120
121
# File 'lib/watir/after_hooks.rb', line 119

def length
  @after_hooks.length
end

#runObject

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.

Examples:

browser.after_hooks.without do |browser|
  browser.element(name: "new_user_button").click
end

Yields:

  • Block that is executed without after hooks being run

Yield Parameters:


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