Best Selenium code snippet using Selenium.WebDriver.Remote.chrome
test_environment.rb
Source:test_environment.rb
...86 when :opera87 create_opera_driver88 when :firefox89 create_firefox_driver90 when :chrome91 create_chrome_driver92 when :iphone93 create_iphone_driver94 when :safari95 create_safari_driver96 when :phantomjs97 create_phantomjs_driver98 else99 WebDriver::Driver.for driver100 end101 @create_driver_error_count -= 1 unless @create_driver_error_count == 0102 instance103 rescue => ex104 @create_driver_error = ex105 @create_driver_error_count += 1106 raise ex107 end108 def remote_capabilities109 caps = WebDriver::Remote::Capabilities.send(ENV['WD_REMOTE_BROWSER'] || 'firefox')110 caps.javascript_enabled = true111 caps.css_selectors_enabled = true112 caps113 end114 MAX_ERRORS = 4115 class DriverInstantiationError < StandardError116 end117 def check_for_previous_error118 return unless @create_driver_error && @create_driver_error_count >= MAX_ERRORS119 msg = "previous #{@create_driver_error_count} instantiations of driver #{driver.inspect} failed, not trying again"120 msg << " (#{@create_driver_error.message})"121 raise DriverInstantiationError, msg, @create_driver_error.backtrace122 end123 def create_remote_driver124 WebDriver::Driver.for(:remote,125 :desired_capabilities => remote_capabilities,126 :url => ENV['WD_REMOTE_URL'] || remote_server.webdriver_url,127 :http_client => keep_alive_client || http_client128 )129 end130 def create_opera_driver131 ENV['SELENIUM_SERVER_JAR'] = remote_server_jar132 WebDriver::Driver.for :opera, :logging_level => ENV['log'] ? :config : :severe133 end134 def create_firefox_driver135 if native_events?136 profile = WebDriver::Firefox::Profile.new137 profile.native_events = true138 WebDriver::Driver.for :firefox, :profile => profile139 else140 WebDriver::Driver.for :firefox141 end142 end143 def create_chrome_driver144 binary = ENV['chrome_binary']145 if binary146 WebDriver::Chrome.path = binary147 end148 server = ENV['chromedriver'] || ENV['chrome_server']149 if server150 WebDriver::Chrome.driver_path = server151 end152 args = []153 args << "--no-sandbox" if ENV['TRAVIS']154 WebDriver::Driver.for :chrome,155 :native_events => native_events?,156 :args => args157 # :http_client => keep_alive_client || http_client158 end159 def create_phantomjs_driver160 binary = ENV['phantomjs_binary']161 if binary162 WebDriver::PhantomJS.path = binary163 end164 WebDriver::Driver.for :phantomjs165 end166 def create_iphone_driver167 url = ENV['iphone_url']168 if url...
Base.rb
Source:Base.rb
...9 def browser(name)10 server = "localhost"11 server_remote = "remoteServerAddress"12 gridURL = "http://" + server + ":4444/wd/hub"13 pathToChromedriver = '/path/To/chromedriver/chromedriver'14 pathToIEDriverServer_x64 = '/path/To/IEDriverServer_x64/IEDriverServer_x64'15 pathToIEDriverServer_Win32 = '/path/To/IEDriverServer_Win32/IEDriverServer_Win32'16 case name17 when "firefox" then18 driver = Selenium::WebDriver.for :firefox19 driver.manage.window.maximize20 when "chrome" then21 Selenium::WebDriver::Chrome::Service.executable_path =22 File.absolute_path(pathToChromedriver)23 driver =24 Selenium::WebDriver.for :chrome, :switches => %w[--ignore-certificate-errors --disable-popup-blocking --disable-translate]25 driver.manage.window.maximize26 when "ie" then27 old_path = ENV['PATH']28 ENV['PATH'] = old_path + ';' + pathToIEDriverServer_Win3229 driver = Selenium::WebDriver.for :ie30 ENV['PATH'] = old_path31 driver.manage.window.maximize32 when "Android Chrome" then33 caps =34 Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" =>35 {"androidPackage" =>36 'com.android.chrome'}37 )38 driver = Selenium::WebDriver.for(:remote,39 :url => "http://" + server + ":9515/wd/hub",40 :desired_capabilities => caps)41 when "Android Chrome Remote" then42 caps =43 Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" =>44 {"androidPackage" =>45 'com.android.chrome'}46 )47 driver = Selenium::WebDriver.for(:remote,48 :url => "http://" + server_remote + ":9515/wd/hub",49 :desired_capabilities => caps)50 when "firefox grid local" then51 caps =52 Selenium::WebDriver::Remote::Capabilities.firefox(53 :version => "43.0",54 )55 driver = Selenium::WebDriver.for(:remote,56 :url => gridURL,57 :desired_capabilities => caps)58 driver.manage.window.maximize59 when "firefox grid remote" then60 caps =61 Selenium::WebDriver::Remote::Capabilities.firefox(62 :version =>63 "43.0 remote",64 )65 driver = Selenium::WebDriver.for(:remote,66 :url => gridURL,67 :desired_capabilities => caps)68 driver.manage.window.maximize69 when "chrome grid local" then70 caps =71 Selenium::WebDriver::Remote::Capabilities.chrome(72 :version => "47.0.2526.106 m (64-bit)",73 )74 driver = Selenium::WebDriver.for(:remote,75 :url => gridURL,76 :desired_capabilities => caps)77 driver.manage.window.maximize78 when "chrome grid remote" then79 caps =80 Selenium::WebDriver::Remote::Capabilities.chrome(81 :version => "47.0.2526.106 m remote",82 )83 driver = Selenium::WebDriver.for(:remote,84 :url => gridURL,85 :desired_capabilities => caps)86 driver.manage.window.maximize87 when "ie grid local" then88 caps =89 Selenium::WebDriver::Remote::Capabilities.ie(90 :version => "11.0 X64",91 )92 driver = Selenium::WebDriver.for(:remote,93 :url => gridURL,94 :desired_capabilities => caps)95 driver.manage.window.maximize96 when "ie grid remote" then97 caps =98 Selenium::WebDriver::Remote::Capabilities.ie(99 :version => "11.0 Win32",100 )101 driver = Selenium::WebDriver.for(:remote,102 :url => gridURL,103 :desired_capabilities => caps)104 driver.manage.window.maximize105 when "Android Chrome grid local" then106 caps =107 Selenium::WebDriver::Remote::Capabilities.chrome(108 "chromeOptions" =>109 {"androidPackage" => 'com.android.chrome'},110 :version => "android local"111 )112 driver = Selenium::WebDriver.for(:remote,113 :url => gridURL,114 :desired_capabilities => caps)115 when "Android Chrome grid remote" then116 caps =117 Selenium::WebDriver::Remote::Capabilities.chrome(118 "chromeOptions" =>119 {"androidPackage" => 'com.android.chrome'},120 :version => "android remote"121 )122 driver = Selenium::WebDriver.for(:remote,123 :url => gridURL,124 :desired_capabilities => caps)125 end126 return driver127 end128 def driver()129 return @driver130 end131 def quit()132 # æ«ãå¾
ã£ãå¾ãçµäº133 sleep(10)...
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...
webdriver.rb
Source:webdriver.rb
...28 Point = Struct.new(:x, :y)29 Dimension = Struct.new(:width, :height)30 Rectangle = Struct.new(:x, :y, :width, :height)31 Location = Struct.new(:latitude, :longitude, :altitude)32 autoload :Chrome, 'selenium/webdriver/chrome'33 autoload :Edge, 'selenium/webdriver/edge'34 autoload :Firefox, 'selenium/webdriver/firefox'35 autoload :IE, 'selenium/webdriver/ie'36 autoload :PhantomJS, 'selenium/webdriver/phantomjs'37 autoload :Remote, 'selenium/webdriver/remote'38 autoload :Safari, 'selenium/webdriver/safari'39 autoload :Support, 'selenium/webdriver/support'40 # @api private41 def self.root42 @root ||= File.expand_path('../..', __FILE__)43 end44 #45 # Create a new Driver instance with the correct bridge for the given browser46 #47 # @overload for(browser)48 # @param [:ie, :internet_explorer, :edge, :remote, :chrome, :firefox, :ff, :phantomjs, :safari] browser The browser to49 # create the driver for50 # @overload for(browser, opts)51 # @param [:ie, :internet_explorer, :edge, :remote, :chrome, :firefox, :ff, :phantomjs, :safari] browser The browser to52 # create the driver for53 # @param [Hash] opts Options passed to Driver.new54 #55 # @return [Driver]56 #57 # @see Selenium::WebDriver::Remote::Driver58 # @see Selenium::WebDriver::Firefox::Driver59 # @see Selenium::WebDriver::IE::Driver60 # @see Selenium::WebDriver::Edge::Driver61 # @see Selenium::WebDriver::Chrome::Driver62 # @see Selenium::WebDriver::PhantomJS::Driver63 # @see Selenium::WebDriver::Safari::Driver64 #65 # @example...
selenium_spec_chrome_remote.rb
Source:selenium_spec_chrome_remote.rb
...16 rescue StandardError17 if timer.expired?18 raise 'Selenium is not running. ' \19 "You can run a selenium server easily with: \n" \20 ' $ docker-compose up -d selenium_chrome'21 else22 puts 'Waiting for Selenium docker instance...'23 sleep 124 retry25 end26 end27end28def selenium_gte?(version)29 defined?(Selenium::WebDriver::VERSION) && (Selenium::WebDriver::VERSION.to_f >= version)30end31Capybara.register_driver :selenium_chrome_remote do |app|32 ensure_selenium_running!33 url = "http://#{selenium_host}:#{selenium_port}/wd/hub"34 caps = Selenium::WebDriver::Remote::Capabilities.chrome35 Capybara::Selenium::Driver.new app,36 browser: :remote,37 desired_capabilities: caps,38 url: url39end40CHROME_REMOTE_DRIVER = :selenium_chrome_remote41module TestSessions42 Chrome = Capybara::Session.new(CHROME_REMOTE_DRIVER, TestApp)43end44TestSessions::Chrome.driver.browser.file_detector = lambda do |args|45 # args => ["/path/to/file"]46 str = args.first.to_s47 str if File.exist?(str)48end49skipped_tests = %i[response_headers status_code trigger download]50# skip window tests when headless for now - closing a window not supported by chromedriver/chrome51skipped_tests << :windows if ENV['TRAVIS'] && (ENV['SKIP_WINDOW'] || ENV['HEADLESS'])52Capybara::SpecHelper.run_specs TestSessions::Chrome, CHROME_REMOTE_DRIVER.to_s, capybara_skip: skipped_tests do |example|53 case example.metadata[:full_description]54 when 'Capybara::Session selenium_chrome_remote #attach_file with multipart form should not break when using HTML5 multiple file input uploading multiple files',55 'Capybara::Session selenium_chrome_remote #attach_file with multipart form should fire change once for each set of files uploaded',56 'Capybara::Session selenium_chrome_remote #attach_file with multipart form should fire change once when uploading multiple files from empty'57 pending "Selenium with Remote Chrome doesn't support multiple file upload" unless selenium_gte?(3.14)58 end59end60RSpec.describe 'Capybara::Session with remote Chrome' do61 include Capybara::SpecHelper62 include_examples 'Capybara::Session', TestSessions::Chrome, CHROME_REMOTE_DRIVER63 include_examples Capybara::RSpecMatchers, TestSessions::Chrome, CHROME_REMOTE_DRIVER64 it 'is considered to be chrome' do65 expect(session.driver.send(:chrome?)).to be_truthy66 end67end...
chrome
Using AI Code Generation
1Java(TM) SE Runtime Environment (build 1.6.0_21-b06)2Java HotSpot(TM) Client VM (build 14.3-b01, mixed mode, sharing)3Java(TM) SE Runtime Environment (build 1.7.0_03-b05)4Java HotSpot(TM) Client VM (build 23.1-b03, mixed mode, sharing)
chrome
Using AI Code Generation
1driver.find_element(:name, 'q').send_keys "Selenium WebDriver"2driver.find_element(:name, 'btnK').click3driver.find_element(:name, 'q').send_keys "Selenium WebDriver"4driver.find_element(:name, 'btnK').click5driver.find_element(:name, 'q').send_keys "Selenium WebDriver"6driver.find_element(:name, 'btnK').click
chrome
Using AI Code Generation
1driver.find_element(:name, 'q').send_keys "Selenium WebDriver"2driver.find_element(:name, 'btnK').click3driver.find_element(:name, 'q').send_keys "Selenium WebDriver"4driver.find_element(:name, 'btnK').click5driver.find_element(:name, 'q').send_keys "Selenium WebDriver"6driver.find_element(:name, 'btnK').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!!