Best Selenium code snippet using Selenium.WebDriver.capabilities
example-rspec-capybara.rb
Source:example-rspec-capybara.rb
...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 exception...
bridge.rb
Source:bridge.rb
...28 #29 # @return [Bridge::MJSONWP, Bridge::W3C]30 #31 def self.handshake(**opts)32 desired_capabilities = opts.delete(:desired_capabilities) { ::Selenium::WebDriver::Remote::Capabilities.new }33 if desired_capabilities.is_a?(Symbol)34 unless ::Selenium::WebDriver::Remote::Capabilities.respond_to?(desired_capabilities)35 raise ::Selenium::WebDriver::Error::WebDriverError, "invalid desired capability: #{desired_capabilities.inspect}"36 end37 desired_capabilities = ::Selenium::WebDriver::Remote::Capabilities.__send__(desired_capabilities)38 end39 bridge = new(opts)40 capabilities = bridge.create_session(desired_capabilities)41 case bridge.dialect42 when :oss # for MJSONWP43 Bridge::MJSONWP.new(capabilities, bridge.session_id, opts)44 when :w3c45 Bridge::W3C.new(capabilities, bridge.session_id, opts)46 else47 raise CoreError, 'cannot understand dialect'48 end49 end50 # Override51 # Creates session handling both OSS and W3C dialects.52 # Copy from Selenium::WebDriver::Remote::Bridge to keep using +merged_capabilities+ for Appium53 #54 # If +desired_capabilities+ has +forceMjsonwp: true+ in the capability, this bridge works with mjsonwp protocol.55 # If +forceMjsonwp: false+ or no the capability, it depends on server side whether this bridge works as w3c or mjsonwp.56 #57 # @param [::Selenium::WebDriver::Remote::W3C::Capabilities, Hash] desired_capabilities A capability58 # @return [::Selenium::WebDriver::Remote::Capabilities, ::Selenium::WebDriver::Remote::W3C::Capabilities]59 #60 # @example61 #62 # opts = {63 # caps: {64 # platformName: :ios,65 # automationName: 'XCUITest',66 # app: 'test/functional/app/UICatalog.app.zip',67 # platformVersion: '11.4',68 # deviceName: 'iPhone Simulator',69 # useNewWDA: true,70 # forceMjsonwp: true71 # },72 # appium_lib: {73 # wait: 3074 # }75 # }76 # core = ::Appium::Core.for(caps)77 # driver = core.start_driver #=> driver.dialect == :oss78 #79 # @example80 #81 # opts = {82 # caps: {83 # platformName: :ios,84 # automationName: 'XCUITest',85 # app: 'test/functional/app/UICatalog.app.zip',86 # platformVersion: '11.4',87 # deviceName: 'iPhone Simulator',88 # useNewWDA: true,89 # },90 # appium_lib: {91 # wait: 3092 # }93 # }94 # core = ::Appium::Core.for(caps)95 # driver = core.start_driver #=> driver.dialect == :w3c if the Appium server support W3C.96 #97 def create_session(desired_capabilities)98 response = execute(:new_session, {}, merged_capabilities(desired_capabilities))99 @session_id = response['sessionId']100 oss_status = response['status'] # for compatibility with Appium 1.7.1-101 value = response['value']102 if value.is_a?(Hash) # include for W3C format103 @session_id = value['sessionId'] if value.key?('sessionId')104 if value.key?('capabilities')105 value = value['capabilities']106 elsif value.key?('value')107 value = value['value']108 end109 end110 raise ::Selenium::WebDriver::Error::WebDriverError, 'no sessionId in returned payload' unless @session_id111 json_create(oss_status, value)112 end113 # Append +appium:+ prefix for Appium following W3C spec114 # https://www.w3.org/TR/webdriver/#dfn-validate-capabilities115 #116 # @param [::Selenium::WebDriver::Remote::W3C::Capabilities, Hash] capabilities A capability117 # @return [::Selenium::WebDriver::Remote::W3C::Capabilities]118 def add_appium_prefix(capabilities)119 w3c_capabilities = ::Selenium::WebDriver::Remote::W3C::Capabilities.new120 capabilities = capabilities.__send__(:capabilities) unless capabilities.is_a?(Hash)121 capabilities.each do |name, value|122 next if value.nil?123 next if value.is_a?(String) && value.empty?124 capability_name = name.to_s125 w3c_name = extension_prefix?(capability_name) ? name : "#{APPIUM_PREFIX}#{capability_name}"126 w3c_capabilities[w3c_name] = value127 end128 w3c_capabilities129 end130 private131 def camel_case(str)132 str.gsub(/_([a-z])/) { Regexp.last_match(1).upcase }133 end134 def extension_prefix?(capability_name)135 snake_cased_capability_names = ::Selenium::WebDriver::Remote::W3C::Capabilities::KNOWN.map(&:to_s)136 camel_cased_capability_names = snake_cased_capability_names.map { |v| camel_case(v) }137 snake_cased_capability_names.include?(capability_name) ||138 camel_cased_capability_names.include?(capability_name) ||139 capability_name.match(::Selenium::WebDriver::Remote::W3C::Capabilities::EXTENSION_CAPABILITY_PATTERN)140 end141 def json_create(oss_status, value)142 if oss_status143 ::Selenium::WebDriver.logger.info 'Detected OSS dialect.'144 @dialect = :oss145 ::Selenium::WebDriver::Remote::Capabilities.json_create(value)146 else147 ::Selenium::WebDriver.logger.info 'Detected W3C dialect.'148 @dialect = :w3c149 ::Selenium::WebDriver::Remote::W3C::Capabilities.json_create(value)150 end151 end152 def delete_force_mjsonwp(capabilities)153 w3c_capabilities = ::Selenium::WebDriver::Remote::W3C::Capabilities.new154 capabilities = capabilities.__send__(:capabilities) unless capabilities.is_a?(Hash)155 capabilities.each do |name, value|156 next if value.nil?157 next if value.is_a?(String) && value.empty?158 next if name == FORCE_MJSONWP159 w3c_capabilities[name] = value160 end161 w3c_capabilities162 end163 def merged_capabilities(desired_capabilities)164 force_mjsonwp = desired_capabilities[FORCE_MJSONWP]165 desired_capabilities = delete_force_mjsonwp(desired_capabilities) unless force_mjsonwp.nil?166 if force_mjsonwp167 {168 desiredCapabilities: desired_capabilities169 }170 else171 new_caps = add_appium_prefix(desired_capabilities)172 w3c_capabilities = ::Selenium::WebDriver::Remote::W3C::Capabilities.from_oss(new_caps)173 {174 desiredCapabilities: desired_capabilities,175 capabilities: {176 firstMatch: [w3c_capabilities]177 }178 }179 end180 end181 end # class Bridge182 end # class Base183 end # module Core184end # module Appium...
sauce_labs.rb
Source:sauce_labs.rb
...8 caps[:name] = "mySkrill Smoke: FIREFOX WIN7 - #{Time.now.strftime '%Y-%m-%d %H:%M'}"9 @local_browser = Selenium::WebDriver.for(10 :remote,11 :url => "http://myaccount_ci:9bc00106-156c-4838-9865-2f92c46762ca@ondemand.saucelabs.com:80/wd/hub",12 :desired_capabilities => caps)13 when 'firefox_winxp'14 caps = Selenium::WebDriver::Remote::Capabilities.firefox15 caps['platform'] = 'Windows 2003'16 caps['version'] = '17'17 caps[:name] = "mySkrill Smoke: FIREFOX FOR WIN XP - #{Time.now.strftime '%Y-%m-%d %H:%M'}"18 @local_browser = Selenium::WebDriver.for(19 :remote,20 :url => "http://myaccount_ci:9bc00106-156c-4838-9865-2f92c46762ca@ondemand.saucelabs.com:80/wd/hub",21 :desired_capabilities => caps)22 when 'firefox_mac'23 caps = Selenium::WebDriver::Remote::Capabilities.firefox24 caps['platform'] = 'Mac 10.6'25 caps['version'] = '17'26 caps[:name] = "mySkrill Smoke: FIREFOX FOR MAC OSX - #{Time.now.strftime '%Y-%m-%d %H:%M'}"27 @local_browser = Selenium::WebDriver.for(28 :remote,29 :url => "http://myaccount_ci:9bc00106-156c-4838-9865-2f92c46762ca@ondemand.saucelabs.com:80/wd/hub",30 :desired_capabilities => caps)31 when 'chrome_winxp'32 caps = Selenium::WebDriver::Remote::Capabilities.chrome33 caps.platform = 'Windows 2003'34 caps[:name] = "mySkrill Smoke: CHROME FOR WIN XP- #{Time.now.strftime '%Y-%m-%d %H:%M'}"35 @local_browser = Selenium::WebDriver.for(36 :remote,37 :url => "http://myaccount_ci:9bc00106-156c-4838-9865-2f92c46762ca@ondemand.saucelabs.com:80/wd/hub",38 :desired_capabilities => caps)39 when 'chrome_win7'40 caps = Selenium::WebDriver::Remote::Capabilities.chrome41 caps.platform = 'Windows 2008'42 caps[:name] = "mySkrill Smoke: CHROME FOR WIN7 - #{Time.now.strftime '%Y-%m-%d %H:%M'}"43 @local_browser = Selenium::WebDriver.for(44 :remote,45 :url => "http://myaccount_ci:9bc00106-156c-4838-9865-2f92c46762ca@ondemand.saucelabs.com:80/wd/hub",46 :desired_capabilities => caps)47 when 'chrome_mac'48 caps = Selenium::WebDriver::Remote::Capabilities.chrome49 caps['platform'] = 'Mac 10.8'50 caps[:name] = "mySkrill Smoke: CHROME FOR MAC - #{Time.now.strftime '%Y-%m-%d %H:%M'}"51 @local_browser = Selenium::WebDriver.for(52 :remote,53 :url => "http://myaccount_ci:9bc00106-156c-4838-9865-2f92c46762ca@ondemand.saucelabs.com:80/wd/hub",54 :desired_capabilities => caps)55 when 'ie9_win7'56 caps = Selenium::WebDriver::Remote::Capabilities.internet_explorer57 caps.platform = 'Windows 2008'58 caps.version = '9'59 caps[:name] = "mySkrill Smoke: IE9 FOR WIN7 - #{Time.now.strftime '%Y-%m-%d %H:%M'}"60 @local_browser = Selenium::WebDriver.for(61 :remote,62 :url => "http://myaccount_ci:9bc00106-156c-4838-9865-2f92c46762ca@ondemand.saucelabs.com:80/wd/hub",63 :desired_capabilities => caps)64 when 'ie8_win7'65 caps = Selenium::WebDriver::Remote::Capabilities.internet_explorer66 caps.platform = 'Windows 2008'67 caps.version = '8'68 caps[:name] = "mySkrill Smoke: IE8 FOR WIN7 - #{Time.now.strftime '%Y-%m-%d %H:%M'}"69 @local_browser = Selenium::WebDriver.for(70 :remote,71 :url => "http://myaccount_ci:9bc00106-156c-4838-9865-2f92c46762ca@ondemand.saucelabs.com:80/wd/hub",72 :desired_capabilities => caps)73 when 'ie9_winxp'74 caps = Selenium::WebDriver::Remote::Capabilities.internet_explorer75 caps.platform = 'Windows 2003'76 caps.version = '9'77 caps[:name] = "mySkrill Smoke: IE9 FOR WIN XP - #{Time.now.strftime '%Y-%m-%d %H:%M'}"78 @local_browser = Selenium::WebDriver.for(79 :remote,80 :url => "http://myaccount_ci:9bc00106-156c-4838-9865-2f92c46762ca@ondemand.saucelabs.com:80/wd/hub",81 :desired_capabilities => caps)82 when 'ie8_winxp'83 caps = Selenium::WebDriver::Remote::Capabilities.internet_explorer84 caps.platform = 'Windows 2003'85 caps.version = '8'86 caps[:name] = "mySkrill Smoke: IE8 FOR WIN XP - #{Time.now.strftime '%Y-%m-%d %H:%M'}"87 @local_browser = Selenium::WebDriver.for(88 :remote,89 :url => "http://myaccount_ci:9bc00106-156c-4838-9865-2f92c46762ca@ondemand.saucelabs.com:80/wd/hub",90 :desired_capabilities => caps)91 when 'ie7_winxp'92 caps = Selenium::WebDriver::Remote::Capabilities.internet_explorer93 caps.platform = 'Windows 2003'94 caps.version = '7'95 caps[:name] = "mySkrill Smoke: IE7 FOR WIN XP - #{Time.now.strftime '%Y-%m-%d %H:%M'}"96 @local_browser = Selenium::WebDriver.for(97 :remote,98 :url => "http://myaccount_ci:9bc00106-156c-4838-9865-2f92c46762ca@ondemand.saucelabs.com:80/wd/hub",99 :desired_capabilities => caps)100 when 'safari'101 # /* Notice: We don't yet support HTTPS for this platform over Sauce Connect */102 caps = Selenium::WebDriver::Remote::Capabilities.safari103 caps.platform = 'Mac 10.8'104 caps.version = '6'105 caps[:name] = "mySkrill Smoke: SAFARI - #{Time.now.strftime '%Y-%m-%d %H:%M'}"106 @local_browser = Selenium::WebDriver.for(107 :remote,108 :url => "http://myaccount_ci:9bc00106-156c-4838-9865-2f92c46762ca@ondemand.saucelabs.com:80/wd/hub",109 :desired_capabilities => caps)110 when 'ipad'111 # /* Notice: We don't yet support HTTPS for this platform over Sauce Connect */112 caps = Selenium::WebDriver::Remote::Capabilities.ipad113 caps.platform = 'Mac 10.8'114 caps.version = '6'115 caps[:name] = "mySkrill Smoke: iOS 6.0 iPAD - #{Time.now.strftime '%Y-%m-%d %H:%M'}"116 @local_browser = Selenium::WebDriver.for(117 :remote,118 :url => "http://myaccount_ci:9bc00106-156c-4838-9865-2f92c46762ca@ondemand.saucelabs.com:80/wd/hub",119 :desired_capabilities => caps)120 when 'iphone'121 # /* Notice: We don't yet support HTTPS for this platform over Sauce Connect */122 caps = Selenium::WebDriver::Remote::Capabilities.iphone123 caps.platform = 'Mac 10.8'124 caps.version = '6'125 caps[:name] = "mySkrill Smoke: iOS 6.0 iPHONE - #{Time.now.strftime '%Y-%m-%d %H:%M'}"126 @local_browser = Selenium::WebDriver.for(127 :remote,128 :url => "http://myaccount_ci:9bc00106-156c-4838-9865-2f92c46762ca@ondemand.saucelabs.com:80/wd/hub",129 :desired_capabilities => caps)130 when 'ie10_win8'131 caps = Selenium::WebDriver::Remote::Capabilities.internet_explorer132 caps['platform'] = 'Windows 2012'133 caps['version'] = '10'134 caps[:name] = "mySkrill Smoke: IE10 FOR WIN8 - #{Time.now.strftime '%Y-%m-%d %H:%M'}"135 @local_browser = Selenium::WebDriver.for(136 :remote,137 :url => "http://myaccount_ci:9bc00106-156c-4838-9865-2f92c46762ca@ondemand.saucelabs.com:80/wd/hub",138 :desired_capabilities => caps)139 when 'opera_win7'140 caps = Selenium::WebDriver::Remote::Capabilities.opera141 caps['platform'] = 'Windows 2008'142 caps['version'] = '12'143 caps[:name] = "mySkrill Smoke: OPERA FOR WIN7 - #{Time.now.strftime '%Y-%m-%d %H:%M'}"144 @local_browser = Selenium::WebDriver.for(145 :remote,146 :url => "http://myaccount_ci:9bc00106-156c-4838-9865-2f92c46762ca@ondemand.saucelabs.com:80/wd/hub",147 :desired_capabilities => caps)148 end149 end150end...
example-rspec.rb
Source:example-rspec.rb
...60 if (ENV['FIREFOXPATH'] != 'default')61 Selenium::WebDriver::Firefox.path = ENV['FIREFOXPATH']62 end63 #this was used with capybara (2.14.2) and selenium-webdriver (3.4.1)64 #@driver = Selenium::WebDriver.for :firefox, :profile => profile, desired_capabilities: desired_caps65 @driver = Selenium::WebDriver.for :firefox, options: options, desired_capabilities: desired_caps66 # when "FIREFOX-SAVED-PROFILE"67 #this was used with capybara (2.14.2) and selenium-webdriver (3.4.1) and has been left 68 #so that if you are using older browsers and need to set this up you still can 69 #this uses a previously created profile in FF. You can look at the paths for each profile by typing70 #in the URL bar about:profiles and then copy the path of the profile into your .env or .env.dev files71 #this allows you to set certain things in the browser like SSL exceptions that you want to be applied 72 #durring your tests. 73 #Does not work before FF47 74 # puts "FireFox Profile: "+ENV['FFPROFILEPATH']75 # profile = Selenium::WebDriver::Zipper.zip(ENV['FFPROFILEPATH'])76 # desired_caps = Selenium::WebDriver::Remote::Capabilities.firefox(77 # {78 # marionette: true,79 # accept_insecure_certs: (ENV['ACCEPTALLCERTS'] == "true"),80 # firefox_options: {profile: profile},81 # }82 # )83 # @driver = Selenium::WebDriver.for :firefox, desired_capabilities: desired_caps84 when "FIREFOX-SAVED-PROFILE"85 #capybara (3.0.2) selenium-webdriver (3.11.0)86 #this uses a previously created profile in FF. You can look at the paths for each profile by typing87 #in the URL bar about:profiles and then copy the path of the profile into your .env or .env.dev files88 #this allows you to set certain things in the browser like SSL exceptions that you want to be applied 89 #durring your tests. 90 #Does not work before FF47 91 options = Selenium::WebDriver::Firefox::Options.new92 options.profile = ENV['FFPROFILEPATH']93 desired_caps = Selenium::WebDriver::Remote::Capabilities.firefox(94 {95 marionette: true,96 accept_insecure_certs: (ENV['ACCEPTALLCERTS'] == "true"),97 }98 )99 if (ENV['FIREFOXPATH'] != 'default')100 Selenium::WebDriver::Firefox.path = ENV['FIREFOXPATH']101 end102 @driver = Selenium::WebDriver.for :firefox, options: options, desired_capabilities: desired_caps103 when "FIREFOX"104 #works >=FF48105 desired_caps = Selenium::WebDriver::Remote::Capabilities.firefox(106 {107 marionette: true,108 accept_insecure_certs: (ENV['ACCEPTALLCERTS'] == "true")109 }110 )111 if (ENV['FIREFOXPATH'] != 'default')112 Selenium::WebDriver::Firefox.path = ENV['FIREFOXPATH']113 end114 @driver = Selenium::WebDriver.for :firefox, desired_capabilities: desired_caps115 when "SAFARI"116 #This is a standard property but doesn't seem to be working in Safari yet:117 # desired_caps = Selenium::WebDriver::Remote::Capabilities.safari(118 # {119 # accept_insecure_certs: (ENV['ACCEPTALLCERTS'] == "true")120 # }121 # )122 @driver = Selenium::WebDriver.for :safari #,desired_capabilities: desired_caps123 when "SAFARI-TECHNOLOGY-PREVIEW"124 #This is a standard property but doesn't seem to be working in Safari yet:125 # desired_caps = Selenium::WebDriver::Remote::Capabilities.safari(126 # {127 # accept_insecure_certs: (ENV['ACCEPTALLCERTS'] == "true")128 # }129 # )130 @driver = Selenium::WebDriver.for :safari, driver_path: '/Applications/Safari Technology Preview.app/Contents/MacOS/safaridriver' #,desired_capabilities: desired_caps131 else132 #default to Firefox133 #works <FF48 && >=FF48134 @driver = Selenium::WebDriver.for :firefox135 end136 @accept_next_alert = true137 @driver.manage.timeouts.implicit_wait = (ENV['TIMEOUT'] || 20).to_i138 @verification_errors = []139 end140 141 after(:each) do142 @driver.quit143 @verification_errors.should == []144 end...
env.rb
Source:env.rb
...27 options = Selenium::WebDriver::Chrome::Options.new(28 args: %w[--remote-debugging-port=9222 --incognito --disable-gpu --no-sandbox --disable-web-security]29 )30 # options.add_emulation(device_name: "#{$DEVICE}")31 capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(32 acceptInsecureCerts: true,33 )34 capabilities["browserName"] = "chrome"35 capabilities["version"] = "87.0"36 capabilities["enableVNC"] = true37 capabilities["enableVideo"] = true38 Capybara::Selenium::Driver.new(39 app,40 browser: :remote,41 url: "http://localhost:4444/wd/hub",42 options: options,43 desired_capabilities: capabilities44)45end46elsif ENV['chrome']47 Capybara.javascript_driver = :selenium48 Capybara.default_driver = :selenium49 Capybara.register_driver(:selenium) do |app|50 options = Selenium::WebDriver::Chrome::Options.new(51 args: %w[--incognito --disable-gpu --no-sandbox --disable-web-security --window-size=1024,768]52 )53 read_timeout = 40054 capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(55 acceptInsecureCerts: true,56 )57 Capybara::Selenium::Driver.new(58 app,59 browser: :chrome,60 options: options,61 desired_capabilities: capabilities62 )63end64# elsif ENV['host']65# Capybara.register_driver(:selenium) do |app|66# options = Selenium::WebDriver::Chrome::Options.new(67# args: %w[--incognito --disable-gpu --no-sandbox --disable-web-security]68# )69# options.add_emulation(device_name: 'Galaxy S5')70# read_timeout = 40071# capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(72# acceptInsecureCerts: true,73# )74# Capybara::Selenium::Driver.new(75# app,76# browser: :chrome,77# options: options,78# desired_capabilities: capabilities79# )80# end81elsif ENV['chrome_headless']82 Capybara.javascript_driver = :selenium_chrome_headless83 Capybara.default_driver = :selenium_chrome_headless84 Capybara.register_driver(:selenium_chrome_headless) do |app|85 options = Selenium::WebDriver::Chrome::Options.new(86 args: %w[87 --headless 88 --incognito 89 --disable-gpu 90 --no-sandbox 91 --disable-setuid-sandbox 92 --disable-web-security 93 --window-size=1366,76894 --start-maximized95 --disable-infobars96 ]97 )98 client = Selenium::WebDriver::Remote::Http::Default.new99 client.read_timeout = 240100 # read_timeout = 800101 capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(102 acceptInsecureCerts: true,103 )104 Capybara::Selenium::Driver.new(105 app,106 browser: :chrome,107 options: options,108 http_client: client,109 desired_capabilities: capabilities110 )111end112elsif ENV['chrome_mobile']113 Capybara.javascript_driver = :selenium114 Capybara.default_driver = :selenium115 Capybara.register_driver(:selenium) do |app|116 options = Selenium::WebDriver::Chrome::Options.new(117 args: %w[--incognito --disable-gpu --no-sandbox --disable-web-security]118 )119 options.add_emulation(device_name: "#{$DEVICE}")120 read_timeout = 400121 capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(122 acceptInsecureCerts: true,123 )124 Capybara::Selenium::Driver.new(125 app,126 browser: :chrome,127 options: options,128 desired_capabilities: capabilities129 )130end131elsif ENV['chrome_mobile_headless']132 Capybara.javascript_driver = :selenium133 Capybara.default_driver = :selenium134 Capybara.register_driver(:selenium) do |app|135 options = Selenium::WebDriver::Chrome::Options.new(136 args: %w[--headless --incognito --disable-gpu --no-sandbox --disable-web-security]137 )138 options.add_emulation(device_name: "#{$DEVICE}")139 read_timeout = 400140 capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(141 acceptInsecureCerts: true,142 )143 Capybara::Selenium::Driver.new(144 app,145 browser: :chrome,146 options: options,147 desired_capabilities: capabilities148 )149end150elsif ENV['chromium']151 Capybara.javascript_driver = :selenium152 Capybara.default_driver = :selenium153 Selenium::WebDriver::Chrome.path = '/usr/bin/chromium-browser'154 Capybara.register_driver(:selenium) do |app|155 options = Selenium::WebDriver::Chrome::Options.new(156 args: %w[--incognito --disable-gpu --no-sandbox --disable-web-security --window-size=1024,768]157 )158 read_timeout = 400159 capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(160 acceptInsecureCerts: true,161 )162 Capybara::Selenium::Driver.new(163 app,164 browser: :chrome,165 options: options,166 desired_capabilities: capabilities167 )168end169elsif ENV['chromium_headless']170 Capybara.javascript_driver = :selenium_chrome_headless171 Capybara.default_driver = :selenium_chrome_headless172 Selenium::WebDriver::Chrome.path = '/usr/bin/chromium-browser'173 Capybara.register_driver(:selenium_chrome_headless) do |app|174 options = Selenium::WebDriver::Chrome::Options.new(175 args: %w[176 --headless 177 --incognito 178 --disable-gpu 179 --no-sandbox 180 --disable-setuid-sandbox 181 --disable-web-security 182 --window-size=1366,768183 --start-maximized184 --disable-infobars185 ]186 )187 client = Selenium::WebDriver::Remote::Http::Default.new188 client.read_timeout = 240189 # read_timeout = 800190 capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(191 acceptInsecureCerts: true,192 )193 Capybara::Selenium::Driver.new(194 app,195 browser: :chrome,196 options: options,197 http_client: client,198 desired_capabilities: capabilities199 )200end201else202 Capybara.default_driver = :selenium203end204# Capybara.default_max_wait_time = 60...
driver_configuration.rb
Source:driver_configuration.rb
1# frozen_string_literal: true2require "quke/configuration"3module Quke #:nodoc:4 # Helper class that manages the options, switches and capabilities for each of5 # the different drivers.6 class DriverConfiguration7 # Access the instance of Quke::Configuration passed to this instance of8 # Quke::DriverOptions when it was initialized.9 attr_reader :config10 # Instantiate an instance of Quke::DriverConfiguration.11 #12 # It expects an instance of Quke::Configuration which will be used13 # internally to determine how to set the options it passes back to each of14 # the drivers.15 # to be used and any related options16 def initialize(config)17 @config = config18 end19 # Returns an instance of Selenium::WebDriver::Chrome::Options to be20 # used when registering an instance of Capybara::Selenium::Driver,21 # configured to run using Chrome.22 #23 # For example when initialising the driver like this24 #25 # args = [26 # "--proxy-server=localhost:8080",27 # "--proxy-bypass-list=127.0.0.1,192.168.0.1",28 # "--user-agent=Mozilla/5.0 (MSIE 10.0; Windows NT 6.1; Trident/5.0)"29 # ]30 #31 # Capybara::Selenium::Driver.new(32 # app,33 # browser: :chrome,34 # options: Selenium::WebDriver::Firefox::Options.new(args: args)35 # )36 #37 # You can instead call Quke::DriverConfiguration.chrome which will38 # manage instantiating and setting up the39 # Selenium::WebDriver::Chrome::Options instance based on the40 # properties of the Quke::Configuration instance its initialised with41 #42 # Capybara::Selenium::Driver.new(43 # app,44 # browser: :chrome,45 # options: my_driver_config.chrome46 # )47 #48 def chrome49 no_proxy = config.proxy.no_proxy.tr(",", ";")50 options = Selenium::WebDriver::Chrome::Options.new51 options.headless! if config.headless52 options.add_argument("--proxy-server=#{config.proxy.host}:#{config.proxy.port}") if config.proxy.use_proxy?53 options.add_argument("--proxy-bypass-list=#{no_proxy}") unless config.proxy.no_proxy.empty?54 options.add_argument("--user-agent=#{config.user_agent}") unless config.user_agent.empty?55 options56 end57 # Returns an instance of Selenium::WebDriver::Firefox::Options to be58 # used when registering an instance of Capybara::Selenium::Driver,59 # configured to run using Firefox (the default).60 #61 # For example when initialising the driver like this62 #63 # my_profile = Selenium::WebDriver::Firefox::Profile.new64 # my_profile.proxy = Selenium::WebDriver::Proxy.new(65 # http: "10.10.2.70:8080",66 # ssl: "10.10.2.70:8080"67 # )68 # my_profile['general.useragent.override'] = "Mozilla/5.0 (MSIE 10.0; Windows NT 6.1; Trident/5.0)"69 # Capybara::Selenium::Driver.new(70 # app,71 # browser: :firefox,72 # options: Selenium::WebDriver::Firefox::Options.new(profile: my_profile)73 # )74 #75 # You can instead call Quke::DriverConfiguration.firefox which will76 # manage instantiating and setting up the77 # Selenium::WebDriver::Firefox::Options instance based on the78 # properties of the Quke::Configuration instance its initialised with79 #80 # Capybara::Selenium::Driver.new(81 # app,82 # browser: :firefox,83 # options: my_driver_config.firefox84 # )85 #86 def firefox87 options = Selenium::WebDriver::Firefox::Options.new(profile: firefox_profile)88 options.headless! if config.headless89 options90 end91 # Returns an instance of Selenium::WebDriver::Remote::Capabilities to be92 # used when registering an instance of Capybara::Selenium::Driver,93 # configured to run using the Browserstack[https://www.browserstack.com/]94 # service.95 #96 # For example when initialising the driver like this97 #98 # my_capabilites = Selenium::WebDriver::Remote::Capabilities.new99 # my_capabilites['build'] = my_config.browserstack['build']100 # # ... set rest of capabilities101 # Capybara::Selenium::Driver.new(102 # app,103 # browser: :remote,104 # url: 'http://jdoe:123456789ABCDE@hub.browserstack.com/wd/hub',105 # desired_capabilities: my_capabilites106 # )107 #108 # You can instead call Quke::DriverConfiguration.browserstack which will109 # manage instantiating and setting up the110 # Selenium::WebDriver::Remote::Capabilities instance based on the111 # properties of the Quke::Configuration instance its initialised with112 #113 # Capybara::Selenium::Driver.new(114 # app,115 # browser: :remote,116 # url: my_driver_config.browserstack_url,117 # desired_capabilities: my_driver_config.browserstack118 # )119 #120 # For further reference on browserstack capabilities121 # https://www.browserstack.com/automate/capabilities122 # https://www.browserstack.com/automate/ruby#configure-capabilities123 def browserstack124 # Documentation and the code for this class can be found here125 # http://www.rubydoc.info/gems/selenium-webdriver/0.0.28/Selenium/WebDriver/Remote/Capabilities126 # https://github.com/SeleniumHQ/selenium/blob/master/rb/lib/selenium/webdriver/remote/capabilities.rb127 capabilities = Selenium::WebDriver::Remote::Capabilities.new128 config.browserstack.capabilities.each do |key, value|129 capabilities[key] = value130 end131 capabilities132 end133 private134 def firefox_profile135 profile = Selenium::WebDriver::Firefox::Profile.new136 profile["general.useragent.override"] = config.user_agent unless config.user_agent.empty?137 profile.proxy = Selenium::WebDriver::Proxy.new(config.proxy.firefox_settings) if config.proxy.use_proxy?138 profile139 end140 end141end...
hooks.rb
Source:hooks.rb
...28#DesiredCapabilities fcapability = DesiredCapabilities.firefox(:firefox_profile => 'fprofile');29#===for remote30#if environment == :int31if browser_name == :firefox32 capabilities1 = Selenium::WebDriver::Remote::Capabilities.new33 capabilities1.browser_name = browser_name34 capabilities1.platform = :WINDOWS35 @browser = Selenium::WebDriver.for( :remote,36 :desired_capabilities => capabilities137 )38elsif browser_name == :chrome39 capabilities2 = Selenium::WebDriver::Remote::Capabilities.new40 capabilities2.browser_name = browser_name41 capabilities2.platform = :WINDOWS42 @browser = Selenium::WebDriver.for(:remote,43 :desired_capabilities => capabilities244 )45end4647=begin48capabilities3 = Selenium::WebDriver::Remote::Capabilities.new49capabilities3.browser_name = :internet_explorer50capabilities3.platform = :WINDOWS5152@browser = Selenium::WebDriver.for(:remote, 53 :desired_capabilities => capabilities354)5556=end57#capabilities = Selenium::WebDriver::Remote::Capabilities.firefox58#capabilities.setPlatform("WINDOWS"); 59#@browser = Selenium::WebDriver.for(:desired_capabilities => :capabilities)606162#==============63 @browser.manage.window.maximize64 @accept_next_alert = true65 @browser.manage.timeouts.implicit_wait = 2066 #@wait = Selenium::WebDriver::Wait.new(:timeout => 30)67 @verification_errors = []68 @browser.get(data_for(:home)["url"])69end70=begin 7172 @verification_errors = []73 @verification_count=0
...
browser.rb
Source:browser.rb
...23 when :headless_firefox24 headless_firefox_browser_options25 end26 end27 def capabilities28 @option ||=29 case type30 when :chrome31 ::Selenium::WebDriver::Chrome::Options.new32 when :firefox33 ::Selenium::WebDriver::Firefox::Options.new34 end35 end36 # driver_path can be configured as a proc. The webdrivers gem uses this37 # proc to update web drivers. Running this proc early allows us to only38 # update the webdriver once and avoid race conditions when using39 # parallel tests.40 def preload41 case type42 when :chrome43 if ::Selenium::WebDriver::Service.respond_to? :driver_path=44 ::Selenium::WebDriver::Chrome::Service.driver_path.try(:call)45 else46 # Selenium <= v3.141.047 ::Selenium::WebDriver::Chrome.driver_path48 end49 when :firefox50 if ::Selenium::WebDriver::Service.respond_to? :driver_path=51 ::Selenium::WebDriver::Firefox::Service.driver_path.try(:call)52 else53 # Selenium <= v3.141.054 ::Selenium::WebDriver::Firefox.driver_path55 end56 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...
capabilities
Using AI Code Generation
1element = driver.find_element(:name, 'q')2driver = Selenium::WebDriver::Remote::Driver.new(:url => "http://localhost:4444/wd/hub", :desired_capabilities => caps)3element = driver.find_element(:name, 'q')4element = driver.find_element(:name, 'q')5element = driver.find_element(:name, 'q')
capabilities
Using AI Code Generation
1driver = Selenium::WebDriver.for(:chrome, :desired_capabilities => caps)2driver.get('http://www.google.com')3driver = Selenium::WebDriver.for(:chrome, :desired_capabilities => caps)4driver.get('http://www.google.com')5driver = Selenium::WebDriver.for(:chrome, :desired_capabilities => caps)6driver.get('http://www.google.com')7driver = Selenium::WebDriver.for(:chrome, :desired_capabilities => caps)8driver.get('http://www.google.com')9driver = Selenium::WebDriver.for(:chrome, :desired_capabilities => caps)10driver.get('http://www.google.com')11driver = Selenium::WebDriver.for(:chrome, :desired_capabilities => caps)12driver.get('http://www.google.com')
capabilities
Using AI Code Generation
1 assert(@driver.capabilities.browser_name == "firefox")2 assert(@driver.capabilities.version == "26.0")3 assert(@driver.capabilities.platform == :windows)4 assert(@driver.capabilities.javascript_enabled == true)
capabilities
Using AI Code Generation
1caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"args" => [ "start-maximized" ]})2caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"args" => [ "start-maximized" ]})3caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"args" => [ "start-maximized" ]})4caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"args" => [ "start-maximized" ]})
capabilities
Using AI Code Generation
1driver.get('http://www.google.com')2driver = Selenium::WebDriver.for(:chrome, :desired_capabilities => caps)3driver.get('http://www.google.com')4driver = Selenium::WebDriver.for(:chrome, :desired_capabilities => caps)5driver.get('http://www.google.com')6driver = Selenium::WebDriver.for(:chrome, :desired_capabilities => caps)7driver.get('http://www.google.com')8driver = Selenium::WebDriver.for(:chrome, :desired_capabilities => caps)9driver.get('http://www.google.com')
capabilities
Using AI Code Generation
1 assert(@driver.capabilities.browser_name == "firefox")2 assert(@driver.capabilities.version == "26.0")3 assert(@driver.capabilities.platform == :windows)4 assert(@driver.capabilities.javascript_enabled == true)
capabilities
Using AI Code Generation
1caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"args" => [ "start-maximized" ]})2caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"args" => [ "start-maximized" ]})3caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"args" => [ "start-maximized" ]})4caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"args" => [ "start-maximized" ]})
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!!