-POLTERGEIST_OPTS = {
- window_size: [1200, 800],
- phantomjs_options: ['--ignore-ssl-errors=true'],
- inspector: true,
-}
+def available_port for_what
+ begin
+ Addrinfo.tcp("0.0.0.0", 0).listen do |srv|
+ port = srv.connect_address.ip_port
+ # Selenium needs an additional locking port, check if it's available
+ # and retry if necessary.
+ if for_what == 'selenium'
+ locking_port = port - 1
+ Addrinfo.tcp("0.0.0.0", locking_port).listen.close
+ end
+ STDERR.puts "Using port #{port} for #{for_what}"
+ return port
+ end
+ rescue Errno::EADDRINUSE, Errno::EACCES
+ retry
+ end
+end
+
+def selenium_opts
+ {
+ port: available_port('selenium'),
+ }
+end
+
+def poltergeist_opts
+ {
+ phantomjs_options: ['--ignore-ssl-errors=true'],
+ port: available_port('poltergeist'),
+ window_size: [1200, 800],
+ }
+end