Class: Watir::AfterHooks

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

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.

Parameters:

  • index (Fixnum)

Returns:

  • (#call)


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.

Examples:

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

Parameters:

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

    Object responding to call

Yields:

  • after_hook block

Yield Parameters:



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.

Examples:

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


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.

Yield Parameters:

  • after_hook (#call)

    Object responding to call



100
101
102
# File 'lib/watir-webdriver/after_hooks.rb', line 100

def each
  @after_hooks.each { |after_hook| yield after_hook }
end

#lengthFixnum Also known as: size

Returns number of after hooks.

Examples:

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

Returns:

  • (Fixnum)


115
116
117
# File 'lib/watir-webdriver/after_hooks.rb', line 115

def length
  @after_hooks.length
end

#runObject

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.

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:



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