我如何在heroku上使用firefox

时间:2016-08-26 13:52:37

标签: ruby-on-rails firefox heroku watir buildpack

我正在使用watir webdriver和它的无头功能以及firefox浏览器转到一个网站,比如www.xyz.com。点击不同的按钮并下载pdf。我已经在我的本地环境中实现了这一点。当我推动我的应用程序到heroku,它要求buildpacks.I已经添加了buildpacks并且它们存在于我的heroku中。我通过运行heroku run bash找到了它。 使用的依赖关系是:

gem 'watir-webdriver', '~> 0.9.1'
gem 'headless', '~> 2.2', '>= 2.2.3'
Buildpack of Xvfb
Buildpack of firefox 

Xvfb buildpack运行正常。运行browser = Watir::Browser.new(:firefox, :profile => profile)时,我收到奇怪的错误,例如Selenium::WebDriver::Error::WebDriverError: unable to obtain stable firefox connection in 60 seconds (127.0.0.1:7055)set path for firefox。我为firefox Selenium::WebDriver::Firefox::Binary.path='vendor/firefox/firefox-bin'设置了路径 。我可以在heroku bash中看到firefox安装在该位置。 我不确定这是否是一个heroku问题或buildpack问题。尽管buildpack的开发人员说他无法在heroku上运行他的firefox buildpack但是他能够在Amazon EC2上部署。是可能的在heroku上安装和使用firefox及其所有功能(如打开和关闭浏览器,下载pdf,打开标签)?如果我对问题不太清楚,我很抱歉。 Firefox Buildpack

begin
  ActiveRecord::Base.transaction do
    download_directory = "#{Rails.root}/tmp/#{dir_name}"

    Selenium::WebDriver::Firefox::Binary.path='vendor/firefox/firefox-bin'
    profile = Selenium::WebDriver::Firefox::Profile.new
    profile['download.prompt_for_download'] = false
    profile['browser.download.folderList'] = 2 # custom location
    profile['browser.download.dir'] = download_directory
    profile['browser.helperApps.neverAsk.saveToDisk'] = "application/pdf"
    # Disable built-in pdf viewer of Firefox browser
    profile['pdfjs.disabled'] = true
    profile['pdfjs.firstRun'] = false

    headless = Headless.new
    headless.start

    browser = Watir::Browser.new(:firefox, :profile => profile)
    # browser.screenshot.save "pp.png"
    browser.goto 'xyz.com'
    browser.window.resize_to(some_x,some_y)
    browser.text_field(:name => "some_name").set("#{some_data}")
    browser.text_field(:name => "some_password").set("#{password}")
    browser.button(:name => "button").click
    #Pdf gets downloaded in the defined location
    #some database updations
    headless.destroy
  end
rescue => r
end

1 个答案:

答案 0 :(得分:1)

你不能在heroku上使用Firefox作为无头浏览器。 Heroku不支持此功能。您可以使用Phantom js之类的JavaScript浏览器。

您可以在heroku上为pantom js配置buildpack。 您需要进行一些代码更改,如:

Selenium::WebDriver::PhantomJS.path = 'path_to_phantomjs'
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 180 # seconds – default is 60
args = %w{--ignore-ssl-errors=true}
browser = Watir::Browser.new :phantomjs, :http_client => client, :args => args