Best Selenium code snippet using Selenium.WebDriver.add_extensions
driver.rb
Source:driver.rb
...64 def initialize(bridge: nil, listener: nil, **opts)65 @service = nil66 @devtools = nil67 bridge ||= create_bridge(**opts)68 add_extensions(bridge.browser)69 @bridge = listener ? Support::EventFiringBridge.new(bridge, listener) : bridge70 end71 def inspect72 format '#<%<class>s:0x%<hash>x browser=%<browser>s>', class: self.class, hash: hash * 2,73 browser: bridge.browser.inspect74 end75 #76 # information about whether a remote end is in a state in which it can create new sessions,77 # and may include additional meta information.78 #79 # @return [Hash]80 #81 def status82 @bridge.status83 end84 #85 # @return [Navigation]86 # @see Navigation87 #88 def navigate89 @navigate ||= WebDriver::Navigation.new(bridge)90 end91 #92 # @return [TargetLocator]93 # @see TargetLocator94 #95 def switch_to96 @switch_to ||= WebDriver::TargetLocator.new(bridge)97 end98 #99 # @return [Manager]100 # @see Manager101 #102 def manage103 bridge.manage104 end105 #106 # @return [ActionBuilder]107 # @see ActionBuilder108 #109 def action110 bridge.action111 end112 def mouse113 bridge.mouse114 end115 def keyboard116 bridge.keyboard117 end118 #119 # Opens the specified URL in the browser.120 #121 def get(url)122 navigate.to(url)123 end124 #125 # Get the URL of the current page126 #127 # @return [String]128 #129 def current_url130 bridge.url131 end132 #133 # Get the title of the current page134 #135 # @return [String]136 #137 def title138 bridge.title139 end140 #141 # Get the source of the current page142 #143 # @return [String]144 #145 def page_source146 bridge.page_source147 end148 #149 # Quit the browser150 #151 def quit152 bridge.quit153 ensure154 @service&.stop155 @devtools&.close156 end157 #158 # Close the current window, or the browser if no windows are left.159 #160 def close161 bridge.close162 end163 #164 # Get the window handles of open browser windows.165 #166 # @return [Array]167 # @see TargetLocator#window168 #169 def window_handles170 bridge.window_handles171 end172 #173 # Get the current window handle174 #175 # @return [String]176 #177 def window_handle178 bridge.window_handle179 end180 #181 # Execute the given JavaScript182 #183 # @param [String] script184 # JavaScript source to execute185 # @param [WebDriver::Element, Integer, Float, Boolean, NilClass, String, Array] args186 # Arguments will be available in the given script in the 'arguments' pseudo-array.187 #188 # @return [WebDriver::Element,Integer,Float,Boolean,NilClass,String,Array]189 # The value returned from the script.190 #191 def execute_script(script, *args)192 bridge.execute_script(script, *args)193 end194 # Execute an asynchronous piece of JavaScript in the context of the195 # currently selected frame or window. Unlike executing196 # execute_script (synchronous JavaScript), scripts197 # executed with this method must explicitly signal they are finished by198 # invoking the provided callback. This callback is always injected into the199 # executed function as the last argument.200 #201 # @param [String] script202 # JavaScript source to execute203 # @param [WebDriver::Element,Integer, Float, Boolean, NilClass, String, Array] args204 # Arguments to the script. May be empty.205 #206 # @return [WebDriver::Element,Integer,Float,Boolean,NilClass,String,Array]207 #208 def execute_async_script(script, *args)209 bridge.execute_async_script(script, *args)210 end211 #-------------------------------- sugar --------------------------------212 #213 # driver.first(id: 'foo')214 #215 alias_method :first, :find_element216 #217 # driver.all(class: 'bar') #=> [#<WebDriver::Element:0x1011c3b88, ...]218 #219 alias_method :all, :find_elements220 #221 # driver.script('function() { ... };')222 #223 alias_method :script, :execute_script224 # Get the first element matching the given selector. If given a225 # String or Symbol, it will be used as the id of the element.226 #227 # @param [String,Hash] sel id or selector228 # @return [WebDriver::Element]229 #230 # Examples:231 #232 # driver['someElementId'] #=> #<WebDriver::Element:0x1011c3b88>233 # driver[:tag_name => 'div'] #=> #<WebDriver::Element:0x1011c3b88>234 #235 def [](sel)236 sel = {id: sel} if sel.is_a?(String) || sel.is_a?(Symbol)237 find_element sel238 end239 def browser240 bridge&.browser241 end242 def capabilities243 bridge.capabilities244 end245 #246 # @api private247 # @see SearchContext248 #249 def ref250 [:driver, nil]251 end252 private253 attr_reader :bridge254 def create_bridge(**opts)255 opts[:url] ||= service_url(opts)256 caps = opts.delete(:capabilities)257 # NOTE: This is deprecated258 cap_array = caps.is_a?(Hash) ? [caps] : Array(caps)259 desired_capabilities = opts.delete(:desired_capabilities)260 if desired_capabilities261 WebDriver.logger.deprecate(':desired_capabilities as a parameter for driver initialization',262 ':capabilities with an Array value of capabilities/options if necessary',263 id: :desired_capabilities)264 desired_capabilities = Remote::Capabilities.new(desired_capabilities) if desired_capabilities.is_a?(Hash)265 cap_array << desired_capabilities266 end267 options = opts.delete(:options)268 if options269 WebDriver.logger.deprecate(':options as a parameter for driver initialization',270 ':capabilities with an Array of value capabilities/options if necessary',271 id: :browser_options)272 cap_array << options273 end274 capabilities = generate_capabilities(cap_array)275 bridge_opts = {http_client: opts.delete(:http_client), url: opts.delete(:url)}276 raise ArgumentError, "Unable to create a driver with parameters: #{opts}" unless opts.empty?277 bridge = Remote::Bridge.new(**bridge_opts)278 bridge.create_session(capabilities)279 bridge280 end281 def generate_capabilities(cap_array)282 cap_array.map { |cap|283 if cap.is_a? Symbol284 cap = Remote::Capabilities.send(cap)285 elsif cap.is_a? Hash286 new_message = 'Capabilities instance initialized with the Hash, or build values with Options class'287 WebDriver.logger.deprecate("passing a Hash value to :capabilities",288 new_message,289 id: :capabilities_hash)290 cap = Remote::Capabilities.new(cap)291 elsif !cap.respond_to? :as_json292 msg = ":capabilities parameter only accepts objects responding to #as_json which #{cap.class} does not"293 raise ArgumentError, msg294 end295 cap&.as_json296 }.inject(:merge) || Remote::Capabilities.send(browser || :new)297 end298 def service_url(opts)299 service_config = opts.delete(:service)300 %i[driver_opts driver_path port].each do |key|301 next unless opts.key? key302 WebDriver.logger.deprecate(":#{key}", ':service with an instance of Selenium::WebDriver::Service',303 id: "service_#{key}".to_sym)304 end305 service_config ||= Service.send(browser,306 args: opts.delete(:driver_opts),307 path: opts.delete(:driver_path),308 port: opts.delete(:port))309 @service = service_config.launch310 @service.uri311 end312 def screenshot313 bridge.screenshot314 end315 def add_extensions(browser)316 extensions = case browser317 when :chrome, :msedge318 Chrome::Driver::EXTENSIONS319 when :firefox320 Firefox::Driver::EXTENSIONS321 when :safari, :safari_technology_preview322 Safari::Driver::EXTENSIONS323 else324 []325 end326 extensions.each { |extension| extend extension }327 end328 end # Driver329 end # WebDriver...
add_extensions
Using AI Code Generation
1 def add_extensions(*extensions)2 @bridge.add_extension(extension)3driver.add_extensions('path/to/extension.crx')4driver.add_extensions('path/to/extension1.crx', 'path/to/extension2.crx')
add_extensions
Using AI Code Generation
1driver.add_extensions("path/to/extension.xpi")2profile.add_extensions("path/to/extension.xpi")3binary.add_extensions("path/to/extension.xpi")4profile.add_extensions("path/to/extension.xpi")5extensions.add_extensions("path/to/extension.xpi")6extension.add_extensions("path/to/extension.xpi")7preferences.add_extensions("path/to/extension.xpi")
add_extensions
Using AI Code Generation
1driver.add_extensions('path/to/extension.crx')2driver.add_extensions('path/to/extension1.crx', 'path/to/extension2.crx')3driver.add_extensions('path/to/extension.zip')
add_extensions
Using AI Code Generation
1Selenium::WebDriver.for(:chrome).add_extension('path/to/extension')2Selenium::WebDriver::Chrome::Options.new.add_extension('path/to/extension')3Selenium::WebDriver::Chrome::Driver.new(Selenium::WebDriver::Chrome::Options.new.add_extension('path/to/extension'))4Selenium::WebDriver::Chrome::Service.new.add_extension('path/to/extension')5Selenium::WebDriver::Chrome::Profile.new.add_extension('path/to/extension')6Selenium::WebDriver::Chrome::Profile.new.add_extension('path/to/extension')7Selenium::WebDriver::Chrome::Profile.new.add_extension('path/to/extension')8Selenium::WebDriver::Chrome::Profile.new.add_extension('path/to/extension')
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!