2013-11-26 72 views
4

我有一系列使用Capybara,Poltergeist和PhantomJS與網頁進行交互的Ruby自動化測試。但是,問題是一些頁面無法正常工作,因爲jQuery似乎沒有被加載。例如,在一個網頁上有這樣的代碼:如何讓jQuery與PhantomJS,Poltergeist和水豚一起工作

<script type="text/javascript"> 
<!-- 
    jQuery(window).load(function() { 
    //Do Javascript-y things 
    }); 
//--> 
</script> 

但是這似乎不工作/運行。從我讀過的文章看來,PhantomJS並不適用於jQuery(或者至少它不會自動加載jQuery)。我怎樣才能解決這個問題。請記住,我無法更改網頁的實際代碼。

摘要:如何在使用PhantomJS,Poltergeist和Capybara時添加/注入/加載jQuery?

編輯:這是我如何使用phantomjs。注意我從來沒有直接使用phantomjs。 Phantomjs可執行文件被Poltergeist間接使用:

Capybara.register_driver :poltergeist do |app| 
    driver = Capybara::Poltergeist::Driver.new(app, 
             :debug => debug, 
             :window_size => [1616, 1000], 
             :js_errors => true, 
         :cookies => true, 
    :phantomjs_options => ['--ignore-ssl-errors=true', 
       '--web-security=false', 
       '--local-to-remote-url-access=true', 
       '--proxy-type=none' ],:timeout => 180 
     )  
+0

我從來沒有加載jQuery的應用程序的這個問題。你在使用Rails嗎?代碼/錯誤是什麼樣的? – CDub

+0

不使用導軌。該腳本(即網頁中的)只是不運行。我在上面包含了我的示例 – Nosrettap

+0

您需要在正在測試/報廢或正在做的任何頁面的上下文中注入jQuery。以下是PhantomjS如何操作的鏈接:http://snippets.aktagon.com/snippets/534-how-to-scrape-web-pages-with-phantomjs-and-jquery – Nobita

回答

相關問題