Sha256: 74a6aa2c4717bb539ed533708cdd896d2c5cd6f5d18d396da0009b5ea1b709dd

Contents?: true

Size: 1.82 KB

Versions: 3

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2023, by Samuel Williams.

require_relative 'generic'
require_relative 'process_group'

module Async
	module WebDriver
		module Bridge
			# A bridge to the Chrome browser using `chromedriver`.
			#
			# ``` ruby
			# begin
			# 	bridge = Async::WebDriver::Bridge::Chrome.start
			# 	client = Async::WebDriver::Client.open(bridge.endpoint)
			# ensure
			# 	bridge&.close
			# end
			# ```
			class Chrome < Generic
				# Create a new bridge to Chrome.
				# @parameter path [String] The path to the `chromedriver` executable.
				def initialize(path: "chromedriver")
					super()
					
					@path = path
					@process = nil
				end
				
				# @returns [String] The version of the `chromedriver` executable.
				def version
					::IO.popen([@path, "--version"]) do |io|
						return io.read
					end
				rescue Errno::ENOENT
					return nil
				end
				
				# @returns [Array(String)] The arguments to pass to the `chromedriver` executable.
				def arguments
					[
						"--port=#{self.port}",
					].compact
				end
				
				# Start the driver.
				def start
					@process ||= ProcessGroup.spawn(@path, *arguments)
					
					super
				end
				
				# Close the driver.
				def close
					super
					
					if @process
						@process.close
						@process = nil
					end
				end
				
				# The default capabilities for the Chrome browser which need to be provided when requesting a new session.
				# @parameter headless [Boolean] Whether to run the browser in headless mode.
				# @returns [Hash] The default capabilities for the Chrome browser.
				def default_capabilities(headless: true)
					{
						alwaysMatch: {
							browserName: "chrome",
							"goog:chromeOptions": {
								args: [headless ? "--headless" : nil].compact,
							}
						},
					}
				end
			end
		end
	end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
async-webdriver-0.3.1 lib/async/webdriver/bridge/chrome.rb
async-webdriver-0.3.0 lib/async/webdriver/bridge/chrome.rb
async-webdriver-0.2.0 lib/async/webdriver/bridge/chrome.rb