Best Selenium code snippet using Selenium.WebDriver.Chrome.add_argument
driver.rb
Source:driver.rb
...53 caps = opts.delete(:desired_capabilities) { Remote::Capabilities.chrome }54 options = opts.delete(:options) { Options.new }55 args = opts.delete(:args) || opts.delete(:switches)56 if args57 WebDriver.logger.deprecate ':args or :switches', 'Selenium::WebDriver::Chrome::Options#add_argument'58 raise ArgumentError, ':args must be an Array of Strings' unless args.is_a? Array59 args.each { |arg| options.add_argument(arg.to_s) }60 end61 profile = opts.delete(:profile)62 if profile63 WebDriver.logger.deprecate 'Selenium::WebDriver::Chrome::Driver#new with `:profile` parameter',64 'Selenium::WebDriver::Chrome::Options#profile or Options#add_option'65 profile = profile.as_json66 if options.args.none?(&/user-data-dir/.method(:match?))67 options.add_argument("--user-data-dir=#{profile['directory']}")68 end69 if profile['extensions']70 WebDriver.logger.deprecate 'Selenium::WebDriver::Chrome::Profile#extensions',71 'Selenium::WebDriver::Chrome::Options#add_extension'72 profile['extensions'].each do |extension|73 options.add_encoded_extension(extension)74 end75 end76 end77 if opts.key?(:detach)78 WebDriver.logger.deprecate 'Selenium::WebDriver::Chrome::Driver#new with `:detach` parameter',79 'Selenium::WebDriver::Chrome::Options#new or Options#add_option'80 options.add_option(:detach, opts.delete(:detach))81 end...
sharedWebDriver.rb
Source:sharedWebDriver.rb
...12 when "ui"13 Selenium::WebDriver::Chrome.driver_path=tconfigObj["DRIVER_PATH"]14 options = Selenium::WebDriver::Chrome::Options.new15 options = enable_mobile_device(options, tconfigObj)16 options.add_argument('start-maximized')17 @driver = Selenium::WebDriver.for(:chrome, options: options)18 when "headless-chrome"19 Selenium::WebDriver::Chrome.driver_path=tconfigObj["DRIVER_PATH"]20 options = Selenium::WebDriver::Chrome::Options.new21 options.add_argument('--headless')22 options = enable_mobile_device(options, tconfigObj)23 options.add_argument('start-maximized')24 @driver = Selenium::WebDriver.for(:chrome, options: options)25 when "headless-cancary"26 Selenium::WebDriver::Chrome.driver_path=tconfigObj["DRIVER_PATH"]27 caps = Selenium::WebDriver::Remote::Capabilities.chrome(chromeOptions: {28 binary: "/Applications/Google\ Chrome\ Canary.app/Contents/MacOS/Google\ Chrome\ Canary", 29 args: [ "--headless" ]})30 @driver = Selenium::WebDriver.for(:chrome, desired_capabilities: caps)31 when "browserstack"32 tconfigObjBrowserStack = load_config(Dir.pwd + "/configuration/user_browserstack.json")33 caps = Selenium::WebDriver::Remote::Capabilities.new34 caps["os"] = tconfigObjBrowserStack["BROWSERSTACK_OS"]35 caps["os_version"] = tconfigObjBrowserStack["BROWSERSTACK_OSVERSION"]36 caps["browser"] = tconfigObjBrowserStack["BROWSERSTACK_BROWSER"]37 caps["browser_version"] = tconfigObjBrowserStack["BROWSERSTACK_BROWSERVERSION"]38 caps["resolution"] = tconfigObjBrowserStack["BROWSERSTACK_RESOLUTION"]39 caps["browserstack.local"] = tconfigObjBrowserStack["BROWSERSTACK_LOCAL"]40 caps["browserstack.selenium_version"] = tconfigObjBrowserStack["BROWSERSTACK_SELENIUMVERSION"]41 remoteHunUrl = "http://%s:%s@hub-cloud.browserstack.com/wd/hub" % [tconfigObjBrowserStack["BROWSERSTACK_USERNAME"], tconfigObjBrowserStack["BROWSERSTACK_ACCESSKEY"]]42 @driver = Selenium::WebDriver.for(:remote, :url => remoteHunUrl, :desired_capabilities => caps)43 when "hub"44 tconfigObjHub = load_config(Dir.pwd + "/configuration/user_hub.json")45 caps = Selenium::WebDriver::Remote::Capabilities.new46 caps["browserName"] = tconfigObjHub["NODE_BROWSER_NAME"]47 caps["browserVersion"] = tconfigObjHub["NODE_BROWSER_VERSION"]48 caps["platform"] = tconfigObjHub["NODE_PLATFORM"]49 caps["seleniumVersion"] = tconfigObjHub["NODE_SELENIUM_VERSION"]50 @driver = Selenium::WebDriver.for(:remote, :url => "%s/wd/hub" % [tconfigObjHub["HUB_URL"]], :desired_capabilities => caps)51 else52 Selenium::WebDriver::Chrome.driver_path=tconfigObj["DRIVER_PATH"]53 options = Selenium::WebDriver::Chrome::Options.new54 options = enable_mobile_device(options, tconfigObj)55 options.add_argument('start-maximized')56 @driver = Selenium::WebDriver.for(:chrome, options: options)57 end58 @driver.manage.window.move_to(0, 0)59 @wait = Selenium::WebDriver::Wait.new(:timeout => 30) # seconds60 @listWait = Selenium::WebDriver::Wait.new(:timeout => 120) # seconds61 @shortWait = Selenium::WebDriver::Wait.new(:timeout => 3) # seconds62 @normalWait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds63 end64 65 def get_driver()66 return @driver67 end68 69 def get_waitor()70 return @wait71 end72 73 def get_short_waitor()74 return @shortWait75 end7677 def get_normal_waitor()78 return @normalWait79 end8081 def get_list_waitor()82 return @listWait83 end8485 private def enable_mobile_device(options, tconfigObj)86 if tconfigObj["USER_AGENT"] != "" && tconfigObj["DEVICE_NAME"] != ""87 options.add_argument('user-agent=' + tconfigObj["USER_AGENT"])88 options.add_emulation(:device_name => tconfigObj["DEVICE_NAME"])89 end90 return options91 end9293end
...
chrome.rb
Source:chrome.rb
...4 Capybara.register_driver name do |app|5 options = Selenium::WebDriver::Chrome::Options.new6 if ActiveRecord::Type::Boolean.new.cast(ENV['OPENPROJECT_TESTING_NO_HEADLESS'])7 # Maximize the window however large the available space is8 options.add_argument('--start-maximized')9 # Open dev tools for quick access10 if ActiveRecord::Type::Boolean.new.cast(ENV['OPENPROJECT_TESTING_AUTO_DEVTOOLS'])11 options.add_argument('--auto-open-devtools-for-tabs')12 end13 else14 options.add_argument('--window-size=1920,1080')15 options.add_argument('--headless')16 end17 options.add_argument('--no-sandbox')18 options.add_argument('--disable-gpu')19 options.add_argument('--disable-popup-blocking')20 options.add_argument("--lang=#{language}")21 # This is REQUIRED for running in a docker container22 # https://github.com/grosser/parallel_tests/issues/65823 options.add_argument('--disable-dev-shm-usage')24 options.add_preference(:download,25 directory_upgrade: true,26 prompt_for_download: false,27 default_directory: DownloadList::SHARED_PATH.to_s)28 options.add_preference(:browser, set_download_behavior: { behavior: 'allow' })29 capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(30 'goog:loggingPrefs': { browser: 'ALL' }31 )32 yield(options, capabilities) if block_given?33 client = Selenium::WebDriver::Remote::Http::Default.new34 client.read_timeout = 18035 client.open_timeout = 18036 is_grid = ENV['SELENIUM_GRID_URL'].present?37 driver_opts = {38 browser: is_grid ? :remote : :chrome,39 capabilities: [capabilities, options],40 http_client: client41 }42 if is_grid43 driver_opts[:url] = ENV['SELENIUM_GRID_URL']44 else45 if Webdrivers::ChromeFinder.location == '/snap/bin/chromium'46 # make chromium snap install work out-of-the-box47 # See https://stackoverflow.com/a/65121582/17766548 chromedriver_path = '/snap/bin/chromium.chromedriver'49 end50 driver_opts[:service] = ::Selenium::WebDriver::Service.chrome(51 path: chromedriver_path,52 args: { verbose: true, log_path: '/tmp/chromedriver.log' }53 )54 end55 driver = Capybara::Selenium::Driver.new app, **driver_opts56 if !is_grid57 # Enable file downloads in headless mode58 # https://bugs.chromium.org/p/chromium/issues/detail?id=69648159 bridge = driver.browser.send :bridge60 bridge.http.call :post,61 "/session/#{bridge.session_id}/chromium/send_command",62 cmd: 'Page.setDownloadBehavior',63 params: { behavior: 'allow', downloadPath: DownloadList::SHARED_PATH.to_s }64 end65 driver66 end67 Capybara::Screenshot.register_driver(name) do |driver, path|68 driver.browser.save_screenshot(path)69 end70end71register_chrome 'en'72# Register german locale for custom field decimal test73register_chrome 'de'74Billy.configure do |c|75 c.proxy_host = Capybara.server_host76 if Capybara.server_port77 c.proxy_port = Capybara.server_port + 100078 end79end80# Register mocking proxy driver81register_chrome 'en', name: :chrome_billy do |options, capabilities|82 options.add_argument("proxy-server=#{Billy.proxy.host}:#{Billy.proxy.port}")83 options.add_argument("proxy-bypass-list=127.0.0.1;localhost;#{Capybara.server_host}")84 capabilities[:acceptInsecureCerts] = true85end86# Register Revit add in87register_chrome 'en', name: :chrome_revit_add_in do |options, _capabilities|88 options.add_argument("user-agent='foo bar Revit'")89end...
selenium_helper.rb
Source:selenium_helper.rb
...3require 'capybara/rspec'4require 'selenium/webdriver'5require 'yaml'6def register_base_driver_options(options)7 options.add_argument('--no-sandbox')8 options.add_argument('--start-maximized')9 options.add_argument('--disable-dev-shm-usage')10 options.add_argument("--remote-debugging-port=9222")11 options.add_argument('--window-size=1900,1400')12 options.add_argument('--default_max_wait_time=7')13 options.add_argument('--server_port=59876')14 options.add_argument('--enable-features=NetworkService,NetworkServiceInProcess')15 options.add_argument('--user-agent=automation-tests')16end17def build_http_client18 ::Selenium::WebDriver::Remote::Http::Default.new.tap do |client|19 client.read_timeout = 120 # Default: 6020 client.open_timeout = 120 # Default: 6021 end22end23Capybara.register_driver :selenium_chrome do |app|24 options = ::Selenium::WebDriver::Chrome::Options.new25 register_base_driver_options(options)26 Capybara::Selenium::Driver.new(app, browser: :chrome, capabilities: options, http_client: build_http_client)27end28Capybara.register_driver :selenium_chrome_headless do |app|29 options = ::Selenium::WebDriver::Chrome::Options.new30 options.add_argument('--headless')31 options.add_argument('--disable-gpu')32 register_base_driver_options(options)33 Capybara::Selenium::Driver.new(app, browser: :chrome, capabilities: options, http_client: build_http_client)34end35Capybara.register_driver :selenium_chrome_incognito do |app|36 options = ::Selenium::WebDriver::Chrome::Options.new37 options.add_argument('--incognito')38 register_base_driver_options(options)39 Capybara::Selenium::Driver.new(app, browser: :chrome, capabilities: options, http_client: build_http_client)40end41Capybara.register_driver :selenium_chrome_headless_incognito do |app|42 options = ::Selenium::WebDriver::Chrome::Options.new43 options.add_argument('--headless')44 options.add_argument('--incognito')45 register_base_driver_options(options)46 Capybara::Selenium::Driver.new(app, browser: :chrome, capabilities: options, http_client: build_http_client)47end48Capybara.register_driver :remote_session do |app|49 browser_caps = ENV.fetch('REMOTE_BROWSER_DEFAULT_CAPS', 'windows_chrome_caps')50 config = YAML.load_file('config/remote_browser_config.yml')51 options = config['common_caps'].merge(config[browser_caps])52 options['name'] = RSpec.current_example.metadata[:feature].to_s53 # bs_options = ::Selenium::WebDriver::Chrome::Options.new54 # register_base_driver_options(bs_options)55 # options.merge(bs_options)56 if ENV['MOBILE_WEB'] == 'true'57 options['name'] = RSpec.current_example.metadata[:layout].to_s58 url = "http://#{user}:#{key}@#{config['server']}"...
base.rb
Source:base.rb
...37# end38# # Selenium::WebDriver::Chrome.driver_path='./features/Drivers/chromedriver'39# # Selenium::WebDriver::Chrome::Service::driver_path='./features/Drivers/chromedriver'40# # # chrome_options = Selenium::WebDriver::Chrome::Options.new41# # # # options.add_argument('--headless')42# # # # chrome_options = Options()43# # # chrome_options.add_argument('--headless')44# # # chrome_options.add_argument('--no-sandbox')45# # # chrome_options.add_argument('--disable-dev-shm-usage')46# # # @driver = Selenium::WebDriver.for :chrome, options: chrome_options47# # # @driver.manage.timeouts.implicit_wait = 120 # seconds48# # # @driver = Selenium::WebDriver::for :chrome49# # # @driver = Selenium::WebDriver::for :firefox50# # # @driver.manage.window.resize_to(1440,900)51# # # @driver.manage.window.resize_to(411,823)52# # # @driver.manage.window.maximize53# def teardown54# @browser.quit55# end56# def browser_name57# (ENV[âBROWSERâ] ||= âfirefoxâ).downcase.to_sym58# end59# def environment...
browser.rb
Source:browser.rb
...56 end57 end58 private59 def headless_chrome_browser_options60 capabilities.add_argument("--headless")61 capabilities.add_argument("--disable-gpu") if Gem.win_platform?62 capabilities63 end64 def headless_firefox_browser_options65 capabilities.add_argument("-headless")66 capabilities67 end68 end69 end70end...
env.rb
Source:env.rb
...5require 'require_all'6require_relative '../../pages/all_pages'7Selenium::WebDriver::Chrome::Service.driver_path="/usr/bin/google-chrome"8options = Selenium::WebDriver::Chrome::Options.new9options.add_argument('--headless')10options.add_argument('--disable-gpu')11options.add_argument('window-size=1024,768')12options.add_argument('--no-sandbox')13options.add_argument('disable-dev-shm-usage')14options = Selenium::WebDriver::Chrome::Options.new15options.add_argument('--headless')16Selenium::WebDriver.for :chrome, options: options17# Chrome non-headless driver18Capybara.register_driver :selenium do |app|19 Capybara::Selenium::Driver.new(app, browser: :chrome)20end21# Chrome headless driver22Capybara.register_driver :headless_chrome do |app|23 caps = Selenium::WebDriver::Remote::Capabilities.chrome(loggingPrefs: { browser: 'ALL' })24 opts = Selenium::WebDriver::Chrome::Options.new25 chrome_args = %w[--headless]26 chrome_args.each { |arg| opts.add_argument(arg) }27 Capybara::Selenium::Driver.new(app, browser: :chrome, options: opts, desired_capabilities: caps)28end29# Switch between :chrome / :headless_chrome to see tests run in chrome30case ENV['HEADLESS']31when 'true', 1, nil32 Capybara.javascript_driver = :headless_chrome33else34 Capybara.javascript_driver = :selenium35end36Before do37 Capybara.current_driver = :headless_chrome38end...
add_argument
Using AI Code Generation
1driver.find_element(:name, 'q').send_keys 'Selenium'2driver.find_element(:name, 'btnK').click3options.add_argument('--start-maximized')4driver.find_element(:name, 'q').send_keys 'Selenium'5driver.find_element(:name, 'btnK').click6options.add_preference(:download, default_directory: 'C:\Users\Downloads')7driver.find_element(:name, 'q').send_keys 'Selenium'8driver.find_element(:name, 'btnK').click9options.add_extension('C:\Users\Downloads\extension.crx')10driver.find_element(:name, 'q').send_keys 'Selenium'11driver.find_element(:name, 'btnK').click12options.add_encoded_extension('C:\Users\Downloads\extension.crx')13driver.find_element(:name, 'q').send_keys 'Selenium'14driver.find_element(:name,
add_argument
Using AI Code Generation
1driver.find_element(:name, "q").send_keys "Selenium WebDriver"2driver.find_element(:name, "btnK").click3options.add_argument('--headless')4options.add_argument('--disable-gpu')5driver.find_element(:name, "q").send_keys "Selenium WebDriver"6driver.find_element(:name, "btnK").click7options.add_argument('--headless')8options.add_argument('--disable-gpu')9driver.find_element(:name, "q").send_keys "Selenium WebDriver"10driver.find_element(:name, "btnK").click11options.add_argument('--headless')12options.add_argument('--disable-gpu')13driver.find_element(:name, "q").send_keys "Selenium WebDriver"14driver.find_element(:name, "btnK").click
add_argument
Using AI Code Generation
1driver.find_element(:name, "q").send_keys "Selenium WebDriver"2driver.find_element(:name, "btnK").click3options.add_argument('--disable-notifications')4driver.find_element(:name, "q").send_keys "Selenium WebDriver"5driver.find_element(:name, "btnK").click6options.add_preference(:profile, {default_content_setting_values: {notifications: 2}})7driver.find_element(:name, "q").send_keys "Selenium WebDriver"8driver.find_element(:name, "btnK").click9options.add_extension("C:/Users/MyPC/Downloads/extension_0_9_2_0.crx")10driver.find_element(:name, "q").send_keys "Selenium WebDriver"11driver.find_element(:name, "btnK").click
add_argument
Using AI Code Generation
1driver.manage.add_argument('--disable-infobars')2driver.manage.add_option(:args, ['--disable-infobars'])3driver.manage.add_option('args', ['--disable-infobars'])4driver.manage.add_option('args', '--disable-infobars')5driver.manage.add_option('args', '--disable-infobars --start-maximized')6driver.manage.add_option('args', ['--disable-infobars', '--start-maximized'])7driver.manage.add_option('args', ['--disable-infobars', '--start-maximized', '--incognito'])
add_argument
Using AI Code Generation
1element = driver.find_element(name: 'q')2element = driver.find_element(name: 'q')3element = driver.find_element(name: 'q')4element = driver.find_element(name: 'q')
add_argument
Using AI Code Generation
1options.add_argument("--headless")2options.add_option(:args, ["--headless"])3options.add_option(:args, "--headless")4options.add_option(:args, "--headless", "--disable-gpu")
add_argument
Using AI Code Generation
1options.add_argument('--headless')2options.add_argument('--disable-gpu')3driver.find_element(:name, "q").send_keys "Selenium WebDriver"4driver.find_element(:name, "btnK").click5options.add_argument('--headless')6options.add_argument('--disable-gpu')7driver.find_element(:name, "q").send_keys "Selenium WebDriver"8driver.find_element(:name, "btnK").click9options.add_argument('--headless')10options.add_argument('--disable-gpu')
add_argument
Using AI Code Generation
1driver.manage.add_argrmeni('--disable-infobarv')2driver.manage.add_option(:args, ['--disable-infobars'])3driver.manage.add_option('args', ['--disable-infobars'])4driver.find_element(:name, "q").send_keys "Selenium WebDriver"5driver.manage.add_option('args', '--disable-infobars')6rivr.manage.adoption('args', '--disabl-infobars --start-maimized')7driver.manag.add_optio('arg', ['--dsable-infbars', '--start-maximized'])8driver.manage.add_option('args', ['--disable-infobars', '--start-maximized', '--incognito'])
add_argument
Using AI Code Generation
1element = driver.find_element(name: 'q')2element = driver.find_element(name: 'q')3element = driver.find_element(name: 'q')4element = driver.find_element(name: 'q')
add_argument
Using AI Code Generation
1options.add_argument("--headless")2options.add_option(:args, ["--headless"])3options.add_option(:args, "--headless")4options.add_option(:args, "--headless", "--disable-gpu")5driver.find_element(:name, "q").send_keys "Selenium WebDriver"6driver.find_element(:name, "btnK").click7options.add_argument('--disable-notifications')8driver.find_element(:name, "q").send_keys "Selenium WebDriver"9driver.find_element(:name, "btnK").click10options.add_preference(:profile, {default_content_setting_values: {notifications: 2}})11driver.find_element(:name, "q").send_keys "Selenium WebDriver"12driver.find_element(:name, "btnK").click13options.add_extension("C:/Users/MyPC/Downloads/extension_0_9_2_0.crx")14driver.find_element(:name, "q").send_keys "Selenium WebDriver"15driver.find_element(:name, "btnK").click
add_argument
Using AI Code Generation
1driver.manage.add_argument('--disable-infobars')2driver.manage.add_option(:args, ['--disable-infobars'])3driver.manage.add_option('args', ['--disable-infobars'])4driver.manage.add_option('args', '--disable-infobars')5driver.manage.add_option('args', '--disable-infobars --start-maximized')6driver.manage.add_option('args', ['--disable-infobars', '--start-maximized'])7driver.manage.add_option('args', ['--disable-infobars', '--start-maximized', '--incognito'])
add_argument
Using AI Code Generation
1driver.find_element(:name, 'q').send_keys 'Selenium'2driver.find_element(:name, 'btnK').click3options.add_argument('--start-maximized')4driver.find_element(:name, 'q').send_keys 'Selenium'5driver.find_element(:name, 'btnK').click6options.add_preference(:download, default_directory: 'C:\Users\Downloads')7driver.find_element(:name, 'q').send_keys 'Selenium'8driver.find_element(:name, 'btnK').click9options.add_extension('C:\Users\Downloads\extension.crx')10driver.find_element(:name, 'q').send_keys 'Selenium'11driver.find_element(:name, 'btnK').click12options.add_encoded_extension('C:\Users\Downloads\extension.crx')13driver.find_element(:name, 'q').send_keys 'Selenium'14driver.find_element(:name,
add_argument
Using AI Code Generation
1driver.find_element(:name, "q").send_keys "Selenium WebDriver"2driver.find_element(:name, "btnK").click3options.add_argument('--disable-notifications')4driver.find_element(:name, "q").send_keys "Selenium WebDriver"5driver.find_element(:name, "btnK").click6options.add_preference(:profile, {default_content_setting_values: {notifications: 2}})7driver.find_element(:name, "q").send_keys "Selenium WebDriver"8driver.find_element(:name, "btnK").click9options.add_extension("C:/Users/MyPC/Downloads/extension_0_9_2_0.crx")10driver.find_element(:name, "q").send_keys "Selenium WebDriver"11driver.find_element(:name, "btnK").click
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!!