Class: Capybara::Cuprite::Browser::Process
- Inherits:
-
Object
- Object
- Capybara::Cuprite::Browser::Process
- Defined in:
- lib/capybara/cuprite/browser/process.rb
Constant Summary collapse
- KILL_TIMEOUT =
2- BROWSER_PATH =
ENV.fetch("BROWSER_PATH", "chrome")
- BROWSER_HOST =
"127.0.0.1"- BROWSER_PORT =
"0"- DEFAULT_OPTIONS =
Chromium command line options peter.sh/experiments/chromium-command-line-switches/
{ "headless" => nil, "disable-gpu" => nil, "hide-scrollbars" => nil, "mute-audio" => nil, # Note: --no-sandbox is not needed if you properly setup a user in the container. # https://github.com/ebidel/lighthouse-ci/blob/master/builder/Dockerfile#L35-L40 # "no-sandbox" => nil, "enable-automation" => nil, "disable-web-security" => nil, }.freeze
Instance Attribute Summary collapse
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#ws_url ⇒ Object
readonly
Returns the value of attribute ws_url.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options) ⇒ Process
constructor
A new instance of Process.
- #restart ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(options) ⇒ Process
Returns a new instance of Process.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/capybara/cuprite/browser/process.rb', line 55 def initialize() @options = .fetch(:browser, {}) exe = [:path] || BROWSER_PATH @path = Cliver.detect(exe) unless @path = "Could not find an executable `#{exe}`. Try to make it " \ "available on the PATH or set environment varible for " \ "example BROWSER_PATH=\"/Applications/Chromium.app/Contents/MacOS/Chromium\"" raise Cliver::Dependency::NotFound.new() end window_size = .fetch(:window_size, [1024, 768]) @options = @options.merge("window-size" => window_size.join(",")) port = .fetch(:port, BROWSER_PORT) @options = @options.merge("remote-debugging-port" => port) host = .fetch(:host, BROWSER_HOST) @options = @options.merge("remote-debugging-address" => host) @options = DEFAULT_OPTIONS.merge(@options) end |
Instance Attribute Details
#host ⇒ Object (readonly)
Returns the value of attribute host.
28 29 30 |
# File 'lib/capybara/cuprite/browser/process.rb', line 28 def host @host end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
28 29 30 |
# File 'lib/capybara/cuprite/browser/process.rb', line 28 def @options end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
28 29 30 |
# File 'lib/capybara/cuprite/browser/process.rb', line 28 def pid @pid end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
28 29 30 |
# File 'lib/capybara/cuprite/browser/process.rb', line 28 def port @port end |
#ws_url ⇒ Object (readonly)
Returns the value of attribute ws_url.
28 29 30 |
# File 'lib/capybara/cuprite/browser/process.rb', line 28 def ws_url @ws_url end |
Class Method Details
.process_killer(pid) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/capybara/cuprite/browser/process.rb', line 34 def self.process_killer(pid) proc do begin if Capybara::Cuprite.windows? ::Process.kill("KILL", pid) else ::Process.kill("TERM", pid) start = Time.now while ::Process.wait(pid, ::Process::WNOHANG).nil? sleep 0.05 next unless (Time.now - start) > KILL_TIMEOUT ::Process.kill("KILL", pid) ::Process.wait(pid) break end end rescue Errno::ESRCH, Errno::ECHILD end end end |
.start(*args) ⇒ Object
30 31 32 |
# File 'lib/capybara/cuprite/browser/process.rb', line 30 def self.start(*args) new(*args).tap(&:start) end |
Instance Method Details
#restart ⇒ Object
104 105 106 107 |
# File 'lib/capybara/cuprite/browser/process.rb', line 104 def restart stop start end |
#start ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/capybara/cuprite/browser/process.rb', line 79 def start read_io, write_io = IO.pipe = { in: File::NULL } [:pgroup] = true unless Capybara::Cuprite.windows? if Capybara::Cuprite.mri? [:out] = [:err] = write_io end redirect_stdout(write_io) do cmd = [@path] + @options.map { |k, v| v.nil? ? "--#{k}" : "--#{k}=#{v}" } @pid = ::Process.spawn(*cmd, ) ObjectSpace.define_finalizer(self, self.class.process_killer(@pid)) end parse_ws_url(read_io) ensure close_io(read_io, write_io) end |
#stop ⇒ Object
98 99 100 101 102 |
# File 'lib/capybara/cuprite/browser/process.rb', line 98 def stop return unless @pid kill ObjectSpace.undefine_finalizer(self) end |