我正在使用分布式 ruby,以便我可以将 selenium web-driver 对象保存在一个脚本中,并在运行客户端时在下一个脚本中使用相同的对象我收到一个错误,指出 #<Drb::DRbConnError: connection closed> .
有没有人尝试过这个或者我们如何克服这个问题?
以下是我的脚本
服务器.rb
require 'drb/drb'
# The URI for the server to connect to
URI="druby://localhost:8787"
# Allows sharing of variables between component runs
class TestScope
# class variable
@@variables = {}
def save_variable(key, value)
@@variables.store(key, value)
end
def get_variable(key)
return @@variables[key]
end
def get_size
return @@variables.size
end
end
# The object that handles requests on the server
FRONT_OBJECT=TestScope.new
DRb.start_service(URI, FRONT_OBJECT, safe_level: 1, verbose: true)
# Wait for the drb server thread to finish before exiting.
DRb.thread.join
客户端1.rb
require 'drb/drb'
require 'selenium-webdriver'
# The URI to connect to
SERVER_URI="druby://localhost:8787"
# Start a local DRbServer to handle callbacks.
# Not necessary for this small example, but will be required
# as soon as we pass a non-marshallable object as an argument
# to a dRuby call.
# Note: this must be called at least once per process to take any effect.
# This is particularly important if your application forks.
DRb.start_service
test_scope = DRbObject.new_with_uri(SERVER_URI)
driver = Selenium::WebDriver::Driver.for :firefox
driver.navigate.to "http://www.google.com"
puts "Saving the driver object to the test scope"
test_scope.save_variable('driver', driver)
客户端2.rb
require 'drb/drb'
require 'selenium-webdriver'
# The URI to connect to
SERVER_URI="druby://localhost:8787"
# Start a local DRbServer to handle callbacks.
# Not necessary for this small example, but will be required
# as soon as we pass a non-marshallable object as an argument
# to a dRuby call.
# Note: this must be called at least once per process to take any effect.
# This is particularly important if your application forks.
DRb.start_service
test_scope = DRbObject.new_with_uri(SERVER_URI)
puts "Fetching the driver object from the test scope"
driver1 = test_scope.get_variable('driver')
driver1.navigate.to "http://www.yahoo.com"