Best Selenium code snippet using Selenium.WebDriver.lock
profile.rb
Source:profile.rb
...11 :untrusted_issuer => 'webdriver_assume_untrusted_issuer',12 :port => 'webdriver_firefox_port',13 :log_file => 'webdriver.log.file'14 }15 attr_reader :name, :log_file, :lock_port16 attr_writer :secure_ssl, :native_events, :load_no_focus_lib, :lock_port17 class << self18 def ini19 @ini ||= ProfilesIni.new20 end21 def from_name(name)22 ini[name]23 end24 def default_preferences25 @default_preferences ||= WebDriver.json_load(26 File.read(File.expand_path("#{WebDriver.root}/selenium/webdriver/firefox/extension/prefs.json"))27 ).freeze28 end29 end30 #31 # Create a new Profile instance32 #33 # @example User configured profile34 #35 # profile = Selenium::WebDriver::Firefox::Profile.new36 # profile['network.proxy.http'] = 'localhost'37 # profile['network.proxy.http_port'] = 909038 #39 # driver = Selenium::WebDriver.for :firefox, :profile => profile40 #41 def initialize(model = nil)42 @model = verify_model(model)43 model_prefs = read_model_prefs44 @lock_port = 145 if model_prefs.empty?46 @native_events = DEFAULT_ENABLE_NATIVE_EVENTS47 @secure_ssl = DEFAULT_SECURE_SSL48 @untrusted_issuer = DEFAULT_ASSUME_UNTRUSTED_ISSUER49 @load_no_focus_lib = DEFAULT_LOAD_NO_FOCUS_LIB50 @additional_prefs = {}51 else52 # TODO: clean this up53 @native_events = model_prefs.delete(WEBDRIVER_PREFS[:native_events]) == "true"54 @secure_ssl = model_prefs.delete(WEBDRIVER_PREFS[:untrusted_certs]) != "true"55 @untrusted_issuer = model_prefs.delete(WEBDRIVER_PREFS[:untrusted_issuer]) == "true"56 @load_no_focus_lib = model_prefs.delete(WEBDRIVER_PREFS[:load_no_focus_lib]) == "true" # not stored in profile atm, so will always be false.57 @additional_prefs = model_prefs58 end59 @extensions = {}60 end61 def layout_on_disk62 profile_dir = @model ? create_tmp_copy(@model) : Dir.mktmpdir("webdriver-profile")63 FileReaper << profile_dir64 install_extensions(profile_dir)65 delete_lock_files(profile_dir)66 delete_extensions_cache(profile_dir)67 update_user_prefs_in(profile_dir)68 profile_dir69 end70 #71 # Set a preference for this particular profile.72 #73 # @see http://kb.mozillazine.org/About:config_entries74 # @see http://preferential.mozdev.org/preferences.html75 #76 def []=(key, value)77 unless VALID_PREFERENCE_TYPES.any? { |e| value.kind_of? e }78 raise TypeError, "expected one of #{VALID_PREFERENCE_TYPES.inspect}, got #{value.inspect}:#{value.class}"79 end80 if value.kind_of?(String) && Util.stringified?(value)81 raise ArgumentError, "preference values must be plain strings: #{key.inspect} => #{value.inspect}"82 end83 @additional_prefs[key.to_s] = value84 end85 def port=(port)86 self[WEBDRIVER_PREFS[:port]] = port87 end88 def lock_port=(lock_port)89 @lock_port = lock_port90 end91 def log_file=(file)92 @log_file = file93 self[WEBDRIVER_PREFS[:log_file]] = file94 end95 def add_webdriver_extension96 unless @extensions.has_key?(:webdriver)97 add_extension(WEBDRIVER_EXTENSION_PATH, :webdriver)98 end99 end100 #101 # Add the extension (directory, .zip or .xpi) at the given path to the profile.102 #103 def add_extension(path, name = extension_name_for(path))104 @extensions[name] = Extension.new(path)105 end106 def native_events?107 @native_events == true108 end109 def load_no_focus_lib?110 @load_no_focus_lib == true111 end112 def secure_ssl?113 @secure_ssl == true114 end115 def assume_untrusted_certificate_issuer?116 @untrusted_issuer == true117 end118 def assume_untrusted_certificate_issuer=(bool)119 @untrusted_issuer = bool120 end121 def proxy=(proxy)122 unless proxy.kind_of? Proxy123 raise TypeError, "expected #{Proxy.name}, got #{proxy.inspect}:#{proxy.class}"124 end125 case proxy.type126 when :manual127 self['network.proxy.type'] = 1128 set_manual_proxy_preference "ftp", proxy.ftp129 set_manual_proxy_preference "http", proxy.http130 set_manual_proxy_preference "ssl", proxy.ssl131 set_manual_proxy_preference "socks", proxy.socks132 if proxy.no_proxy133 self["network.proxy.no_proxies_on"] = proxy.no_proxy134 else135 self["network.proxy.no_proxies_on"] = ""136 end137 when :pac138 self['network.proxy.type'] = 2139 self['network.proxy.autoconfig_url'] = proxy.pac140 when :auto_detect141 self['network.proxy.type'] = 4142 else143 raise ArgumentError, "unsupported proxy type #{proxy.type}"144 end145 proxy146 end147 private148 def set_manual_proxy_preference(key, value)149 return unless value150 host, port = value.to_s.split(":", 2)151 self["network.proxy.#{key}"] = host152 self["network.proxy.#{key}_port"] = Integer(port) if port153 end154 def install_extensions(directory)155 destination = File.join(directory, "extensions")156 @extensions.each do |name, extension|157 p :extension => name if $DEBUG158 extension.write_to(destination)159 end160 end161 def read_model_prefs162 return {} unless @model163 read_user_prefs(File.join(@model, 'user.js'))164 end165 def delete_extensions_cache(directory)166 FileUtils.rm_f File.join(directory, "extensions.cache")167 end168 def delete_lock_files(directory)169 %w[.parentlock parent.lock].each do |name|170 FileUtils.rm_f File.join(directory, name)171 end172 end173 def extension_name_for(path)174 File.basename(path, File.extname(path))175 end176 def update_user_prefs_in(directory)177 path = File.join(directory, 'user.js')178 prefs = read_user_prefs(path)179 prefs.merge! self.class.default_preferences.fetch 'mutable'180 prefs.merge! @additional_prefs181 prefs.merge! self.class.default_preferences.fetch 'frozen'182 prefs[WEBDRIVER_PREFS[:untrusted_certs]] = !secure_ssl?183 prefs[WEBDRIVER_PREFS[:native_events]] = native_events?...
common.rb
Source:common.rb
...22require 'selenium/webdriver/common/proxy'23require 'selenium/webdriver/common/log_entry'24require 'selenium/webdriver/common/file_reaper'25require 'selenium/webdriver/common/service'26require 'selenium/webdriver/common/socket_lock'27require 'selenium/webdriver/common/socket_poller'28require 'selenium/webdriver/common/port_prober'29require 'selenium/webdriver/common/zipper'30require 'selenium/webdriver/common/wait'31require 'selenium/webdriver/common/alert'32require 'selenium/webdriver/common/mouse'33require 'selenium/webdriver/common/keyboard'34require 'selenium/webdriver/common/touch_screen'35require 'selenium/webdriver/common/target_locator'36require 'selenium/webdriver/common/navigation'37require 'selenium/webdriver/common/timeouts'38require 'selenium/webdriver/common/window'39require 'selenium/webdriver/common/logger'40require 'selenium/webdriver/common/logs'...
env.rb
Source:env.rb
1require 'capybara/cucumber'2require 'selenium-webdriver'3require 'pry'4require 'rspec'5require 'site_prism'6require 'sentry-ruby'7require 'nokogiri'8require 'open-uri'9require 'simplecov'10require 'rest-client'11require 'rubyXL'12require 'parallel'13require 'capybara-screenshot/cucumber'14Capybara::Screenshot.autosave_on_failure = true15Capybara.save_path = "screenshot"16Capybara::Screenshot.prune_strategy = :keep_last_run17SimpleCov.start18# set driver paths19geckodriver_cmd = "which geckodriver"20chromedriver_cmd = "which chromedriver"21edgedriver_cmd = "which msedgedriver"22browser_width, browser_height = 1920, 108023browser = ENV['CUCUMBER_BROWSER'] || 'headless'24browser_size = ENV['BROWSER_SIZE']25remote_server = ENV['SELENIUM_REMOTE_URL']26# Browser Size Input Example: "1920,1080"27if browser_size28 browser_width, browser_height = browser_size.split(",").map {|s| s.strip.to_i}29end30Selenium::WebDriver::Firefox::Launcher.send(:remove_const, 'SOCKET_LOCK_TIMEOUT')31Selenium::WebDriver::Firefox::Launcher::SOCKET_LOCK_TIMEOUT = 12032Selenium::WebDriver::Service.send(:remove_const, 'SOCKET_LOCK_TIMEOUT')33Selenium::WebDriver::Service::SOCKET_LOCK_TIMEOUT = 12034Capybara.default_selector = :css35Capybara.run_server = false36if ['firefox', 'chrome', 'headless', 'safari'].include?(browser)37 Capybara.register_driver :selenium do |app|38 case browser39 when 'firefox'40 geckodriver_path = `#{geckodriver_cmd}`.chomp41 Selenium::WebDriver::Firefox::Service.driver_path = "#{geckodriver_path}"42 profile = Selenium::WebDriver::Firefox::Profile.new43 client = Selenium::WebDriver::Remote::Http::Default.new44 client.read_timeout = 12045 profile['plugin.state.addlive_v'] = 246 opts = remote_server ? {browser: :remote, desired_capabilities: browser.to_sym, url: remote_server} : {browser: browser.to_sym}47 when 'chrome', 'headless'48 args = [49 "--use-fake-ui-for-media-stream",50 "--disable-infobars",51 "--disable-web-security",52 "--verbose",53 "--no-sandbox",54 "--disable-dev-shm-usage"55 ]56 args.push('--headless') if browser == 'headless'57 caps = Selenium::WebDriver::Remote::Capabilities.chrome(58 loggingPrefs: {browser: 'ALL'},59 "chromeOptions": { args: args },60 "goog:chromeOptions": { args: args }61 )62 opts = { browser: :chrome, desired_capabilities: caps }63 client = Selenium::WebDriver::Remote::Http::Default.new64 client.read_timeout = 12065 chromedriver_path = `#{chromedriver_cmd}`.chomp66 Selenium::WebDriver::Chrome.driver_path = "#{chromedriver_path}"67 when 'safari'68 profile = Selenium::WebDriver.for :safari69 caps = Selenium::WebDriver::Remote::Capabilities.safari(70 loggingPrefs: {browser: 'ALL'},71 )72 opts = { browser: :safari, desired_capabilities: caps }73 client = Selenium::WebDriver::Remote::Http::Default.new74 client.read_timeout = 12075 end76 Capybara::Selenium::Driver.new app, opts.merge(profile: profile, http_client: client)77 end78 Capybara.default_driver = :selenium79 Capybara.javascript_driver = :selenium80else81 raise "Browser #{ browser } is not defined in #{ __FILE__ }"82end83# Resize browser84unless browser == "headless"85 Capybara.current_session.driver.browser.manage.window86 .resize_to(browser_width, browser_height)87end...
firefox.rb
Source:firefox.rb
2require 'socket'3require 'rexml/document'4require 'selenium/webdriver/firefox/util'5require 'selenium/webdriver/firefox/extension'6require 'selenium/webdriver/firefox/socket_lock'7require 'selenium/webdriver/firefox/binary'8require 'selenium/webdriver/firefox/profiles_ini'9require 'selenium/webdriver/firefox/profile'10require 'selenium/webdriver/firefox/launcher'11require 'selenium/webdriver/firefox/bridge'12module Selenium13 module WebDriver14 module Firefox15 DEFAULT_PORT = 705516 DEFAULT_ENABLE_NATIVE_EVENTS = Platform.os == :windows17 DEFAULT_SECURE_SSL = false18 DEFAULT_ASSUME_UNTRUSTED_ISSUER = true19 DEFAULT_LOAD_NO_FOCUS_LIB = false20 def self.path=(path)...
lock
Using AI Code Generation
1driver.find_element(:name, "q").send_keys "Selenium WebDriver"2driver.find_element(:name, "btnG").click3driver.find_element(:name, "q").send_keys "Selenium WebDriver"4driver.find_element(:name, "btnG").click5driver.find_element(:name, "q").send_keys "Selenium WebDriver"6driver.find_element(:name, "btnG").click
lock
Using AI Code Generation
1 @driver.find_element(:name, 'q').send_keys 'Selenium'2 @driver.find_element(:name, 'btnG').click3 @driver.find_element(:name, 'q').send_keys 'Selenium'4 @driver.find_element(:name, 'btnG').click5 @driver.find_element(:name, 'q').send_keys 'Selenium'6 @driver.find_element(:name, 'btnG').click7 @driver.find_element(:name, 'q').send
lock
Using AI Code Generation
1driver.find_element(:name, 'q').send_keys 'selenium webdriver'2driver.find_element(:name, 'btnK').click3driver.find_element(:link_text, 'Selenium - Web Browser Automation').click4wait = Selenium::WebDriver::Wait.new(:timeout => 10)5element = wait.until { driver.find_element(:name, 'q') }6driver.find_element(:name, 'btnK').click7driver.find_element(:link_text, 'Selenium - Web Browser Automation').click8wait = Selenium::WebDriver::Wait.new(:timeout => 10)9element = wait.until { driver.find_element(:name, 'q') }10driver.find_element(:name, 'btnK').click11driver.find_element(:link_text, 'Selenium - Web Browser Automation').click12wait = Selenium::WebDriver::Wait.new(:timeout => 10)13element = wait.until { driver.find_element(:name, 'q') }14driver.find_element(:name, 'btnK').click15driver.find_element(:link_text, 'Selenium - Web Browser Automation').click16wait = Selenium::WebDriver::Wait.new(:timeout => 10)17element = wait.until { driver.find_element(:
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!!