打开Chrome浏览器时,Capybara无法加载或添加扩展程序

时间:2014-08-26 06:03:11

标签: ruby google-chrome google-chrome-extension rubygems capybara

我正在尝试使用扩展程序/附加“mod标头”自动打开Chrome浏览器,以便我可以使用http标头,用户代理等发送网址。

问题是我的脚本(下面给出的代码段)是打开Chrome浏览器而没有扩展/附加“mod标头”,不知道我做错了什么?请帮忙

env.rb:


require 'rspec'
require 'cucumber'
require 'capybara'
require 'selenium-webdriver'
require 'capybara/cucumber'
require 'base64'

Capybara.app_host = "http://google.com"

settings = YAML.load_file(File.expand_path(File.dirname(__FILE__))+ "/../../config/settings.yml")
puts settings.inspect
puts settings["browser"]

case settings["browser"]
  when "CHROME"
    chrome_extensions = []
    my_capabilities = Hash.new
    # chrome_extension_path = '/Users/Duncan/Desktop/Browser_Testing/extension_1_2_4.crx'
    # puts '#{chrome_extension_path}'
    begin
        File.open('/Users/Duncan/Desktop/Browser_Testing/extension_1_2_4.crx', "rb") do |file|
        chrome_extensions << Base64.encode64(file.read.chomp)
        puts 'over it is...'
        end
    rescue Exception => e
      raise "ERROR: Couldn't File.read or Base64.encode64 a Chrome extension: #{e.message}"
    end
    # Append the extensions to your capabilities hash
      my_capabilities.merge!({'chrome.extensions' => chrome_extensions})
      caps = Selenium::WebDriver::Remote::Capabilities.chrome(my_capabilities)
      Capybara.register_driver :selenium_chrome do |app|
     #Capybara::Selenium::Driver.new(app, :browser => :chrome, :switches => ["--load-extension=/Users/chinmayajb/Desktop/MPM_Testing/extension_1_2_4.crx"])
      Capybara::Selenium::Driver.new(app, :browser => :chrome, :desired_capabilities => caps)
    end
    Capybara.default_driver = :selenium_chrome
end

2 个答案:

答案 0 :(得分:0)

这应该适用于在Chrome浏览器上使用Ruby Capybara加载扩展程序:

caps = Selenium::WebDriver::Remote::Capabilities.chrome({'chromeOptions' 
    => 
    {'extensions' => Base64.strict_encode64(

    File.open('C:\Workspace\RemoteSystemsTempFiles\extension.crx','rb').read
    )]}})
    Capybara.register_driver :selenium_chrome do |app|
        Capybara::Selenium::Driver.new(app, :browser => :chrome, 
        :desired_capabilities => caps)
    end
    Capybara.default_driver = :selenium_chrome

答案 1 :(得分:0)

从.crx文件添加扩展名非常容易。您不需要使用File.open

尝试通过options添加它,就像这样:

  Capybara.register_driver :selenium_chrome do |app|
    options = Selenium::WebDriver::Chrome::Options.new
    options.add_extension(Rails.root.join('spec', 'chrome_extensions', 'react-devtools.crx'))
    options.add_extension(Rails.root.join('spec', 'chrome_extensions', 'redux-devtools.crx'))

    chrome_options = {
      browser: :chrome,
      options: options
    }

    Capybara::Selenium::Driver.new(app, chrome_options)
  end

(来自https://medium.com/@ccschmitz/upgrade-your-js-debugging-in-capybara-w-browser-extensions-1b34fce4d229的示例)