Best Selenium code snippet using Selenium.WebDriver.stop_server
service.rb
Source:service.rb
...21 # responsible for starting and stopping driver implementations.22 #23 # Subclasses must implement the following private methods:24 # * #start_process25 # * #stop_server26 # * #cannot_connect_error_text27 #28 # @api private29 #30 class Service31 START_TIMEOUT = 2032 SOCKET_LOCK_TIMEOUT = 4533 STOP_TIMEOUT = 2034 @executable = nil35 @missing_text = nil36 class << self37 attr_reader :executable, :missing_text38 end39 attr_accessor :host40 def initialize(executable_path, port, driver_opts)41 @executable_path = binary_path(executable_path)42 @host = Platform.localhost43 @port = Integer(port)44 @extra_args = extract_service_args(driver_opts)45 raise Error::WebDriverError, "invalid port: #{@port}" if @port < 146 end47 def binary_path(path)48 path = Platform.find_binary(self.class.executable) if path.nil?49 raise Error::WebDriverError, self.class.missing_text unless path50 Platform.assert_executable path51 path52 end53 def start54 if process_running?55 raise "already started: #{uri.inspect} #{@executable_path.inspect}"56 end57 Platform.exit_hook { stop } # make sure we don't leave the server running58 socket_lock.locked do59 find_free_port60 start_process61 connect_until_stable62 end63 end64 def stop65 stop_server66 @process.poll_for_exit STOP_TIMEOUT67 rescue ChildProcess::TimeoutError68 ensure69 stop_process70 end71 def uri72 @uri ||= URI.parse("http://#{@host}:#{@port}")73 end74 private75 def build_process(*command)76 WebDriver.logger.debug("Executing Process #{command}")77 @process = ChildProcess.build(*command)78 if WebDriver.logger.debug?79 @process.io.stdout = @process.io.stderr = WebDriver.logger.io80 elsif Platform.jruby?81 # Apparently we need to read the output of drivers on JRuby.82 @process.io.stdout = @process.io.stderr = File.new(Platform.null_device, 'w')83 end84 @process85 end86 def connect_to_server87 Net::HTTP.start(@host, @port) do |http|88 http.open_timeout = STOP_TIMEOUT / 289 http.read_timeout = STOP_TIMEOUT / 290 yield http91 end92 end93 def find_free_port94 @port = PortProber.above(@port)95 end96 def start_process97 raise NotImplementedError, 'subclass responsibility'98 end99 def stop_process100 return if process_exited?101 @process.stop STOP_TIMEOUT102 @process.io.stdout.close if Platform.jruby? && !WebDriver.logger.debug?103 end104 def stop_server105 return if process_exited?106 connect_to_server { |http| http.get('/shutdown') }107 end108 def process_running?109 defined?(@process) && @process && @process.alive?110 end111 def process_exited?112 @process.nil? || @process.exited?113 end114 def connect_until_stable115 socket_poller = SocketPoller.new @host, @port, START_TIMEOUT116 return if socket_poller.connected?117 raise Error::WebDriverError, cannot_connect_error_text118 end...
in_process_server.rb
Source:in_process_server.rb
...12 ffi_lib WebDriver::IE::DLLS[:win32]13 end14 ffi_convention :stdcall15 attach_function :start_server, :StartServer, [:int], :pointer16 attach_function :stop_server, :StopServer, [:pointer], :void17 attach_function :session_count, :GetServerSessionCount, [], :int18 attach_function :current_port, :GetServerPort, [], :int19 attach_function :is_running, :ServerIsRunning, [], :bool20 def initialize21 @handle = nil22 end23 #24 # Starts the server, communicating on the specified port, if it is not already running25 #26 def start(start_port, timeout)27 return port if running?28 @handle = self.class.start_server(start_port)29 unless SocketPoller.new(Platform.localhost, start_port, timeout).connected?30 raise Error::WebDriverError, "unable to connect to IE server within #{timeout} seconds"31 end32 start_port33 end34 def stop35 return if session_count != 0 || @handle.nil?36 self.class.stop_server @handle37 @handle = nil38 end39 def running?40 self.class.is_running41 end42 def port43 self.class.current_port44 end45 def uri46 "http://#{Platform.localhost}:#{port}"47 end48 private49 def session_count50 self.class.session_count...
server.rb
Source:server.rb
...12 ffi_lib WebDriver::IE::DLLS[:win32]13 end14 ffi_convention :stdcall15 attach_function :start_server, :StartServer, [:int], :pointer16 attach_function :stop_server, :StopServer, [:pointer], :void17 attach_function :session_count, :GetServerSessionCount, [], :int18 attach_function :current_port, :GetServerPort, [], :int19 attach_function :is_running, :ServerIsRunning, [], :bool20 def initialize21 @handle = nil22 end23 #24 # Starts the server, communicating on the specified port, if it is not already running25 #26 def start(start_port)27 return port if running?28 @handle = self.class.start_server(start_port)29 start_port30 end31 def stop32 return if session_count != 0 || @handle.nil?33 self.class.stop_server @handle34 @handle = nil35 end36 def running?37 self.class.is_running38 end39 def port40 self.class.current_port41 end42 private43 def session_count44 self.class.session_count45 end46 end # Server47 end # IE...
stop_server
Using AI Code Generation
1 (unknown error: DevToolsActivePort file doesn't exist)2 (The process started from chrome location C:\Users\DELL\AppData\Local\Google\Chrome\Application\chrome.exe is no longer running, so ChromeDriver is assuming that Chrome has crashed.)3 (Driver info: chromedriver=2.28.455506 (1b8e8d2e2e2d1e9b9c8b8b0f7d2e3d2b2c2b8c3),platform=Windows NT 6.1.7601 SP1 x86_64) (Selenium::WebDriver::Error::UnknownError)
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!!