Best Selenium code snippet using Selenium.WebDriver.Chrome.directory
hooks.rb
Source:hooks.rb
...24require 'watir-scroll'25require 'fileutils'26require File.join(File.absolute_path('../', File.dirname(__FILE__)),"Base","browser_container.rb")27require File.join(File.absolute_path('../', File.dirname(__FILE__)),"support","db_queries.rb")28driver_directory = File.join(File.absolute_path('../', File.dirname(__FILE__)),"Drivers")29ENV['PATH'] = "#{ENV['PATH']}#{File::PATH_SEPARATOR}#{driver_directory}"30# ENV['BROWSER']31# Before ('@firefox') do |scenario|32# print "Scenario starting - " + scenario.name.to_s + ".\n"33# print "************************************************************************************************************************************************************************************\n"34# WatirNg.patch!35# @driver_path = File.join(File.absolute_path('../', File.dirname(__FILE__)),"drivers","geckodriver.exe")36# Selenium::WebDriver::Firefox.driver_path = @driver_path37# driver = Selenium::WebDriver.for(:firefox, driver_path: @driver_path)#, desired_capabilities: caps)38# @browser = Watir::Browser.new(driver)39# @browser.driver.manage.window.maximize40# #@wait = Watir::Wait41# @bdriver = @browser42# $FeatureName=scenario.feature.name43# # get scenrion name based on run time44# $ScenarioName = scenario.name45# @tTime = Time.now46# @reporter = Watir::HtmlReporter.new(true)47# #@reporter.StartIndentation()48# #@reporter.ReportGeneral("Test Execution Started for #{@FeatureName}/#{@ScenarioName} at #{@tTime.strftime("%Y/%m/%d%H:%M:%S")}")49# #@reporter.EndIndentation()50# require File.join(File.absolute_path('../', File.dirname(__FILE__)),"Base","grs_site_factory.rb")51# @grssitefactoryobj = GRSSiteFactory.new()52# @bcontainer = BrowserContainer.new(@browser,@reporter)53# @loginpg = @grssitefactoryobj.login_webpage(@browser,@reporter)54# @homepg = @grssitefactoryobj.home_webpage(@browser,@reporter)55# @grshomepg = @grssitefactoryobj.grs_home_page(@browser,@reporter)56# #@mouseelement = Watir::Element.new57# $scenario = scenario58#59# end60#Before each scenario this block is run61Before do |scenario|62 print "Scenario starting - " + scenario.name.to_s + ".\n"63 print "************************************************************************************************************************************************************************************\n"64 WatirNg.patch!65 #@driver_path = File.join(File.absolute_path('../', File.dirname(__FILE__)),"drivers","chromedriver.exe")66 #Selenium::WebDriver::Chrome.driver_path = @driver_path67 #caps = Selenium::WebDriver::Remote::Capabilities.new68 #@browser = Watir::Browser.new :chrome69 ##@driver_path = File.join(File.absolute_path('../', File.dirname(__FILE__)),"drivers","geckodriver.exe")70 ##Selenium::WebDriver::Firefox.driver_path = @driver_path71 #profile = Selenium::WebDriver::Firefox::Profile.new72 #profile.assume_untrusted_certificate_issuer = false73 ##@browser = Watir::Browser.new :firefox , driver_path: @driver_path#, profile => :profile74 #@browser = Watir::Browser.start75 #76 #caps = Selenium::WebDriver::Remote::Capabilities.firefox77 #caps['acceptInsecureCerts'] = true78 #*****************************SETTINGS for FireFox***********************************************************************************79 # @driver_path = File.join(File.absolute_path('../', File.dirname(__FILE__)),"drivers","geckodriver.exe")80 # Selenium::WebDriver::Firefox.driver_path = @driver_path81 # driver = Selenium::WebDriver.for(:firefox, driver_path: @driver_path)#, desired_capabilities: caps)82 # @browser = Watir::Browser.new(driver)83 # @browser.driver.execute_script("document.body.style.zoom='100%'")84 #*************************************************************************************************************************************85 #*****************************SETTINGS for IE*****************************************************************************************86 if scenario.name.to_s.include? "Verify that if I apply Teams filter and export the data in Excel, the data is exported based on the filter"87 @download_directory = File.join(File.absolute_path('../', File.dirname(__FILE__)),"GRSExcelExportFiles")88 # @download_directory.tr!('/', '\\') if Selenium::WebDriver::Platform.windows?89 # @profile = Selenium::WebDriver::Firefox::Profile.new90 # @profile['browser.download.folderList'] = 2 # custom location91 # @profile['browser.download.dir'] = @download_directory92 # @profile['browser.helperApps.neverAsk.saveToDisk'] = 'xls'93 #b = Watir::Browser.new :firefox, profile: profile94 # options = Selenium::WebDriver::Firefox::Options.new95 # options.add_argument("browser.download.folderList = 2")96 # options.add_argument("browser.download.dir = #{@download_directory}")97 # options.add_argument("browser.download.useDownloadDir = true")98 # options.add_argument("browser.helperApps.neverAsk.saveToDisk = 'xls'")99 #options.setPreference("pdfjs.disabled", true); // disable the built-in PDF viewer100 #@options = Selenium::WebDriver::Firefox::Options.new101 # @options = Selenium::WebDriver::Firefox::Options.new102 # @options.add_preference("browser.download.folderList", 2)103 # @options.add_preference("browser.download.dir", "#{download_directory}")104 # @options.add_preference("browser.helperApps.neverAsk.saveToDisk","application/excel") #This has been commented for so and so reason - Ajay105 #@options.add_preference("browser.helperApps.alwaysAsk.forceâ, false)106 #@profile = Watir::B107 @profile = Selenium::WebDriver::Firefox::Profile.new108 @profile['browser.download.folderList'] = 2 # custom location109 @profile['browser.download.dir'] = @download_directory.to_s110 @profile['browser.helperApps.neverAsk.saveToDisk'] = "application/excel"111 @driver_path = File.join(File.absolute_path('../', File.dirname(__FILE__)),"drivers","geckodriver.exe")112 Selenium::WebDriver::Firefox.driver_path = @driver_path113 driver = Selenium::WebDriver.for(:firefox, driver_path: @driver_path)#, :profile => default)#options: @options)#,profile: @profile)#, desired_capabilities: caps)114 @browser = Watir::Browser.new(driver)115 #@browser.driver.execute_script("document.body.style.zoom='100%'")116 else117 @configInfo = Config.new()118 @browserName = @configInfo.getBrowserMode119 # puts ENV['BROWSER']120 case @browserName121 when "ie"122 caps = Selenium::WebDriver::Remote::Capabilities.ie123 #caps['IGNORE_ZOOM_SETTING'] = true...
options.rb
Source:options.rb
...179 #180 # @param [String] package The package name of the Chrome or WebView app.181 # @param [String] serial_number The device serial number on which to launch the Chrome or WebView app.182 # @param [String] use_running_app When true uses an already-running Chrome or WebView app,183 # instead of launching the app with a clear data directory.184 # @param [String] activity Name of the Activity hosting the WebView (Not available on Chrome Apps).185 #186 def enable_android(package: 'com.android.chrome', serial_number: nil, use_running_app: nil, activity: nil)187 @options[:android_package] = package188 @options[:android_activity] = activity unless activity.nil?189 @options[:android_device_serial] = serial_number unless serial_number.nil?190 @options[:android_use_running_app] = use_running_app unless use_running_app.nil?191 end192 private193 def enable_logging(browser_options)194 browser_options['goog:loggingPrefs'] = @logging_prefs195 end196 def process_browser_options(browser_options)197 enable_logging(browser_options) unless @logging_prefs.empty?198 options = browser_options[self.class::KEY]199 options['binary'] ||= binary_path if binary_path200 if @profile201 options['args'] ||= []202 options['args'] << "--user-data-dir=#{@profile.directory}"203 end204 return if (@encoded_extensions + @extensions).empty?205 options['extensions'] = @encoded_extensions + @extensions.map(&method(:encode_extension))206 end207 def binary_path208 Chrome.path209 end210 def encode_extension(path)211 File.open(path, 'rb') { |crx_file| Base64.strict_encode64 crx_file.read }212 end213 def validate_extension(path)214 raise Error::WebDriverError, "could not find extension at #{path.inspect}" unless File.file?(path)215 raise Error::WebDriverError, "file was not an extension #{path.inspect}" unless File.extname(path) == '.crx'216 @extensions << path...
capybara_specific_setups.rb
Source:capybara_specific_setups.rb
...26firefox_profile['browser.download.folderList'] = 227firefox_profile['browser.helperApps.neverAsk.saveToDisk'] = 'text/csv' # Suppress "open with" dialog28firefox_options.profile = firefox_profile29chrome_prefs = {30 'download.default_directory' => DownloadHelpers::PATH,31 'download.directory_upgrade' => true,32 'download.prompt_for_download' => false,33 'plugins.plugins_disabled' => ['Chrome PDF Viewer']34}35#-- ---------------------------------------------------------------------------36#++37#38# == Driver configurations ==39#40# For each one of the following setups, just use the corresponding tag on the feature you'd like41# to test.42#43# === Local debug setups ===44#45# Use a physical browser instance (needs access to an xdisplay port). Very slow but useful for46# local debugging.47# ** Firefox **48Capybara.register_driver :physical_firefox do |app|49 Capybara::Selenium::Driver.new(app, browser: :firefox)50end51# ** Chrome **52Capybara.register_driver :physical_chrome do |app|53 capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(54 'goog:chromeOptions': {55 'args': [56 'disable-gpu', 'disable-popup-blocking',57 'window-size=1280,1024', '--enable-features=NetworkService,NetworkServiceInProcess'58 ],59 'prefs': chrome_prefs60 }61 )62 Capybara::Selenium::Driver.new(app, browser: :chrome, desired_capabilities: capabilities)63end64#-- ---------------------------------------------------------------------------65#++66# === Headless setups ===67#68# No display port required; faster: the way to go for CI builds.69# ** Firefox **70# [202112] Legacy setup:71# Capybara.register_driver(:headless_firefox) do |app|72# Capybara::Selenium::Driver.new(73# app,74# browser: :firefox,75# options: firefox_options76# )77# end78# NOTE: Selenium webdriver using Firefox/headless currently has no real support for mobileEmulation.79# ** Chrome **80Capybara.register_driver(:headless_chrome) do |app|81 chrome_options = Selenium::WebDriver::Chrome::Options.new82 %w[headless disable-gpu window-size=1280,1024 no-sandbox].each { |arg| chrome_options.add_argument(arg) }83 chrome_options.add_preference(:download,84 directory_upgrade: true,85 prompt_for_download: false,86 default_directory: ENV['downloads_folder'] || DownloadHelpers::PATH)87 chrome_options.add_preference(:browser, set_download_behavior: { behavior: 'allow' })88 capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(89 'goog:chromeOptions': {90 'args': %w[91 headless disable-gpu disable-popup-blocking window-size=1280,1024 no-sandbox92 enable-features=NetworkService,NetworkServiceInProcess93 ],94 'prefs': chrome_options95 }96 )97 driver = Capybara::Selenium::Driver.new(app, browser: :chrome, capabilities: capabilities)98 # [202112] In case the dowwnload behaviour is not set properly we can force it using a bridge for the driver:99 # bridge = driver.browser.send(:bridge)100 # path = "/session/#{bridge.session_id}/chromium/send_command"...
driver.rb
Source:driver.rb
...63 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...
capybara.rb
Source:capybara.rb
...4require "selenium-webdriver"5require "webdrivers"6Webdrivers.logger.level = :DEBUG if ENV["DEBUG"]7Sniffybara::Driver.run_configuration_file = File.expand_path("VA-axe-run-configuration.json", __dir__)8download_directory = Rails.root.join("tmp/downloads_#{ENV['TEST_SUBCATEGORY'] || 'all'}")9cache_directory = Rails.root.join("tmp/browser_cache_#{ENV['TEST_SUBCATEGORY'] || 'all'}")10Dir.mkdir download_directory unless File.directory?(download_directory)11if File.directory?(cache_directory)12 FileUtils.rm_r cache_directory13else14 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)52end53Capybara::Screenshot.register_driver(:parallel_sniffybara) do |driver, path|...
capybara_selenium.rb
Source:capybara_selenium.rb
1# frozen_string_literal: true2require "capybara/rspec"3require "selenium-webdriver"4default_directory = Rails.root.join('tmp', 'downloads')5# there's a bug in capybara-screenshot that requires us to name6# the driver ":selenium" so we changed it from :headless_chrome"7Capybara.register_driver(:selenium_chrome) do |app|8 capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(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...
directory
Using AI Code Generation
1driver.manage.add_cookie(name: 'name', value: 'value')2driver.manage.delete_cookie('name')3driver.switch_to.frame(0)4driver.switch_to.frame('name')5driver.switch_to.frame(driver.find_element(:xpath, '//*[@id="iframe"]'))6driver.switch_to.window(driver.window_handles.last)7driver.switch_to.window(driver.window_handles.first)8driver.switch_to.window(driver.window_handles[1])9driver.switch_to.window(driver.window_handles[0])10driver.manage.add_cookie(name: 'name', value: 'value')11driver.manage.delete_cookie('name')
directory
Using AI Code Generation
1driver.save_screenshot("google.png")2driver.save_screenshot("google.png")3driver.save_screenshot("google.png")4driver.save_screenshot("google.png")5driver.save_screenshot("google.png")6driver.save_screenshot("google.png")7driver.save_screenshot("google.png")
directory
Using AI Code Generation
1options.add_argument('--headless')2options.add_argument('--disable-gpu')3options.add_argument('--window-size=1280,800')4options.add_argument('--headless')
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!!