Best Selenium code snippet using Selenium.WebDriver.SpecSupport.current_env
test_environment.rb
Source:test_environment.rb
...26 @driver = (ENV['WD_SPEC_DRIVER'] || :chrome).to_sym27 end28 def print_env29 puts "\nRunning Ruby specs:\n\n"30 env = current_env.merge(ruby: RUBY_DESCRIPTION)31 just = current_env.keys.map { |e| e.to_s.size }.max32 env.each do |key, value|33 puts "#{key.to_s.rjust(just)}: #{value}"34 end35 puts "\n"36 end37 def browser38 driver == :remote ? (ENV['WD_REMOTE_BROWSER'] || :chrome).to_sym : driver39 end40 def driver_instance41 @driver_instance || create_driver!42 end43 def reset_driver!(time = 0)44 quit_driver45 sleep time46 driver_instance47 end48 # TODO: optimize since this approach is not assured on IE49 def ensure_single_window50 driver_instance.window_handles[1..-1].each do |handle|51 driver_instance.switch_to.window(handle)52 driver_instance.close53 end54 driver_instance.switch_to.window(driver_instance.window_handles.first)55 end56 def quit_driver57 return unless @driver_instance58 @driver_instance.quit59 ensure60 @driver_instance = nil61 end62 def app_server63 @app_server ||= RackServer.new(root.join('common/src/web').to_s).tap(&:start)64 end65 def remote_server66 @remote_server ||= Selenium::Server.new(67 remote_server_jar,68 port: PortProber.above(4444),69 log: $DEBUG,70 background: true,71 timeout: 6072 )73 end74 def reset_remote_server75 @remote_server.stop if defined? @remote_server76 @remote_server = nil77 remote_server78 end79 def remote_server?80 !@remote_server.nil?81 end82 def remote_server_jar83 local_selenium_jar = 'selenium_server_deploy.jar'84 downloaded_selenium_jar = "selenium-server-standalone-#{Selenium::Server.latest}.jar"85 directory_names = [root, root.join('rb'), root.join('build'), root.join('build/rb')]86 file_names = [local_selenium_jar, downloaded_selenium_jar]87 file_names.delete(local_selenium_jar) if ENV['DOWNLOAD_SERVER']88 files = file_names.each_with_object([]) do |file, array|89 directory_names.each { |dir| array << "#{dir}/#{file}" }90 end91 jar = files.find { |file| File.exist?(file) } ||92 Selenium::Server.download(:latest) && files.find { |file| File.exist?(file) }93 WebDriver.logger.info "Server Location: #{jar}"94 puts "Server Location: #{jar}"95 jar96 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, not trying again"...
guards.rb
Source:guards.rb
...6 def guards7 @guards ||= Hash.new { |hash, key| hash[key] = [] }8 end9 def record(guard_name, opts, data)10 opts = current_env.merge(opts)11 key = "#{opts[:browser]}/#{opts[:driver]}/#{opts[:platform]}"12 guards[key] << [guard_name, data]13 end14 def report15 gs = guards["#{GlobalTestEnv.browser}/#{GlobalTestEnv.driver}/#{Platform.os}"]16 print "\n\nSpec guards for this implementation: "17 if gs.empty?18 puts "none."19 else20 puts21 gs.each do |guard_name, data|22 puts "\t#{guard_name.to_s.ljust(20)}: #{data.inspect}"23 end24 end25 end26 def current_env27 {:browser => GlobalTestEnv.browser, :driver => GlobalTestEnv.driver, :platform => Platform.os}28 end29 def env_matches?(env)30 env.all? do |key, value|31 if value.kind_of?(Array)32 value.include? current_env[key]33 else34 value == current_env[key]35 end36 end37 end38 end39 def not_compliant_on(opts = {}, &blk)40 Guards.record(:not_compliant, opts, :file => caller.first)41 yield unless Guards.env_matches?(opts)42 end43 def deviates_on(opts = {}, &blk)44 Guards.record(:deviates, opts, :file => caller.first)45 yield unless Guards.env_matches?(opts)46 end47 def compliant_on(opts = {}, &blk)48 Guards.record(:compliant_on, opts, :file => caller.first)...
current_env
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').click7C:/Ruby192/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/spec_support.rb:49:in `current_env': uninitialized constant Selenium::WebDriver::SpecSupport::SpecSupport (NameError) from C:/Ruby192/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/spec_support.rb:73:in`current_browser' from C:/Ruby192/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/spec_support.rb:79:in `current_driver' from C:/Ruby192/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2
current_env
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').click7C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.39.0/lib/selenium/webdriver/spec_support.rb:25:in `current_env': undefined method `[]' for nil:NilClass (NoMethodError)
current_env
Using AI Code Generation
1config = YAML.load_file('config.yml')2browser.text_field(:id => 'username').send_keys config['username']3browser.text_field(:name => 'password').send_keys config['password']4browser.button(:type => 'submit').click
current_env
Using AI Code Generation
1current_url()2screenshot()3File.open("screenshot.png", "wb") do |f|4 f.write(screenshot.image)
current_env
Using AI Code Generation
1config = YAML.load_file('config.yml')2browser.text_field(:id => 'username').send_keys config['username']3browser.text_field(:name => 'password').send_keys config['password']4browser.button(:type => 'submit').click
current_env
Using AI Code Generation
1config = YAML.load_file('config.yml')2browser.text_field(:id => 'username').send_keys config['username']3browser.text_field(:name => 'password').send_keys config['password']4browser.button(:type => 'submit').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!!