Best Selenium code snippet using Selenium.WebDriver.ssl
example-rspec-capybara.rb
Source:example-rspec-capybara.rb
...47 profile['browser.download.dir'] = File.expand_path("../../../", __FILE__)+"/downloads"48 profile['browser.helperApps.neverAsk.saveToDisk'] = "application/pdf"49 profile['plugin.disable_full_page_plugin_for_types'] = "application/pdf"50 options = Selenium::WebDriver::Firefox::Options.new(profile: profile)51 #https://github.com/SeleniumHQ/selenium/wiki/Ruby-Bindings#ssl-certificates52 desired_caps = Selenium::WebDriver::Remote::Capabilities.firefox(53 {54 marionette: true,55 accept_insecure_certs: (ENV['ACCEPTALLCERTS'] == "true")56 }57 )58 if (ENV['FIREFOXPATH'] != 'default')59 Selenium::WebDriver::Firefox.path = ENV['FIREFOXPATH']60 end61 #this was used with capybara (2.14.2) and selenium-webdriver (3.4.1)62 #Capybara::Selenium::Driver.new(app, :browser => :firefox, :profile => profile, desired_capabilities: desired_caps)63 Capybara::Selenium::Driver.new(app, :browser => :firefox, options: options, desired_capabilities: desired_caps)64 end65 Capybara.default_driver = :selenium66## legacy capybara (2.14.2)67# when "FIREFOX-SAVED-PROFILE"68 ## this was used with capybara (2.14.2) and selenium-webdriver (3.4.1) and has been left 69 ## so that if you are using older browsers and need to set this up you still can 70 ## this uses a previously created profile in FF. You can look at the paths for each profile by typing71 ## in the URL bar about:profiles and then copy the path of the profile into your .env or .env.dev files72 ## this allows you to set certain things in the browser like SSL exceptions that you want to be applied 73 ## durring your tests. 74 ## Does not work before FF47 75 # puts "FireFox Profile: "+ENV['FFPROFILEPATH']76 # Capybara.register_driver :selenium do |app|77 # profile = Selenium::WebDriver::Zipper.zip(ENV['FFPROFILEPATH'])78 # desired_caps = Selenium::WebDriver::Remote::Capabilities.firefox(79 # {80 # marionette: true,81 # accept_insecure_certs: (ENV['ACCEPTALLCERTS'] == "true"),82 # firefox_options: {profile: profile},83 # }84 # )85 # 86 # Capybara::Selenium::Driver.new(87 # app,88 # browser: :firefox,89 # desired_capabilities: desired_caps90 # )91 # end92 # Capybara.current_driver = :selenium 93when "FIREFOX-SAVED-PROFILE"94 #capybara (3.0.2) selenium-webdriver (3.11.0)95 #this uses a previously created profile in FF. You can look at the profile name by typing96 #in the URL bar about:profiles and then copy the name of the profile into your .env or .env.dev files97 #this allows you to set certain things in the browser like SSL exceptions that you want to be applied 98 #durring your tests. It does take longer to load 99 #Does not work before FF47 100 101 Capybara.register_driver :selenium do |app|102 options = Selenium::WebDriver::Firefox::Options.new103 options.profile = ENV['FFPROFILEPATH']104 #https://github.com/SeleniumHQ/selenium/wiki/Ruby-Bindings#ssl-certificates105 desired_caps = Selenium::WebDriver::Remote::Capabilities.firefox(106 {107 marionette: true,108 accept_insecure_certs: (ENV['ACCEPTALLCERTS'] == "true"),109 }110 )111 112 Capybara::Selenium::Driver.new(113 app,114 browser: :firefox,115 options: options,116 desired_capabilities: desired_caps117 )118 end119 120 if (ENV['FIREFOXPATH'] != 'default')121 Selenium::WebDriver::Firefox.path = ENV['FIREFOXPATH']122 end 123 Capybara.current_driver = :selenium124when "FIREFOX"125 #works >=FF48126 #https://github.com/SeleniumHQ/selenium/wiki/Ruby-Bindings#ssl-certificates127 desired_caps = Selenium::WebDriver::Remote::Capabilities.firefox(128 {129 marionette: true,130 accept_insecure_certs: (ENV['ACCEPTALLCERTS'] == "true")131 }132 )133 Capybara.register_driver :selenium do |app|134 Capybara::Selenium::Driver.new(135 app, 136 browser: :firefox,137 desired_capabilities: desired_caps138 )139 end140 if (ENV['FIREFOXPATH'] != 'default')141 Selenium::WebDriver::Firefox.path = ENV['FIREFOXPATH']142 end143 Capybara.default_driver = :selenium144when "SAFARI"145 #tested: Safari 10.1.1146 #result: as of Safari 10 there are many problems and not all tests will work147 #This is a standard property but doesn't seem to be working in Safari yet: 148 # desired_caps = Selenium::WebDriver::Remote::Capabilities.safari(149 # {150 # accept_insecure_certs: (ENV['ACCEPTALLCERTS'] == "true")151 # }152 # )153 Capybara.register_driver :selenium do |app|154 Capybara::Selenium::Driver.new(155 app,156 browser: :safari #,157 #desired_capabilities: desired_caps158 )159 end160 Capybara.default_driver = :selenium161when "SAFARI-TECHNOLOGY-PREVIEW"162 #This is what we use to test the Safari release channel. 163 #You will have to install Safari Technology Preview from Apple.164 #This is a standard property but doesn't seem to be working in Safari yet:165 # desired_caps = Selenium::WebDriver::Remote::Capabilities.safari(166 # {167 # accept_insecure_certs: (ENV['ACCEPTALLCERTS'] == "true")168 # }169 # )170 171 Capybara.register_driver :selenium do |app|172 Capybara::Selenium::Driver.new(173 app,174 browser: :safari,175 driver_path: '/Applications/Safari Technology Preview.app/Contents/MacOS/safaridriver' #,176 #desired_capabilities: desired_caps177 )178 end179 Capybara.default_driver = :selenium180else181 #default to Firefox182 #works <FF48 && >=FF48183 Capybara.register_driver :selenium do |app|184 Capybara::Selenium::Driver.new(app, browser: :firefox)185 end186 Capybara.default_driver = :selenium187end188Capybara.default_max_wait_time = (ENV['TIMEOUT'] || 20).to_i189##############################TESTS##############################190#go to https://self-signed.badssl.com/ manualy and add an exception191#then if you are using FireFox go to about:profiles in address bar and get the 192#path to the FireFox Profile and set it in /features/step_definitions/.env.dev 193#run test to see if the exception you194describe 'EXAMPLE TESTS', :js => true, :type => :feature do195 it "ssl cert" do196 #test if ssl cert exception is working 197 visit 'https://self-signed.badssl.com/'198 #puts page.driver.browser.manage.window.inspect199 #worked < chromedriver 2.33 with Chrome 62.200 #page.driver.browser.manage.window.resize_to 1024, 768201 # > chromedriver 2.33 with Chrome 62.202 page.current_window.resize_to 1024, 768203 page.save_screenshot('screenshots/rspec+capybara+selenium.png')204 sleep 1205 end206 it "close page with alert open" do207 visit 'http://www.javascripter.net/faq/confirm.htm'208 page.current_window.resize_to 1024, 768209 page.accept_alert do210 click_button "Try it now"211 end...
profile.rb
Source:profile.rb
...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?184 prefs[WEBDRIVER_PREFS[:untrusted_issuer]] = assume_untrusted_certificate_issuer?185 # If the user sets the home page, we should also start up there186 prefs["startup.homepage_welcome_url"] = prefs["browser.startup.homepage"]187 write_prefs prefs, path188 end189 def read_user_prefs(path)190 prefs = {}191 return prefs unless File.exist?(path)192 File.read(path).split("\n").each do |line|193 if line =~ /user_pref\("([^"]+)"\s*,\s*(.+?)\);/194 key, value = $1.strip, $2.strip195 # wrap the value in an array to make it a valid JSON string.196 prefs[key] = WebDriver.json_load("[#{value}]").first...
example-rspec.rb
Source:example-rspec.rb
...154 end155 156 it "check_SSL_certs" do157 #test that self-signed exception is working158 @base_url = "https://self-signed.badssl.com/"159 @driver.get(@base_url)160 @driver.save_screenshot('screenshots/rsec+selenium+baddssl.png')161 sleep 5162 end163end...
ssl
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').click
ssl
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').click7driver.find_element(:name, 'q').send_keys "Selenium WebDriver"8driver.find_element(:name, 'btnG').click9driver.find_element(:name, '
ssl
Using AI Code Generation
1service = Selenium::WebDriver::Chrome::Service.new(:ssl => true)2service = Selenium::WebDriver::Firefox::Service.new(:ssl => true)3service = Selenium::WebDriver::IE::Service.new(:ssl => true)4service = Selenium::WebDriver::Safari::Service.new(:ssl => true)
ssl
Using AI Code Generation
1service = Selenium::WebDriver::Chrome::Service.new(2service = Selenium::WebDriver::Firefox::Service.new(3service = Selenium::WebDriver::Edge::Service.new(4service = Selenium::WebDriver::Safari::Service.new(5service = Selenium::WebDriver::InternetExplorer::Service.new(
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!!