@@ -29,7 +29,6 @@ class Options < WebDriver::Options
29
29
# see: http://chromedriver.chromium.org/capabilities
30
30
CAPABILITIES = { args : 'args' ,
31
31
binary : 'binary' ,
32
- extensions : 'extensions' ,
33
32
local_state : 'localState' ,
34
33
prefs : 'prefs' ,
35
34
detach : 'detach' ,
@@ -40,6 +39,9 @@ class Options < WebDriver::Options
40
39
perf_logging_prefs : 'perfLoggingPrefs' ,
41
40
window_types : 'windowTypes' } . freeze
42
41
42
+ # NOTE: special handling of 'extensions' to validate when set instead of when used
43
+ attr_reader :extensions
44
+
43
45
# Create a new Options instance.
44
46
#
45
47
# @example
@@ -64,21 +66,23 @@ class Options < WebDriver::Options
64
66
# @option opts [Array<String>] :window_types A list of window types to appear in the list of window handles
65
67
#
66
68
67
- def initialize ( profile : nil , encoded_extensions : [ ] , **opts )
69
+ def initialize ( profile : nil , **opts )
68
70
super ( **opts )
69
71
70
72
@profile = profile
71
- @options [ :encoded_extensions ] = encoded_extensions
72
-
73
- @options [ :args ] ||= [ ]
74
- @options [ :prefs ] ||= { }
75
- @options [ :emulation ] ||= { }
76
- @options [ :local_state ] ||= { }
77
- @options [ :exclude_switches ] ||= [ ]
78
- @options [ :perf_logging_prefs ] ||= { }
79
- @options [ :window_types ] ||= [ ]
80
- @options [ :extensions ] ||= [ ]
81
- @options [ :extensions ] . each ( &method ( :validate_extension ) )
73
+
74
+ @options = { args : [ ] ,
75
+ prefs : { } ,
76
+ emulation : { } ,
77
+ extensions : [ ] ,
78
+ local_state : { } ,
79
+ exclude_switches : [ ] ,
80
+ perf_logging_prefs : { } ,
81
+ window_types : [ ] } . merge ( @options )
82
+
83
+ @encoded_extensions = @options . delete ( :encoded_extensions ) || [ ]
84
+ @extensions = [ ]
85
+ ( @options . delete ( :extensions ) ) . each ( &method ( :validate_extension ) )
82
86
end
83
87
84
88
#
@@ -93,7 +97,21 @@ def initialize(profile: nil, encoded_extensions: [], **opts)
93
97
94
98
def add_extension ( path )
95
99
validate_extension ( path )
96
- @options [ :extensions ] << path
100
+ end
101
+
102
+ #
103
+ # Add an extension by local path.
104
+ #
105
+ # @example
106
+ # extensions = ['/path/to/extension.crx', '/path/to/other.crx']
107
+ # options = Selenium::WebDriver::Chrome::Options.new
108
+ # options.extensions = extensions
109
+ #
110
+ # @param [Array<String>] :extensions A list of paths to (.crx) Chrome extensions to install on startup
111
+ #
112
+
113
+ def extensions = ( extensions )
114
+ extensions . each ( &method ( :validate_extension ) )
97
115
end
98
116
99
117
#
@@ -107,9 +125,8 @@ def add_extension(path)
107
125
#
108
126
109
127
def add_encoded_extension ( encoded )
110
- @options [ : encoded_extensions] << encoded
128
+ @encoded_extensions << encoded
111
129
end
112
- alias_method :encoded_extension= , :add_encoded_extension
113
130
114
131
#
115
132
# Add a command-line argument to use when starting Chrome.
@@ -178,15 +195,16 @@ def add_emulation(**opts)
178
195
private
179
196
180
197
def process_browser_options ( browser_options )
181
- options = browser_options [ KEY ]
198
+ options = browser_options [ self . class :: KEY ]
182
199
options [ 'binary' ] ||= binary_path if binary_path
183
- options [ 'args' ] << "--user-data-dir=#{ @profile . directory } " if @profile
184
- merge_extensions ( options )
185
- end
200
+ if @profile
201
+ options [ 'args' ] ||= [ ]
202
+ options [ 'args' ] << "--user-data-dir=#{ @profile . directory } "
203
+ end
204
+
205
+ return if ( @encoded_extensions + @extensions ) . empty?
186
206
187
- def merge_extensions ( browser_options )
188
- encoded_extensions = browser_options . delete ( :encoded_extensions )
189
- browser_options [ :extensions ] = extensions . map ( &method ( :encode_extension ) ) + encoded_extensions
207
+ options [ 'extensions' ] = @encoded_extensions + @extensions . map ( &method ( :encode_extension ) )
190
208
end
191
209
192
210
def binary_path
@@ -200,6 +218,8 @@ def encode_extension(path)
200
218
def validate_extension ( path )
201
219
raise Error ::WebDriverError , "could not find extension at #{ path . inspect } " unless File . file? ( path )
202
220
raise Error ::WebDriverError , "file was not an extension #{ path . inspect } " unless File . extname ( path ) == '.crx'
221
+
222
+ @extensions << path
203
223
end
204
224
205
225
def camelize? ( key )
0 commit comments