Best Selenium code snippet using Selenium.WebDriver.Chrome.add_preference
driver_test.rb
Source:driver_test.rb
...46 test "define extra capabilities using chrome" do47 driver = ActionDispatch::SystemTesting::Driver.new(:selenium, screen_size: [1400, 1400], using: :chrome) do |option|48 option.add_argument("start-maximized")49 option.add_emulation(device_name: "iphone 6")50 option.add_preference(:detach, true)51 end52 driver.use53 browser_options = driver.__send__(:browser_options)54 expected = {55 "goog:chromeOptions" => {56 "args" => ["start-maximized"],57 "mobileEmulation" => { "deviceName" => "iphone 6" },58 "prefs" => { "detach" => true }59 },60 "browserName" => "chrome"61 }62 assert_equal expected, browser_options[:options].as_json63 end64 test "define extra capabilities using headless_chrome" do65 driver = ActionDispatch::SystemTesting::Driver.new(:selenium, screen_size: [1400, 1400], using: :headless_chrome) do |option|66 option.add_argument("start-maximized")67 option.add_emulation(device_name: "iphone 6")68 option.add_preference(:detach, true)69 end70 driver.use71 browser_options = driver.__send__(:browser_options)72 expected = {73 "goog:chromeOptions" => {74 "args" => ["--headless", "start-maximized"],75 "mobileEmulation" => { "deviceName" => "iphone 6" },76 "prefs" => { "detach" => true }77 },78 "browserName" => "chrome"79 }80 assert_equal expected, browser_options[:options].as_json81 end82 test "define extra capabilities using firefox" do83 driver = ActionDispatch::SystemTesting::Driver.new(:selenium, screen_size: [1400, 1400], using: :firefox) do |option|84 option.add_preference("browser.startup.homepage", "http://www.seleniumhq.com/")85 option.add_argument("--host=127.0.0.1")86 end87 driver.use88 browser_options = driver.__send__(:browser_options)89 expected = {90 "moz:firefoxOptions" => {91 "args" => ["--host=127.0.0.1"],92 "prefs" => { "browser.startup.homepage" => "http://www.seleniumhq.com/" }93 },94 "browserName" => "firefox"95 }96 assert_equal expected, browser_options[:options].as_json97 end98 test "define extra capabilities using headless_firefox" do99 driver = ActionDispatch::SystemTesting::Driver.new(:selenium, screen_size: [1400, 1400], using: :headless_firefox) do |option|100 option.add_preference("browser.startup.homepage", "http://www.seleniumhq.com/")101 option.add_argument("--host=127.0.0.1")102 end103 driver.use104 browser_options = driver.__send__(:browser_options)105 expected = {106 "moz:firefoxOptions" => {107 "args" => ["-headless", "--host=127.0.0.1"],108 "prefs" => { "browser.startup.homepage" => "http://www.seleniumhq.com/" }109 },110 "browserName" => "firefox"111 }112 assert_equal expected, browser_options[:options].as_json113 end114 test "does not define extra capabilities" do...
driver.rb
Source:driver.rb
...80 options.add_option(:detach, opts.delete(:detach))81 end82 prefs = opts.delete(:prefs)83 if prefs84 WebDriver.logger.deprecate ':prefs', 'Selenium::WebDriver::Chrome::Options#add_preference'85 prefs.each do |key, value|86 options.add_preference(key, value)87 end88 end89 options = options.as_json90 caps.merge!(options) unless options[Options::KEY].empty?91 if opts.key?(:proxy) || opts.key?('proxy')92 WebDriver.logger.deprecate 'Selenium::WebDriver::Chrome::Driver#new with `:proxy` parameter',93 'Selenium::WebDriver::Chrome::Capabilities#proxy='94 caps[:proxy] = opts.delete(:proxy) if opts.key?(:proxy)95 caps[:proxy] ||= opts.delete('proxy') if opts.key?('proxy')96 end97 caps98 end99 end # Driver100 end # Chrome...
webdriver_run.rb
Source:webdriver_run.rb
...42 end43 elsif %i[firefox firefox_headless].include? TestBench.browser44 Webdrivers::Geckodriver.update45 options = Selenium::WebDriver::Firefox::Options.new46 options.add_preference("download.prompt_for_download", false)47 options.add_preference("profile.content_settings.exceptions.automatic_downloads.*.setting", 1)48 options.add_preference("profile.content_settings.popups", 0)49 options.add_preference("browser.download.dir", DownloadHelper.path)50 options.add_preference("browser.download.folderList", 2)51 options.add_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv,text/tsv,application/pdf,application/doc,application/docx,image/jpeg,application/octet-stream doc xls pdf txt")52 # profile.native_events = true53 if TestBench.browser == :firefox54 puts "Using Firefox"55 else56 puts "Using Firefox Headless"57 options.headless!58 end59 Capybara::Selenium::Driver.new(60 app,61 browser: :firefox,62 http_client: client,63 options: options,64 )65 end...
capybara.rb
Source:capybara.rb
...14 Dir.mkdir cache_directory15end16Capybara.register_driver(:parallel_sniffybara) do |app|17 chrome_options = ::Selenium::WebDriver::Chrome::Options.new18 chrome_options.add_preference(:download,19 prompt_for_download: false,20 default_directory: download_directory)21 chrome_options.add_preference(:browser,22 disk_cache_dir: cache_directory)23 options = {24 service: ::Selenium::WebDriver::Service.chrome(args: { port: 51_674 }),25 browser: :chrome,26 options: chrome_options27 }28 Sniffybara::Driver.register_specialization(29 :chrome, Capybara::Selenium::Driver::ChromeDriver30 )31 Sniffybara::Driver.current_driver = Sniffybara::Driver.new(app, options)32end33Capybara.register_driver(:sniffybara_headless) do |app|34 chrome_options = ::Selenium::WebDriver::Chrome::Options.new35 chrome_options.add_preference(:download,36 prompt_for_download: false,37 default_directory: download_directory)38 chrome_options.add_preference(:browser,39 disk_cache_dir: cache_directory)40 chrome_options.args << "--headless"41 chrome_options.args << "--disable-gpu"42 chrome_options.args << "--window-size=1200,1200"43 options = {44 service: ::Selenium::WebDriver::Service.chrome(args: { port: 51_674 }),45 browser: :chrome,46 options: chrome_options47 }48 Sniffybara::Driver.register_specialization(49 :chrome, Capybara::Selenium::Driver::ChromeDriver50 )51 Sniffybara::Driver.current_driver = Sniffybara::Driver.new(app, options)52end...
portal_env.rb
Source:portal_env.rb
...43 # options = Selenium::WebDriver::Ie::Options.new44 # options = Selenium::WebDriver::Edge::Options.new45 # options = Selenium::WebDriver::Safari::Options.new46 # options.add_argument('--headless') # configure the driver to run in headless mode47 options.add_preference('profile.managed_default_content_settings.notifications', 1)48 options.add_preference('profile.managed_default_content_settings.cookies', 1)49 options.add_preference('profile.managed_default_content_settings.plugins', 1)50 options.add_preference('profile.managed_default_content_settings.geolocation', 1)51 options.add_preference('profile.managed_default_content_settings.images', 1)52 options.add_preference('profile.managed_default_content_settings.media_stream', 1)53 options.add_preference('profile.managed_default_content_settings.popups', 1)54 $web_driver = Selenium::WebDriver.for :chrome, options: options55 #$web_driver = Selenium::WebDriver.for :firefox, options: options, prefs: preferences56 # $web_driver = Selenium::WebDriver.for :ie, options: options, prefs: preferences57 # $web_driver = Selenium::WebDriver.for :edge, options: options, prefs: preferences58 # $web_driver = Selenium::WebDriver.for :safari, options: options, prefs: preferences59 $web_driver.manage.timeouts.page_load = 36060 $web_driver.manage.window.maximize61 Selenium::WebDriver::Wait.new(timeout: 10)62 $web_driver.manage.timeouts.implicit_wait = 1063 $web_driver.navigate.to "https://#{ENV['HOST']}.hi.guru/"64 $web_driver.navigate.to "https://google.com/"65end...
capybara_selenium.rb
Source:capybara_selenium.rb
...9 chromeOptions: { args: %w[disable-gpu disable-setuid-sandbox window-size=7680,4320] }10 )11 browser_options = ::Selenium::WebDriver::Chrome::Options.new12 browser_options.args << "--disable-gpu"13 browser_options.add_preference(:download,14 prompt_for_download: false,15 default_directory: default_directory)16 browser_options.add_preference(:browser, set_download_behavior: { behavior: 'allow' })17 http_client = Selenium::WebDriver::Remote::Http::Default.new18 http_client.read_timeout = 12019 http_client.open_timeout = 12020 Capybara::Selenium::Driver.new(app,21 browser: :chrome,22 desired_capabilities: capabilities,23 http_client: http_client,24 options: browser_options)25end26Capybara.javascript_driver = :selenium_chrome27Capybara.default_max_wait_time = 1528Capybara.register_driver(:selenium_chrome_headless) do |app|29 capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(30 chromeOptions: { args: %w[headless disable-gpu disable-setuid-sandbox window-size=7680,4320] }31 )32 browser_options = ::Selenium::WebDriver::Chrome::Options.new33 browser_options.args << "--headless"34 browser_options.args << "--disable-gpu"35 browser_options.add_preference(:download,36 prompt_for_download: false,37 default_directory: default_directory)38 browser_options.add_preference(:browser, set_download_behavior: { behavior: 'allow' })39 http_client = Selenium::WebDriver::Remote::Http::Default.new40 http_client.read_timeout = 12041 http_client.open_timeout = 12042 Capybara::Selenium::Driver.new(app,43 browser: :chrome,44 desired_capabilities: capabilities,45 http_client: http_client,46 options: browser_options)47end48Capybara.javascript_driver = :selenium_chrome_headless49Capybara.default_max_wait_time = 15...
env.rb
Source:env.rb
...26puts "Browser : #{browser}"27Capybara.register_driver :firefox do |app|28 profile = Selenium::WebDriver::Firefox::Profile.new29 options = Selenium::WebDriver::Firefox::Options.new30 options.add_preference 'dom.webdriver.enabled', false31 options.add_preference 'dom.webnotifications.enabled', false32 options.add_preference 'dom.push.enabled', false33 options.profile = profile34 client = Selenium::WebDriver::Remote::Http::Default.new35 client.open_timeout = wait_time36 client.read_timeout = wait_time37 Capybara::Selenium::Driver.new(38 app,39 browser: :firefox,40 options: options,41 http_client: client42 )43end44Capybara.register_driver :chrome do |app|45 options = Selenium::WebDriver::Chrome::Options.new46 options.add_argument('--no-sandbox')...
add_preference
Using AI Code Generation
1driver.add_preference(:download, prompt_for_download: false, directory_upgrade: true, default_directory: 'C:\Users\Downloads')2driver.find_element(:link, '5.141.59').click3preferences = { download: { prompt_for_download: false, directory_upgrade: true, default_directory: 'C:\Users\Downloads' } }4capabilities['chromeOptions'] = { prefs: preferences }5driver.find_element(:link, '3.141.59').click6driver.add_preference(:download, prompt_for_download: false, directory_upgrade: true, default_directory: Dir.pwd)7preferences = { download: { prompt_for_download: false, directory_upgrade: true, default_directory: Dir.pwd } }8capabilities['chromeOptions'] = { prefs: preferences }9driver.find_element(:link, '3.141.59').click10driver.add_preference(:download, prompt_for_download: false, directory_upgrade: true, default_directory: File.expand_path('..', Dir.pwd))11preferences = { download: { prompt_for_download: false, directory_upgrade: true, default_directory: File.expand_path('..', Dir.pwd) } }12capabilities['chromeOptions'] = { prefs: preferences }
add_preference
Using AI Code Generation
1sviceriver::Chrome::Service.new(2driver.manage.add_cookie(name: 'foo', value: 'bar')3options.add_preference(:download, default_directory: 'downloads')4caps = Selenium::WebDriver::Remote::Capabilities.chrome(5 chromeOptions: { prefs: { download: { default_directory: 'downloads' } } }6caps = Selenium::WebDriver::Remote::Capabilities.chrome(7 chromeOptions: { prefs: { download: { default_directory: 'downloads' } } }8caps = Selenium:WebDriver::Remote::Capabilities.(9 chromeOptionse { prefs: { download: { default_directory: 'downloads' }
add_preference
Using AI Code Generation
1driver.add_preference(:rence method of Selenium.WebDriver.Chrome class2driver.add_preference(:download, :prompt_for_download => false, :default_directory => 'C:\Users\Downloads')3driver.find_element(:link_text, "3.0.1").click
add_preference
Using AI Code Generation
1driver.add_preference("browser.download.folderList", 2)2driver.add_preference("browser.download.manager.showWhenStarting", false)3driver.add_preference("browser.download.dir", "C:\\Downloads")4driver.add_preference("browser.helperApps.neverAsk.saveToDisk", "text/csv")5driver.get("http://www.google.com")6options.add_preference(:download, :prompt_for_download => false, :default_directory => 'C:\Users\Downloads')7driver.find_element(:link_text, "3.0.1").click
add_preference
Using AI Code Generation
1service = Selenium::WebDriver::Chrome::Service.new(2driver.manage.add_cookie(name: 'foo', value: 'bar')3options.add_preference(:download, default_directory: 'downloads')4caps = Selenium::WebDriver::Remote::Capabilities.chrome(5 chromeOptions: { prefs: { download: { default_directory: 'downloads' } } }6caps = Selenium::WebDriver::Remote::Capabilities.chrome(7 chromeOptions: { prefs: { download: { default_directory: 'downloads' } } }8caps = Selenium::WebDriver::Remote::Capabilities.chrome(9 chromeOptions: { prefs: { download: { default_directory: 'downloads' } } }10caps = Selenium::WebDriver::Remote::Capabilities.chrome(11 chromeOptions: { prefs: { download: { default_directory: 'downloads' }
add_preference
Using AI Code Generation
1driver.add_preference(:'download.default_directory', '/home/username/Downloads')2driver.find_element(:link_text, 'Download').click3options.add_option(:prefs, :download => {:default_directory => '/home/username/Downloads'})4driver.find_element(:link_text, 'Download').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!!