Class: Selenium::WebDriver::Options

Inherits:
Object
  • Object
show all
Defined in:
rb/lib/selenium/webdriver/common/options.rb

Constant Summary collapse

W3C_OPTIONS =
%i[browser_name browser_version platform_name accept_insecure_certs page_load_strategy proxy
set_window_rect timeouts unhandled_prompt_behavior strict_file_interactability
web_socket_url].freeze

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**opts) ⇒ Options

Returns a new instance of Options.



69
70
71
72
73
74
# File 'rb/lib/selenium/webdriver/common/options.rb', line 69

def initialize(**opts)
  self.class.set_capabilities

  @options = opts
  @options[:browser_name] = self.class::BROWSER
end

Class Attribute Details

.driver_pathObject (readonly)

Returns the value of attribute driver_path.



28
29
30
# File 'rb/lib/selenium/webdriver/common/options.rb', line 28

def driver_path
  @driver_path
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



67
68
69
# File 'rb/lib/selenium/webdriver/common/options.rb', line 67

def options
  @options
end

Class Method Details

.chrome(**opts) ⇒ Object



30
31
32
# File 'rb/lib/selenium/webdriver/common/options.rb', line 30

def chrome(**opts)
  Chrome::Options.new(**opts)
end

.edge(**opts) ⇒ Object Also known as: microsoftedge



43
44
45
# File 'rb/lib/selenium/webdriver/common/options.rb', line 43

def edge(**opts)
  Edge::Options.new(**opts)
end

.firefox(**opts) ⇒ Object



34
35
36
# File 'rb/lib/selenium/webdriver/common/options.rb', line 34

def firefox(**opts)
  Firefox::Options.new(**opts)
end

.ie(**opts) ⇒ Object Also known as: internet_explorer



38
39
40
# File 'rb/lib/selenium/webdriver/common/options.rb', line 38

def ie(**opts)
  IE::Options.new(**opts)
end

.safari(**opts) ⇒ Object



48
49
50
# File 'rb/lib/selenium/webdriver/common/options.rb', line 48

def safari(**opts)
  Safari::Options.new(**opts)
end

.set_capabilitiesObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'rb/lib/selenium/webdriver/common/options.rb', line 52

def set_capabilities
  (W3C_OPTIONS + self::CAPABILITIES.keys).each do |key|
    next if method_defined? key

    define_method key do
      @options[key]
    end

    define_method "#{key}=" do |value|
      @options[key] = value
    end
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



92
93
94
95
96
# File 'rb/lib/selenium/webdriver/common/options.rb', line 92

def ==(other)
  return false unless other.is_a? self.class

  as_json == other.as_json
end

#add_option(name, value = nil) ⇒ Object

Add a new option not yet handled by bindings.

Examples:

Leave Chrome open when chromedriver is killed

options = Selenium::WebDriver::Chrome::Options.new
options.add_option(:detach, true)

Parameters:

  • name (String, Symbol)

    Name of the option

  • value (Boolean, String, Integer) (defaults to: nil)

    Value of the option



87
88
89
90
# File 'rb/lib/selenium/webdriver/common/options.rb', line 87

def add_option(name, value = nil)
  @options[name.keys.first] = name.values.first if value.nil? && name.is_a?(Hash)
  @options[name] = value
end

#as_jsonObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'rb/lib/selenium/webdriver/common/options.rb', line 104

def as_json(*)
  options = @options.dup

  w3c_options = process_w3c_options(options)

  self.class::CAPABILITIES.each do |capability_alias, capability_name|
    capability_value = options.delete(capability_alias)
    options[capability_name] = capability_value if !capability_value.nil? && !options.key?(capability_name)
  end
  browser_options = defined?(self.class::KEY) ? {self.class::KEY => options} : options

  process_browser_options(browser_options)
  generate_as_json(w3c_options.merge(browser_options))
end