Class: Oats::Oselenium

Inherits:
Object
  • Object
show all
Defined in:
lib/oats/oselenium.rb

Constant Summary collapse

@@browsers =
[]
@@server_started_by_oats =
false

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Oselenium

Returns a new instance of Oselenium.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/oats/oselenium.rb', line 39

def initialize(*args)

  browser_type = $oats['selenium']['browser_type']
  #    is_webdriver = ! $oats['selenium']['rcdriver']

  unless $oats_global['download_dir']
    if browser_type == 'firefox'
      $oats_global['download_dir'] = $oats['execution']['dir_results'] + '/downloads'
    elsif Oats.data("selenium.default_downloads")
      $oats_global['download_dir'] = File.join(ENV[ RUBY_PLATFORM =~ /(mswin|mingw)/ ? 'USERPROFILE' : 'HOME'], 'Downloads')
      $oats_global['download_dir'].gsub!('\\','/') if RUBY_PLATFORM =~ /(mswin|mingw)/
    end
  end

  download_dir = $oats_global['download_dir'].gsub('/','\\\\') if $oats_global['download_dir'] and
    RUBY_PLATFORM =~ /(mswin|mingw)/
  download_dir ||= $oats_global['download_dir']
  case browser_type
  when 'firefox'
    profile = $oats['selenium']['firefox_profile']
    $oats_global["browser_path"] ||= ENV[profile] if profile
    #      if is_webdriver
    profile = Selenium::WebDriver::Firefox::Profile.from_name profile if profile
    profile = Selenium::WebDriver::Firefox::Profile.new unless profile
    profile['browser.download.dir'] = download_dir
    profile["browser.helperApps.neverAsk.saveToDisk"] = "text/plain, application/vnd.ms-excel, application/pdf, text/csv"
    profile['browser.download.folderList'] = 2
    $oats['selenium']['firefox_profile_set'].each { |method,value| profile.send method+'=', value }

  when 'chrome'
    profile = Selenium::WebDriver::Chrome::Profile.new
    profile['download.prompt_for_download'] = false
    profile['download.default_directory'] = download_dir
#        unless $oats_global['browser_path']
#          vpath = File.join($oats['_']['vendor'], ENV['OS'], 'chromedriver' + (RUBY_PLATFORM =~ /(mswin|mingw)/ ? '.exe' : '') )
#          $oats_global['browser_path'] = vpath if File.exist?(vpath)
#        end
  end
  FileUtils.rm_f Dir.glob(File.join($oats_global['download_dir'],'*')) if $oats_global['download_dir']
  Oats.info "Browser type: #{browser_type.inspect}, profile: #{profile.inspect}, path: #{$oats_global["browser_path"].inspect}"
  $oats_info['browser'] = $oats['selenium']['browser_type'].sub(/ .*/,'')
  remote_webdriver = $oats['selenium']['remote_webdriver']
  driver_type = $oats['selenium']['browser_type']
  if remote_webdriver
    remote_webdriver = remote_webdriver[browser_type]
    if remote_webdriver
      driver_type = 'remote'
      @remote_webdriver_is_active = true
    end
  end
  opts = [ driver_type.to_sym ]
  opts_hash ={}
  opts_hash[:profile] = profile if profile and driver_type != 'remote'
  if driver_type == 'remote'
    opts_hash[:url] = "http://"+remote_webdriver+'/wd/hub'
    opts_hash[:desired_capabilities] = $oats['selenium']['browser_type'].to_sym
  end
  case browser_type
  when /firefox/
    Selenium::WebDriver::Firefox.path = $oats_global["browser_path"]
  when /chrome/
    Selenium::WebDriver::Chrome.driver_path = $oats_global['browser_path']
  end if $oats_global['browser_path']
  browser_options = Oats.data "selenium.options.#{browser_type}"
  browser_options.each_pair { |name, val| opts_hash[name.to_sym] = val } if browser_options
  opts.push(opts_hash) unless opts_hash.empty?
  #      OatsLock.record_firefox_processes do
  @browser = Selenium::WebDriver.for(*opts)
  #      end
  @browser.osel = self
  $selenium = @browser
  @@browsers << @browser
  (*args)
end

Instance Attribute Details

#browserObject (readonly)

Returns the value of attribute browser.



14
15
16
# File 'lib/oats/oselenium.rb', line 14

def browser
  @browser
end

#login_base_urlObject (readonly)

Returns the value of attribute login_base_url.



14
15
16
# File 'lib/oats/oselenium.rb', line 14

def 
  @login_base_url
end

#siteObject (readonly)

Returns the value of attribute site.



14
15
16
# File 'lib/oats/oselenium.rb', line 14

def site
  @site
end

#userObject (readonly)

Returns the value of attribute user.



14
15
16
# File 'lib/oats/oselenium.rb', line 14

def user
  @user
end

Class Method Details

.browser(*args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/oats/oselenium.rb', line 21

def Oselenium.browser(*args)
  if args.include?(true)
    new_browser = true
    args.delete true
  end
  if not args.empty?
    if not new_browser and Oselenium.browsers.last
      return Oselenium.browsers.last.(*args)
    else
      return Oselenium.new(*args).browser
    end
  elsif Oselenium.browsers.last
    return Oselenium.browsers.last
  else
    return Oselenium.new.browser
  end
end

.browsersObject



17
18
19
# File 'lib/oats/oselenium.rb', line 17

def Oselenium.browsers
  @@browsers
end

.closeObject



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/oats/oselenium.rb', line 145

def Oselenium.close
  while browser = Oselenium.browsers.pop
    if browser.osel.site
      browser.oats_debug "Closing browser session from #{browser.osel.site}"
    else
      browser.oats_debug "Closing browser session."
    end
    browser.quit
  end
  $selenium = nil
end

.pause_browserObject



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/oats/oselenium.rb', line 157

def Oselenium.pause_browser
  return if Oselenium.browsers.empty? or ! TestData.pause_after_error
  seconds = 999999
  pause_val = $oats['selenium']['pause_on_exit']
  if not pause_val.integer? or
      pause_val <= 0
    seconds = nil
  elsif pause_val == 1
    seconds = nil unless TestData.current_test.status == 1
  elsif pause_val > 0
    seconds = pause_val
  end
  if seconds
    $stderr.puts "Paused because selenium:pause_on_exit is set to [#{pause_val}]"
    $stderr.puts "PLEASE HIT <ENTER> TO CONTINUE!"
    begin
      # timeout(seconds) { loop { browser.get_title ; sleep 1 } }
      timeout(seconds) { STDIN.readline }
    rescue Timeout::Error
    end
  end
  TestData.pause_after_error = false
end

.portObject



181
182
183
184
185
186
187
# File 'lib/oats/oselenium.rb', line 181

def Oselenium.port
  #    if $oats_execution['agent']
  #      '4'+$oats_execution['agent']['execution:occ:agent_port'].to_s
  #    else
  $oats['selenium']['port']
  #    end
end

.remote_webdriver_map_file_path(file) ⇒ Object

Fixes the path to Windows if running on windows remote webdriver



125
126
127
128
129
130
131
132
133
# File 'lib/oats/oselenium.rb', line 125

def Oselenium.remote_webdriver_map_file_path(file)
  if $selenium and $selenium.osel.remote_webdriver?
    file.sub!(ENV['OATS_HOME'], $oats['selenium']['remote_webdriver']['oats_dir'])
    file_os = $oats['selenium']['remote_webdriver']['os']
  else
    file_os = RUBY_PLATFORM
  end
  file_os =~ /(mswin|mingw)/ ? file.gsub('/','\\') : file
end

.resetObject



135
136
137
138
139
140
141
142
143
# File 'lib/oats/oselenium.rb', line 135

def Oselenium.reset
  Oselenium.close
rescue Timeout::Error
  ## Sometimes doesn't die gracefully. It is OK, will be killed below.
ensure
  @@browsers = []
  $selenium = nil
  OatsLock.kill_webdriver_browsers
end

Instance Method Details

#login(*args) ⇒ Object

Stub method/interface for actions to take after instantiating the browser



115
116
117
# File 'lib/oats/oselenium.rb', line 115

def (*args) #
  selenium.open(args.first)
end

#remote_webdriver?Boolean

True if selenium is running in remote_webdriver mode

Returns:

  • (Boolean)


120
121
122
# File 'lib/oats/oselenium.rb', line 120

def remote_webdriver?
  @remote_webdriver_is_active
end