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.


15
16
17
18
# File 'lib/watir/after_hooks.rb', line 15

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)

129
130
131
# File 'lib/watir/after_hooks.rb', line 129

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:


35
36
37
38
39
40
41
42
43
# File 'lib/watir/after_hooks.rb', line 35

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.

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

59
60
61
# File 'lib/watir/after_hooks.rb', line 59

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


102
103
104
# File 'lib/watir/after_hooks.rb', line 102

def each
  @after_hooks.each { |after_hook| yield after_hook }
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)

117
118
119
# File 'lib/watir/after_hooks.rb', line 117

def length
  @after_hooks.length
end

#runObject

Runs after hooks.


67
68
69
70
71
72
73
74
# File 'lib/watir/after_hooks.rb', line 67

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 => ex
  Watir.logger.info "Could not execute After Hooks because browser window was closed #{ex}"
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:


88
89
90
91
92
93
94
# File 'lib/watir/after_hooks.rb', line 88

def without
  current_after_hooks = @after_hooks
  @after_hooks = []
  yield(@browser)
ensure
  @after_hooks = current_after_hooks
end