加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 如何覆盖类方法

发布时间:2020-12-16 21:27:46 所属栏目:百科 来源:网络整理
导读:我想重写方法Selenium :: WebDriver.for.这是我试过的: module SeleniumWebDriverExtension def self.for(browser,*args) if browser != :phantomjs super(browser,*args) else options = { "phantomjs.cli.args" = ["--ssl-protocol=tlsv1"] } capabilitie
我想重写方法Selenium :: WebDriver.for.这是我试过的:
module SeleniumWebDriverExtension
  def self.for(browser,*args)
    if browser != :phantomjs
      super(browser,*args)
    else
      options = {
          "phantomjs.cli.args" => ["--ssl-protocol=tlsv1"]
      }
      capabilities = Selenium::WebDriver::Remote::Capabilities.phantomjs(options)
      super(browser,desired_capabilities: capabilities)
    end
  end
end

Selenium::WebDriver.prepend(SeleniumWebDriverExtension)

但是当调用Selenium :: Webdriver.for(:phantomjs)时出现错误.

NoMethodError: super: no superclass method `for' for Selenium::WebDriver::Driver:Class

如何从重写方法调用原始方法?

解决方法

module SeleniumWebDriverExtension
  def for(browser,*args)
    ...
  end
end

Selenium::WebDriver.singleton_class.prepend(SeleniumWebDriverExtension)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读