PhantomJS在使用polergeist通过capybara设置时没有运行javascript

时间:2014-03-19 00:05:57

标签: capybara phantomjs poltergeist

我设置水豚驱动程序的方式似乎导致phantomjs无法执行javascript。为了确认这可以在capybara / poltergeist之外工作,我使用了原始的watir-webdriver和相同的phantomjs安装,它确实运行了javascript。

所以这是我的代码,使用poltergeist创建了capybara驱动程序:

require 'capybara'
require 'capybara/poltergeist'

include Capybara::DSL

Capybara.register_driver :poltergeist do |app|
  options = {
    :js_errors => true,
    :timeout => 120,
    :debug => false,
    :phantomjs_options => ['--load-images=no', '--disk-cache=false'],
    :inspector => true,
  }
  Capybara::Poltergeist::Driver.new(app, options)
end

Capybara.default_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
Capybara.app_host = 'https://google.com'

visit('/')
fill_in('q', with: 'green cheese')
sleep 2

p all('span').map{|s| s.text}

当我跑步时,我出去了:

["", "", "+You", "", "Search", "", "Images", "", "Maps", "", "Play", "", "YouTube", "", "News", "", "Gmail", "", "More", "More", "", "", "", "Sign in", "Sign in", "", "", "", "", "× A faster way to browse the web Install Google Chrome", "", "", "", "", "Advertising ProgramsBusiness Solutions+GoogleAbout Google © 2013 - Privacy & Terms"]

这表明搜索建议没有显示,这意味着javascript没有运行。我已经使用屏幕截图确认了这一点。

现在,当我运行以下只使用water-webdriver的代码时,它可以工作:

require 'watir-webdriver'

b = Watir::Browser.new :phantomjs

b.goto 'https://google.com'
b.text_field(:name, 'q').set "green cheese"
sleep 2
p b.spans.map{|s| s.text }
b.close

并提供以下输出:

["", "", "+You", "", "Search", "", "Images", "", "Maps", "", "Play", "", "YouTube", "", "News", "", "Gmail", "", "More", "More", "", "", "", "Sign in", "Sign in", "", "", "", "", "×\nA faster way to browse the web\nInstall Google Chrome", "", "", "", "", "Advertising ProgramsBusiness Solutions+GoogleAbout Google\n© 2013 - Privacy & Terms", "green cheese", "green cheese strain", "green cheese weed", "green cheese enchiladas", "green cheesecake", "green cheese moon", "green cheese enchiladas recipe", "green cheese strain review", "green cheese media group", "green cheese penny", "", "", "", ""]

那里的所有绿色奶酪都清楚地显示了搜索建议,这意味着javascript必须正在运行。我再次使用屏幕截图确认了这一点。

任何人都可以帮助我了解如何配置Capybara / poltergeist以使其有效吗?

最后一点说明:这与Rails无关。这是一个独立的测试套件,你可以在这里找到完整的代码:https://github.com/rschultheis/rspec_capybara_starter

1 个答案:

答案 0 :(得分:0)

我能够通过将我的phantomjs从1.9.8升级到2.1.1来解决这个问题。有关执行此操作的说明,请参阅此http://phantomjs.org/download.html