Class: Watirmark::Session

Inherits:
Object show all
Includes:
Singleton, CucumberPostFailureBuffering
Defined in:
lib/watirmark/session.rb

Constant Summary collapse

POST_WAIT_CHECKERS =
[]

Instance Method Summary collapse

Methods included from CucumberPostFailureBuffering

#buffer_post_failure, #catch_post_failures, #post_failure, #post_failure=

Constructor Details

#initializeSession

set up the global variables, reading from the config file



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/watirmark/session.rb', line 49

def initialize
  Watirmark.add_exit_task {
    closebrowser if config.closebrowseronexit || config.headless
    @driver.quit if config.webdriver.to_s.eql? 'sauce'
  }

  if config.webdriver.to_s.eql? 'firefox'
    config.firefox_profile = default_firefox_profile
  elsif config.webdriver.to_s.eql? 'firefox_proxy'
    config.firefox_profile = proxy_firefox_profile(config.proxy_host, config.proxy_port)
  end
end

Instance Method Details

#browserObject



36
37
38
# File 'lib/watirmark/session.rb', line 36

def browser
  Page.browser
end

#browser=(x) ⇒ Object



40
41
42
# File 'lib/watirmark/session.rb', line 40

def browser=(x)
  Page.browser = x
end

#closebrowserObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/watirmark/session.rb', line 143

def closebrowser
  begin
    Page.browser.close
  rescue Errno::ECONNREFUSED, Selenium::WebDriver::Error::WebDriverError
    # browser already closed or unavailable
  ensure
    Page.browser = nil
  end

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

#configObject



44
45
46
# File 'lib/watirmark/session.rb', line 44

def config
  Watirmark::Configuration.instance
end

#default_firefox_profileObject



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/watirmark/session.rb', line 62

def default_firefox_profile
  file_types = "text/comma-separated-values,text/csv,application/pdf, application/x-msdos-program, application/x-unknown-application-octet-stream,
          application/vnd.ms-powerpoint, application/excel, application/vnd.ms-publisher, application/x-unknown-message-rfc822, application/vnd.ms-excel,
          application/msword, application/x-mspublisher, application/x-tar, application/zip, application/x-gzip, application/x-stuffit,
          application/vnd.ms-works, application/powerpoint, application/rtf, application/postscript, application/x-gtar,
          video/quicktime, video/x-msvideo, video/mpeg, audio/x-wav, audio/x-midi, audio/x-aiff, text/plain, application/vnd.ms-excel [official],
          application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/msexcel, application/x-msexcel,
          application/x-excel, application/vnd.ms-excel, application/excel, application/x-ms-excel, application/x-dos_ms_excel,
          text/csv, text/comma-separated-values, application/octet-stream, application/haansoftxls, application/msexcell,
          application/softgrid-xls, application/vnd.ms-excel, x-softmaker-pm"

  if Configuration.instance.default_firefox_profile
    Watirmark.logger.info "Using firefox profile: #{Configuration.instance.default_firefox_profile}"
    profile = Selenium::WebDriver::Firefox::Profile.from_name Configuration.instance.default_firefox_profile
  else
    profile = Selenium::WebDriver::Firefox::Profile.new
  end
  profile.native_events = false
  if Configuration.instance.projectpath
    download_directory = File.join(Configuration.instance.projectpath, "reports", "downloads")
    download_directory.gsub!("/", "\\") if Selenium::WebDriver::Platform.windows?
    profile['browser.download.folderList'] = 2 # custom location
    profile['browser.download.dir'] = download_directory
    profile['browser.helperApps.neverAsk.saveToDisk'] = file_types
    profile['security.warn_entering_secure'] =  false
    profile['security.warn_submit_insecure'] = false
    profile['security.warn_entering_secure.show_once'] = false
    profile['security.warn_entering_weak'] =  false
    profile['security.warn_entering_weak.show_once'] =  false
    profile['security.warn_leaving_secure'] =  false
    profile['security.warn_leaving_secure.show_once'] =  false
    profile['security.warn_viewing_mixed'] =  false
    profile['security.warn_viewing_mixed.show_once'] =  false
    profile['security.mixed_content.block_active_content'] = false
  end
  if config.firebug
    unless RbConfig::CONFIG['host_os'].match('linux')
      warn "Firebug with Firefox only supported on Linux"
      return
    end
    path = (File.expand_path "../../../bin/firefox_extensions/firebug", __FILE__)
    file = 'addon-1843-latest.xpi'
    Dir.chdir(path) do
      File.delete(file) if File.exists?(file)
      `wget https://addons.mozilla.org/firefox/downloads/latest/1843/addon-1843-latest.xpi`
    end
    profile.add_extension("#{path}/#{file}")
  end
  profile["focusmanager.testmode"] = true
  profile
end

#getosObject



158
159
160
161
162
163
164
165
166
167
# File 'lib/watirmark/session.rb', line 158

def getos
  case RUBY_PLATFORM
    when /cygwin|mswin|mingw|bccwin|wince|emx/
      return "windows"
    when /darwin/
      return "mac"
    when /linux/
      return "linux"
  end
end

#newsessionObject



127
128
129
130
# File 'lib/watirmark/session.rb', line 127

def newsession
  closebrowser
  openbrowser
end

#openbrowserObject



132
133
134
135
136
137
138
139
140
141
# File 'lib/watirmark/session.rb', line 132

def openbrowser
  Watir.default_timeout = config.watir_timeout
  Watir.prefer_css = config.prefer_css
  Watir.always_locate = config.always_locate

  use_headless_display if config.headless
  Page.browser = new_watir_browser
  initialize_page_checkers
  Page.browser
end

#proxy_firefox_profile(hostname, port) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/watirmark/session.rb', line 114

def proxy_firefox_profile(hostname,port)
  profile = default_firefox_profile
  profile['network.proxy.http'] = hostname
  profile['network.proxy.http_port'] = port.to_i
  profile['network.proxy.ssl'] = hostname
  profile['network.proxy.ssl_port'] = port.to_i
  profile['network.proxy.ftp'] = hostname
  profile['network.proxy.ftp_port'] = port.to_i
  profile['network.proxy_type'] = 1
  profile['network.proxy.type'] = 1
  profile
end