Best Selenium code snippet using Selenium.WebDriver.SpecSupport.extract_browser_from_bazel_target_name
test_environment.rb
Source:test_environment.rb
...22 attr_reader :driver23 def initialize24 @create_driver_error = nil25 @create_driver_error_count = 026 extract_browser_from_bazel_target_name27 @driver = (ENV['WD_SPEC_DRIVER'] || :chrome).to_sym28 @driver_instance = nil29 end30 def print_env31 puts "\nRunning Ruby specs:\n\n"32 env = current_env.merge(ruby: RUBY_DESCRIPTION)33 just = current_env.keys.map { |e| e.to_s.size }.max34 env.each do |key, value|35 puts "#{key.to_s.rjust(just)}: #{value}"36 end37 puts "\n"38 end39 def browser40 driver == :remote ? (ENV['WD_REMOTE_BROWSER'] || :chrome).to_sym : driver41 end42 def driver_instance43 @driver_instance || create_driver!44 end45 def reset_driver!(time = 0)46 quit_driver47 sleep time48 driver_instance49 end50 # TODO: optimize since this approach is not assured on IE51 def ensure_single_window52 driver_instance.window_handles[1..-1].each do |handle|53 driver_instance.switch_to.window(handle)54 driver_instance.close55 end56 driver_instance.switch_to.window(driver_instance.window_handles.first)57 end58 def quit_driver59 return unless @driver_instance60 @driver_instance.quit61 ensure62 @driver_instance = nil63 end64 def app_server65 @app_server ||= RackServer.new(root.join('common/src/web').to_s).tap(&:start)66 end67 def remote_server68 @remote_server ||= Selenium::Server.new(69 remote_server_jar,70 port: PortProber.above(4444),71 log: $DEBUG,72 background: true,73 timeout: 6074 )75 end76 def reset_remote_server77 @remote_server.stop if defined? @remote_server78 @remote_server = nil79 remote_server80 end81 def remote_server?82 !@remote_server.nil?83 end84 def remote_server_jar85 test_jar = "#{Pathname.new(Dir.pwd).join('rb')}/selenium_server_deploy.jar"86 built_jar = root.join('bazel-bin/java/server/src/org/openqa/selenium/grid/selenium_server_deploy.jar')87 jar = if File.exist?(test_jar) && ENV['DOWNLOAD_SERVER'].nil?88 test_jar89 elsif File.exist?(built_jar) && ENV['DOWNLOAD_SERVER'].nil?90 built_jar91 else92 Selenium::Server.download(:latest)93 end94 WebDriver.logger.info "Server Location: #{jar}"95 jar.to_s96 end97 def quit98 app_server.stop99 @remote_server.stop if defined? @remote_server100 @driver_instance = @app_server = @remote_server = nil101 end102 def url_for(filename)103 app_server.where_is filename104 end105 def root106 # prefer #realpath over #expand_path to avoid problems with UNC107 # see https://bugs.ruby-lang.org/issues/13515108 @root ||= Pathname.new('../../../../../../../').realpath(__FILE__)109 end110 def create_driver!(**opts, &block)111 check_for_previous_error112 method = "create_#{driver}_driver".to_sym113 instance = if private_methods.include?(method)114 send method, opts115 else116 WebDriver::Driver.for(driver, opts)117 end118 @create_driver_error_count -= 1 unless @create_driver_error_count.zero?119 if block120 begin121 yield(instance)122 ensure123 instance.quit124 end125 else126 @driver_instance = instance127 end128 rescue StandardError => e129 @create_driver_error = e130 @create_driver_error_count += 1131 raise e132 end133 private134 def current_env135 {136 browser: browser,137 driver: driver,138 version: driver_instance.capabilities.version,139 platform: Platform.os,140 ci: Platform.ci141 }142 end143 MAX_ERRORS = 4144 class DriverInstantiationError < StandardError145 end146 def check_for_previous_error147 return unless @create_driver_error && @create_driver_error_count >= MAX_ERRORS148 msg = "previous #{@create_driver_error_count} instantiations of driver #{driver.inspect} failed,"149 msg += " not trying again (#{@create_driver_error.message})"150 raise DriverInstantiationError, msg, @create_driver_error.backtrace151 end152 def create_remote_driver(opt = {})153 options = opt.delete(:capabilities)154 opt[:capabilities] = [WebDriver::Remote::Capabilities.send(browser)]155 opt[:capabilities] << options if options156 opt[:url] = ENV['WD_REMOTE_URL'] || remote_server.webdriver_url157 opt[:http_client] ||= WebDriver::Remote::Http::Default.new158 WebDriver::Driver.for(:remote, opt)159 end160 def create_firefox_driver(opt = {})161 WebDriver::Firefox.path = ENV['FIREFOX_BINARY'] if ENV['FIREFOX_BINARY']162 WebDriver::Driver.for :firefox, opt163 end164 def create_firefox_nightly_driver(opt = {})165 ENV['FIREFOX_BINARY'] = ENV['FIREFOX_NIGHTLY_BINARY']166 opt[:capabilities] = WebDriver::Firefox::Options.new(debugger_address: true)167 create_firefox_driver(opt)168 end169 def create_ie_driver(opt = {})170 opt[:capabilities] = WebDriver::IE::Options.new(require_window_focus: true)171 WebDriver::Driver.for :ie, opt172 end173 def create_chrome_driver(opt = {})174 WebDriver::Chrome.path = ENV['CHROME_BINARY'] if ENV['CHROME_BINARY']175 WebDriver::Driver.for :chrome, opt176 end177 def create_safari_preview_driver(opt = {})178 WebDriver::Safari.technology_preview!179 WebDriver::Driver.for :safari, opt180 end181 def create_edge_driver(opt = {})182 WebDriver::Edge.path = ENV['EDGE_BINARY'] if ENV['EDGE_BINARY']183 WebDriver::Driver.for :edge, opt184 end185 def extract_browser_from_bazel_target_name186 name = ENV['TEST_TARGET']187 return unless name188 case name189 when %r{//rb:remote-(.+)-test}190 ENV['WD_REMOTE_BROWSER'] = Regexp.last_match(1).tr('-', '_')191 ENV['WD_SPEC_DRIVER'] = 'remote'192 when %r{//rb:(.+)-test}193 ENV['WD_SPEC_DRIVER'] = Regexp.last_match(1).tr('-', '_')194 else195 raise "Don't know how to extract browser name from #{name}"196 end197 end198 end199 end # SpecSupport...
extract_browser_from_bazel_target_name
Using AI Code Generation
1puts Selenium::WebDriver::SpecSupport.extract_browser_from_bazel_target_name("chrome")2puts Selenium::WebDriver::SpecSupport.extract_browser_from_bazel_target_name("firefox")3puts Selenium::WebDriver::SpecSupport.extract_browser_from_bazel_target_name("ie")4puts Selenium::WebDriver::SpecSupport.extract_browser_from_bazel_target_name("edge")5puts Selenium::WebDriver::SpecSupport.extract_browser_from_bazel_target_name("safari")
extract_browser_from_bazel_target_name
Using AI Code Generation
1extract_browser_from_bazel_target_name(ENV['TEST_TARGET'])2Selenium::WebDriver::SpecSupport.extract_browser_from_bazel_target_name(ENV['TEST_TARGET'])3extract_browser_from_bazel_target_name(ENV['TEST_TARGET'])4Selenium::WebDriver::SpecSupport.extract_browser_from_bazel_target_name(ENV['TEST_TARGET'])5Selenium::WebDriver::SpecSupport.extract_browser_from_bazel_target_name(ENV['TEST_TARGET'])6Selenium::WebDriver::SpecSupport.extract_browser_from_bazel_target_name(ENV['TEST_TARGET'])7extract_browser_from_bazel_target_name(ENV['TEST_TARGET'])8Selenium::WebDriver::SpecSupport.extract_browser_from_bazel_target_name(ENV['TEST_TARGET'])
extract_browser_from_bazel_target_name
Using AI Code Generation
1puts Selenium::WebDriver::SpecSupport.extract_browser_from_bazel_target_name('chrome')2puts Selenium::WebDriver::SpecSupport.extract_browser_from_bazel_target_name('firefox')3puts Selenium::WebDriver::SpecSupport.extract_browser_from_bazel_target_name('edge')4puts Selenium::WebDriver::SpecSupport.extract_browser_from_bazel_target_name('safari')5puts Selenium::WebDriver::SpecSupport.extract_browser_from_bazel_target_name('safari-preview')6puts Selenium::WebDriver::SpecSupport.extract_browser_from_bazel_target_name('internet-explorer')7puts Selenium::WebDriver::SpecSupport.extract_browser_from_bazel_target_name('internet-explorer-11')8puts Selenium::WebDriver::SpecSupport.extract_browser_from_bazel_target_name('edge-legacy')
extract_browser_from_bazel_target_name
Using AI Code Generation
1Selenium::WebDriver::SpecSupport.extract_browser_from_bazel_target_name(ARGV[0])2extract_browser_from_bazel_target_name(ARGV[0])3Selenium::WebDriver::SpecSupport.extract_browser_from_bazel_target_name(ARGV[0])4Selenium::WebDriver::SpecSupport::extract_browser_from_bazel_target_name(ARGV[0])5Selenium::WebDriver::SpecSupport::extract_browser_from_bazel_target_name(ARGV[0])6Selenium::WebDriver::SpecSupport::extract_browser_from_bazel_target_name(ARGV[0])
extract_browser_from_bazel_target_name
Using AI Code Generation
1extract_browser_from_bazel_target_name(ENV['TEST_TARGET'])2Selenium::WebDriver::SpecSupport.extract_browser_from_bazel_target_name(ENV['TEST_TARGET'])3extract_browser_from_bazel_target_name(ENV['TEST_TARGET'])4Selenium::WebDriver::SpecSupport.extract_browser_from_bazel_target_name(ENV['TEST_TARGET'])5Selenium::WebDriver::SpecSupport.extract_browser_from_bazel_target_name(ENV['TEST_TARGET'])6Selenium::WebDriver::SpecSupport.extract_browser_from_bazel_target_name(ENV['TEST_TARGET'])7extract_browser_from_bazel_target_name(ENV['TEST_TARGET'])8Selenium::WebDriver::SpecSupport.extract_browser_from_bazel_target_name(ENV['TEST_TARGET'])
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!!