Best Selenium code snippet using Selenium.WebDriver.uri
provider.rb
Source:provider.rb
...82end8384class Provider8586 attr_accessor :drb_server_uri, :ring_server_uri8788 def initialize(params = {})89 @drb_server_host = params[:drb_server_host] || external_interface90 @drb_server_port = params[:drb_server_port] || 091 @ring_server_host = params[:ring_server_host] || external_interface92 @ring_server_port = params[:ring_server_port] || Rinda::Ring_PORT93 @controller_uri = params[:controller_uri]94 @renewer = params[:renewer] || Rinda::SimpleRenewer.new95 @driver = params[:driver] || 'webdriver'96 @browser_type = params[:browser_type] || 'firefox'9798 logfile = params[:logfile] || STDOUT99 @log = Logger.new(logfile, 'daily')100 @log.level = params[:loglevel] || Logger::INFO101 @log.datetime_format = "%Y-%m-%d %H:%M:%S "102 end103104 ##105 # Start providing Watir objects on the ring server106 def start(params = {})107 # create a DRb 'front' object108 watir_provider = Watir::Provider.new(@driver)109 @log.debug("Watir provider is : #{watir_provider}")110 architecture = Config::CONFIG['arch']111 hostname = ENV['SERVER_NAME'] || %x{hostname}.strip112113 # setup the security--remember to call before DRb.start_service()114 DRb.install_acl(ACL.new(@acls))115116 # start the DRb Server117 drb_server = DRb.start_service(118 "druby://#{@drb_server_host}:#{@drb_server_port}")119120 # obtain DRb Server uri121 @drb_server_uri = drb_server.uri122 @log.info("Provider started on : #{@drb_server_uri}")123124 # create a service tuple125 @tuple = [126 :WatirGrid,127 :WatirProvider,128 watir_provider,129 'A watir provider',130 hostname,131 architecture,132 @driver,133 @browser_type134 ]135136 # locate the Rinda Ring Server via a UDP broadcast137 @log.debug("Broadcast Ring Server : druby://#{@ring_server_host}:#{@ring_server_port}")138 find_ring_server139140 # advertise this service on the primary remote tuple space141 @ring_server.write(@tuple, @renewer)142143 # log DRb server uri144 @log.info("Provider registered : #{@controller_uri}")145146 # wait for explicit stop via ctrl-c147 DRb.thread.join if __FILE__ == $0148 end149150 ##151 # Stop the provider by shutting down the DRb service152 def stop153 DRb.stop_service154 @log.info("Provider stopped on : #{@drb_server_uri}")155 end156157 private158159 ##160 # Locate the Rinda Ring Server via a UDP broadcast or direct URI161 def find_ring_server162 if @controller_uri163 @ring_server = DRbObject.new(nil, @controller_uri)164 else165 @ring_server = Rinda::RingFinger.new(166 @ring_server_host, @ring_server_port)167 @ring_server = @ring_server.lookup_ring_any168 @controller_uri = "druby://#{@ring_server_host}:#{@ring_server_port}"169 end170 @log.info("Controller found on : #{@controller_uri}")171 end172173 ##174 # Get the external facing interface for this server175 def external_interface176 begin177 UDPSocket.open {|s| s.connect('ping.watirgrid.com', 1); s.addr.last }178 rescue179 '127.0.0.1'180 end181 end182end
...
driver.rb
Source:driver.rb
1require "uri"2class Capybara::Selenium::Driver < Capybara::Driver::Base3 DEFAULT_OPTIONS = {4 :browser => :firefox5 }6 SPECIAL_OPTIONS = [:browser]7 attr_reader :app, :options8 def browser9 unless @browser10 @browser = Selenium::WebDriver.for(options[:browser], options.reject { |key,val| SPECIAL_OPTIONS.include?(key) })11 main = Process.pid12 at_exit do13 # Store the exit status of the test run since it goes away after calling the at_exit proc...14 @exit_status = $!.status if $!.is_a?(SystemExit)15 quit if Process.pid == main16 exit @exit_status if @exit_status # Force exit with stored status17 end18 end19 @browser20 end21 def initialize(app, options={})22 begin23 require 'selenium-webdriver'24 rescue LoadError => e25 if e.message =~ /selenium-webdriver/26 raise LoadError, "Capybara's selenium driver is unable to load `selenium-webdriver`, please install the gem and add `gem 'selenium-webdriver'` to your Gemfile if you are using bundler."27 else28 raise e29 end30 end31 @app = app32 @browser = nil33 @exit_status = nil34 @frame_handles = {}35 @options = DEFAULT_OPTIONS.merge(options)36 end37 def visit(path)38 browser.navigate.to(path)39 end40 def go_back41 browser.navigate.back42 end43 def go_forward44 browser.navigate.forward45 end46 def html47 browser.page_source48 end49 def title50 browser.title51 end52 def current_url53 browser.current_url54 end55 def find_xpath(selector)56 browser.find_elements(:xpath, selector).map { |node| Capybara::Selenium::Node.new(self, node) }57 end58 def find_css(selector)59 browser.find_elements(:css, selector).map { |node| Capybara::Selenium::Node.new(self, node) }60 end61 def wait?; true; end62 def needs_server?; true; end63 def execute_script(script)64 browser.execute_script script65 end66 def evaluate_script(script)67 browser.execute_script "return #{script}"68 end69 def save_screenshot(path, options={})70 browser.save_screenshot(path)71 end72 def reset!73 # Use instance variable directly so we avoid starting the browser just to reset the session74 if @browser75 begin @browser.manage.delete_all_cookies76 rescue Selenium::WebDriver::Error::UnhandledError77 # delete_all_cookies fails when we've previously gone78 # to about:blank, so we rescue this error and do nothing79 # instead.80 end81 uri = URI(Capybara::EMPTY_HTML_FILE_PATH)82 uri.scheme = "file"83 @browser.navigate.to(uri.to_s)84 end85 end86 ##87 #88 # Webdriver supports frame name, id, index(zero-based) or {Capybara::Element} to find iframe89 #90 # @overload within_frame(index)91 # @param [Integer] index index of a frame92 # @overload within_frame(name_or_id)93 # @param [String] name_or_id name or id of a frame94 # @overload within_frame(element)95 # @param [Capybara::Node::Base] a_node frame element96 #97 def within_frame(frame_handle)...
lib.rb
Source:lib.rb
...60 end61end6263class Mechanize64 def parse_html(data, uri="http://www.example.com")65 uri = URI.parse(uri) unless uri.is_a?(URI)66 page = Mechanize::Page.new( uri, {'content-type' => 'text/html'}, data, "200", self)67 self.history.push(page)68 page69 end7071 def set72 self.max_history = 173 self.read_timeout = 574 self.open_timeout = 575 self.follow_meta_refresh = true76 self.redirection_limit=577 self.user_agent_alias = 'Windows Mozilla'78 self.request_headers = {'accept-language' => 'en'}79 self80 end
...
service.rb
Source:service.rb
...8 START_TIMEOUT = 209 STOP_TIMEOUT = 510 DEFAULT_PORT = 891011 MISSING_TEXT = "Unable to find javadriver executable."12 attr_reader :uri13 def self.default_service(executable_path, port = nil)14 new executable_path, port || ::Selenium::WebDriver::PortProber.above(DEFAULT_PORT)15 end16 def initialize(executable_path, port)17 @uri = URI.parse "http://#{::Selenium::WebDriver::Platform.localhost}:#{port}"18 @executable = executable_path19 end20 def start(args = [], profile)21 if @process && @process.alive?22 raise "already started: #{@uri.inspect} #{@executable.inspect}"23 end24 @profile = profile25 @process = create_process(args)26 @process.start27 socket_poller = ::Selenium::WebDriver::SocketPoller.new ::Selenium::WebDriver::Platform.localhost, @uri.port, START_TIMEOUT28 unless socket_poller.connected?29 raise Error::WebDriverError, "unable to connect to javadriver @ #{@uri} after #{START_TIMEOUT} seconds"30 end31 ::Selenium::WebDriver::Platform.exit_hook { stop } # make sure we don't leave the server running32 end33 def stop34 return if @process.nil? || @process.exited?35 Net::HTTP.start(uri.host, uri.port) do |http|36 http.open_timeout = STOP_TIMEOUT / 237 http.read_timeout = STOP_TIMEOUT / 238 http.get("/shutdown")39 end40 @process.poll_for_exit STOP_TIMEOUT41 rescue ChildProcess::TimeoutError42 # ok, force quit43 @process.stop STOP_TIMEOUT44 if ::Selenium::WebDriver::Platform.jruby? && !$DEBUG45 @process.io.close rescue nil46 end47 end48 def create_process(args)49 @profile.set_port(uri.port)50 command_line = @profile.get_commandline51 vm_command = @profile.get_java_command 52 @executable = vm_command if !vm_command.nil?53 server_command = [@executable, *command_line, *args]54 process = ChildProcess.build(*server_command.compact)55 if $DEBUG == true56 process.io.inherit!57 elsif ::Selenium::WebDriver::Platform.jruby?58 # apparently we need to read the output for javadriver to work on jruby59 process.io.stdout = process.io.stderr = File.new(::Selenium::WebDriver::Platform.null_device, 'w')60 end61 process62 end63 end # Service...
selenium-webdriver.gemspec
Source:selenium-webdriver.gemspec
...16 DESCRIPTION17 s.license = 'Apache-2.0'18 s.homepage = 'https://selenium.dev'19 s.metadata = {20 'changelog_uri' => 'https://github.com/SeleniumHQ/selenium/blob/trunk/rb/CHANGES',21 'source_code_uri' => 'https://github.com/SeleniumHQ/selenium/tree/trunk/rb'22 }23 s.required_rubygems_version = Gem::Requirement.new('> 1.3.1') if s.respond_to? :required_rubygems_version=24 s.required_ruby_version = Gem::Requirement.new('>= 2.6')25 s.files = [26 'CHANGES',27 'LICENSE',28 'NOTICE',29 'Gemfile',30 'README.md',31 'selenium-webdriver.gemspec',32 'lib/selenium-webdriver.rb',33 'lib/selenium/server.rb',34 'lib/selenium/webdriver.rb'35 ] + Dir['lib/selenium/webdriver/**/*']...
remote.rb
Source:remote.rb
...15# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY16# KIND, either express or implied. See the License for the17# specific language governing permissions and limitations18# under the License.19require 'uri'20require 'selenium/webdriver/remote/bridge'21require 'selenium/webdriver/remote/driver'22require 'selenium/webdriver/remote/response'23require 'selenium/webdriver/remote/server_error'24require 'selenium/webdriver/remote/http/common'25require 'selenium/webdriver/remote/http/default'26require 'selenium/webdriver/remote/capabilities'27require 'selenium/webdriver/remote/oss/bridge'28require 'selenium/webdriver/remote/oss/commands'29require 'selenium/webdriver/remote/oss/driver'30require 'selenium/webdriver/remote/w3c/bridge'31require 'selenium/webdriver/remote/w3c/capabilities'32require 'selenium/webdriver/remote/w3c/commands'33require 'selenium/webdriver/remote/w3c/driver'...
selenium_webdriver_phantomjs_monkey_patch.rb
Source:selenium_webdriver_phantomjs_monkey_patch.rb
...22Selenium::WebDriver::PhantomJS::Service.class_eval do23 def start(args = [])24 require 'selenium/webdriver/common'25 if @process && @process.alive?26 raise "already started: #{@uri.inspect} #{@executable.inspect}"27 end28 puts "Starting monkey-patched PhantomJS Selenium Webdriver"29 # @process = create_process(args)30 # @process.start31 socket_poller = Selenium::WebDriver::SocketPoller.new Selenium::WebDriver::Platform.localhost, @uri.port, Selenium::WebDriver::PhantomJS::Service::START_TIMEOUT32 unless socket_poller.connected?33 raise Selenium::WebDriver::Error::WebDriverError, "unable to connect to phantomjs @ #{@uri} after #{Selenium::WebDriver::PhantomJS::Service::START_TIMEOUT} seconds"34 end35 Selenium::WebDriver::Platform.exit_hook { stop } # make sure we don't leave the server running36 end37end...
uri
Using AI Code Generation
1driver.find_element(:name, 'q').send_keys "Selenium"2driver.find_element(:name, 'btnG').click3driver.find_element(:name, 'q').send_keys "Selenium"4driver.find_element(:name, 'btnG').click
uri
Using AI Code Generation
1driver.find_element(:name, 'q').send_keys "Hello WebDriver!"2driver.find_element(:name, 'btnG').click3driver.find_element(:name, 'q').send_keys "Hello WebDriver!"4driver.find_element(:name, 'btnG').click5driver.find_element(:name, 'q').send_keys "Hello WebDriver!"6driver.find_element(:name, 'btnG').click7driver = Selenium::WebDriver.for(8driver.find_element(:name, 'q').send_keys "Hello WebDriver!"9driver.find_element(:name, 'btnG').click10driver.find_element(:name, 'q').send_keys "Hello WebDriver!"11driver.find_element(:name, 'btnG').click
uri
Using AI Code Generation
1element = driver.find_element(:name, 'q')2element = wait.until { driver.find_element(:name, 'q') }3element = driver.find_element(:name, 'q')
uri
Using AI Code Generation
1driver.navigate.to URI.parse("http://www.google.com")2driver.navigate.to URI.parse("http://www.google.com")3driver.navigate.to URI.parse("http://www.google.com")4driver.navigate.to URI.parse("http://www.google.com")5driver.navigate.to URI.parse("http://www.google.com")6driver.navigate.to URI.parse("http://www.google.com")
uri
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').click7driver.find_element(:name, 'q').send_keys "Selenium WebDriver"8driver.find_element(:name, 'btnK').click9driver.find_element(:name, 'q').send_keys "Selenium WebDriver"10driver.find_element(:name, 'btnK').click11driver.find_element(:name, 'q').send_keys "Selenium WebDriver"12driver.find_element(:name, 'btnK').click
uri
Using AI Code Generation
1Given(/^I go to Google$/) do2When(/^I search for Selenium$/) do3 element = @browser.find_element(:name, 'q')4Then(/^I should see the Selenium site$/) do5 wait = Selenium::WebDriver::Wait.new(:timeout => 10)6 wait.until { @browser.title.downcase.start_with? "selenium" }
uri
Using AI Code Generation
1driver.find_element(:name, 'q').send_keys "Selenium"2driver.find_element(:name, 'btnG').click3driver.find_element(:name, 'q').send_keys "Selenium"4driver.find_element(:name, 'btnG').click
uri
Using AI Code Generation
1driver.find_element(:name, 'q').send_keys "Hello WebDriver!"2driver.find_element(:name, 'btnG').click3driver.find_element(:name, 'q').send_keys "Hello WebDriver!"4driver.find_element(:name, 'btnG').click5driver.find_element(:name, 'q').send_keys "Hello WebDriver!"6driver.find_element(:name, 'btnG').click7driver = Selenium::WebDriver.for(8driver.find_element(:name, 'q').send_keys "Hello WebDriver!"9driver.find_element(:name, 'btnG').click10driver.find_element(:name, 'q').send_keys "Hello WebDriver!"11driver.find_element(:name, 'btnG').click
uri
Using AI Code Generation
1element = driver.find_element(:name, 'q')2element = wait.until { driver.find_element(:name, 'q') }3element = driver.find_element(:name, 'q')
uri
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').click7driver.find_element(:name, 'q').send_keys "Selenium WebDriver"8driver.find_element(:name, 'btnK').click9driver.find_element(:name, 'q').send_keys "Selenium WebDriver"10driver.find_element(:name, 'btnK').click11driver.find_element(:name, 'q').send_keys "Selenium WebDriver"12driver.find_element(:name, 'btnK').click
uri
Using AI Code Generation
1driver.find_element(:name, 'q').send_keys "Hello WebDriver!"2driver.find_element(:name, 'btnG').click3driver.find_element(:name, 'q').send_keys "Hello WebDriver!"4driver.find_element(:name, 'btnG').click5driver.find_element(:name, 'q').send_keys "Hello WebDriver!"6driver.find_element(:name, 'btnG').click7driver = Selenium::WebDriver.for(8driver.find_element(:name, 'q').send_keys "Hello WebDriver!"9driver.find_element(:name, 'btnG').click10driver.find_element(:name, 'q').send_keys "Hello WebDriver!"11driver.find_element(:name, 'btnG').click
uri
Using AI Code Generation
1element = driver.find_element(:name, 'q')2element = wait.until { driver.find_element(:name, 'q') }3element = driver.find_element(:name, 'q')
uri
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').click7driver.find_element(:name, 'q').send_keys "Selenium WebDriver"8driver.find_element(:name, 'btnK').click9driver.find_element(:name, 'q').send_keys "Selenium WebDriver"10driver.find_element(:name, 'btnK').click11driver.find_element(:name, 'q').send_keys "Selenium WebDriver"12driver.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!!