Module: WatirSession

Extended by:
WatirSession
Included in:
WatirSession
Defined in:
lib/watir_session/watir_session.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#browserObject (readonly)

Returns the value of attribute browser


5
6
7
# File 'lib/watir_session/watir_session.rb', line 5

def browser
  @browser
end

#watir_configObject


8
9
10
# File 'lib/watir_session/watir_session.rb', line 8

def watir_config
  @watir_config ||= WatirConfig.new
end

Instance Method Details

#after_browser(*args) ⇒ Object


112
113
114
# File 'lib/watir_session/watir_session.rb', line 112

def after_browser(*args)
  execute_hook :after_browser, *args
end

#after_each(*args) ⇒ Object


102
103
104
105
106
107
108
109
110
# File 'lib/watir_session/watir_session.rb', line 102

def after_each(*args)
  execute_hook :after_each, *args

  take_screenshot(*args) unless watir_config.take_screenshots == :never

  quit_browser unless watir_config.reuse_browser

  after_browser(*args)
end

#after_suite(*args) ⇒ Object


121
122
123
124
125
# File 'lib/watir_session/watir_session.rb', line 121

def after_suite(*args)
  quit_browser if watir_config.reuse_browser

  execute_hook :after_suite, *args
end

#before_browser(*args) ⇒ Object


66
67
68
# File 'lib/watir_session/watir_session.rb', line 66

def before_browser(*args)
  execute_hook :before_browser, *args
end

#before_each(*args) ⇒ Object


86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/watir_session/watir_session.rb', line 86

def before_each(*args)
  if watir_config.reuse_browser && browser.nil
    raise StandardError, "#before_tests method must be set in order to use
the #reuse_browser configuration setting"
  end

  before_browser(*args)

  @browser = create_browser(*args) unless watir_config.reuse_browser
  @browser.window.maximize if watir_config.maximize_browser

  execute_hook :before_each, *args

  @browser
end

#before_suite(*args) ⇒ Object


60
61
62
63
64
# File 'lib/watir_session/watir_session.rb', line 60

def before_suite(*args)
  create_browser if watir_config.reuse_browser

  execute_hook :before_suite, *args
end

#configure_watirObject


153
154
155
# File 'lib/watir_session/watir_session.rb', line 153

def configure_watir
  Watir.default_timeout = watir_config.watir_timeout
end

#create_browser(*args) ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/watir_session/watir_session.rb', line 70

def create_browser(*args)
  use_headless_display if watir_config.headless

  @browser = execute_hook(:create_browser, *args).compact.first
  return @browser if @browser

  if args.empty?
    http_client = Selenium::WebDriver::Remote::Http::Default.new
    http_client.timeout = watir_config.http_timeout
    @browser = Watir::Browser.new(watir_config.browser,
                                  http_client: http_client)
  else
    @browser = Watir::Browser.new *args
  end
end

#create_configurationsObject


16
17
18
19
20
21
22
23
24
25
26
# File 'lib/watir_session/watir_session.rb', line 16

def create_configurations
  spec_path = $LOAD_PATH.select { |path| path =~ /\/spec$/ }.first
  config_path = spec_path.gsub('spec', 'config')

  return unless Dir.exist?(config_path)
  Dir.entries(config_path).select { |file| file =~ /\.yml$/ }.each do |yaml|
    config_name = yaml.gsub('.yml', '')
    next unless Object.const_defined?("#{config_name.capitalize}Config")
    load_yml(config_path, config_name)
  end
end

#custom_configObject


12
13
14
# File 'lib/watir_session/watir_session.rb', line 12

def custom_config
  @custom_config ||= CustomConfig.new
end

#execute_hook(method, *args) ⇒ Object


45
46
47
48
49
50
51
52
53
# File 'lib/watir_session/watir_session.rb', line 45

def execute_hook(method, *args)
  sessions = registered_sessions.select do |session|
    session.public_methods(false).include? method
  end

  sessions.each_with_object([]) do |session, array|
    array << session.send(method, *args)
  end
end

#load_yml(config_path, config_name) ⇒ Object


28
29
30
31
32
33
34
35
# File 'lib/watir_session/watir_session.rb', line 28

def load_yml(config_path, config_name)
  config = YAML.load_file("#{config_path}/#{config_name}.yml")
  if config.values.all? { |v| v.is_a? Hash } && custom_config.respond_to?(config_name.singularize)
    config = config[custom_config.send(config_name.singularize)]
  end
  obj = Object.const_get("#{config_name.capitalize}Config").new(config)
  custom_config.send("#{config_name.singularize}=", obj)
end

#quit_browserObject


127
128
129
130
131
132
133
134
135
136
137
# File 'lib/watir_session/watir_session.rb', line 127

def quit_browser
  if @headless
    @headless.destroy
    @headless = nil
  end

  return if @browser.nil?

  @browser.quit
  @browser = nil
end

#register_session(session) ⇒ Object


41
42
43
# File 'lib/watir_session/watir_session.rb', line 41

def register_session(session)
  registered_sessions << session
end

#registered_sessionsObject


37
38
39
# File 'lib/watir_session/watir_session.rb', line 37

def registered_sessions
  @registered_sessions ||= []
end

#reset_config!Object


144
145
146
147
# File 'lib/watir_session/watir_session.rb', line 144

def reset_config!
  @watir_config = nil
  @custom_config = nil
end

#reset_registered_sessions!Object


149
150
151
# File 'lib/watir_session/watir_session.rb', line 149

def reset_registered_sessions!
  @registered_sessions = nil
end

#restart_browser!Object


139
140
141
142
# File 'lib/watir_session/watir_session.rb', line 139

def restart_browser!
  quit_browser
  create_browser
end

#startObject


55
56
57
58
# File 'lib/watir_session/watir_session.rb', line 55

def start
  configure_watir
  create_configurations
end

#take_screenshot(*args) ⇒ Object


116
117
118
119
# File 'lib/watir_session/watir_session.rb', line 116

def take_screenshot(*args)
  screenshot = execute_hook(:take_screenshot, *args).compact
  browser.screenshot.save("screenshot.png") if screenshot.nil?
end

#use_headless_displayObject


157
158
159
160
161
162
163
164
165
# File 'lib/watir_session/watir_session.rb', line 157

def use_headless_display
  unless RbConfig::CONFIG['host_os'].match('linux')
    warn "Headless only supported on Linux"
    return
  end
  require 'headless'
  @headless = Headless.new
  @headless.start
end