我設置水豚驅動程序的方式似乎是導致phantomjs不執行JavaScript。爲了證實這可以在水豚/ poltergeist之外工作,我使用了與同樣的phantomjs安裝的raw watir-webdriver,並且它運行了javascript。PhantomJS沒有運行JavaScript時通過水豚安裝與poltergeist
因此,這裏是我的代碼創建使用騷靈的水豚司機:
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沒有運行。我已經使用屏幕截圖確認了這一點。
現在,當我運行只使用水的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必須運行。再次,我已經使用屏幕截圖確認了這一點。
任何人都可以幫助我理解如何配置水豚/ poltergeist所以它的作品?
最後一點:這與Rails無關。這是一個獨立的測試套件,你可以在這裏找到完整的代碼:https://github.com/rschultheis/rspec_capybara_starter
我意識到它已經有一段時間了;蕾你有沒有找到這個決議?我恨不得不放棄poltergeist –
Nevermind,我找到了我的解決方案,並在下面添加了一個答案。 –