首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有Selenium WebDriver的DRuby

带有Selenium WebDriver的DRuby
EN

Stack Overflow用户
提问于 2021-09-28 11:47:05
回答 1查看 30关注 0票数 0

我使用分布式ruby,这样我就可以将selenium web-driver对象保存在一个脚本中,并在下一个脚本中使用相同的对象。当我运行客户端时,我得到一个错误,指出#。

有没有人尝试过这一点,或者我们如何克服这个问题?

下面是我的脚本

Server.rb

代码语言:javascript
复制
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

Client1.rb

代码语言:javascript
复制
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)

Client2.rb

代码语言:javascript
复制
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"
EN

回答 1

Stack Overflow用户

发布于 2021-10-04 04:50:47

为了使用DRb共享对象,必须在dRb服务器中定义该类,因为它允许同一或不同机器上的一个Ruby进程中的对象调用另一个Ruby进程中的对象上的方法。

如果存在需要在dRb客户端上创建对象并在其他DRb客户端中使用该对象的场景。我们需要使用ruby脚本运行器并在scriptrunner.rb中定义对象,以便多个客户端可以使用它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69361162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档