2015-01-07 39 views
0

我有一個簡單的網絡應用程序(發音指南),它顯示了一個鏈接的術語列表。當用戶點擊一個時,它會觸發音頻播放器播放發音。使用PhantomJS點擊觸發的日誌GET請求

因此,有一個點擊事件會觸發GET請求來獲取播放器加載並播放的音頻文件。

我想記錄所有的GET請求,看看是否都成功。我正在嘗試使用PhantomJS來做到這一點。我拼湊在一起:

var page = require('webpage').create(), 
    system = require('system'), 
    address = "http://d-college.cengage.com/demos/pronuncation_guide/index.html" 


    page.onResourceRequested = function (req) { 
     console.log('requested: ' + JSON.stringify(req, undefined, 4)); 
    }; 

    page.onResourceReceived = function (res) { 
     console.log('received: ' + JSON.stringify(res, undefined, 4)); 
    }; 

    page.open(address, function (status) { 
     if (status !== 'success') { 
      console.log('FAIL to load the address'); 
     } 

     page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() { 
     page.evaluate(function() { 
       $("a").click(); 
      }); 
     phantom.exit() 
    }); 
    }); 

這確實成功記錄頁面加載的所有資產,以及包含的jquery。但後來我得到:

Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL file://clicklog.js. Domains, protocols and ports must match. 

我覺得這是不是真的本身錯誤(請參見:CasperJS and 'Unsafe JavaScript attempt to access frame with URL' error)我也不認爲它導致程序BARF。

但是,點擊是否發生?爲什麼不記錄最終的GET請求?

回答

1

那些打印不安全...行是在1.9.8中引入的錯誤,只發生在phantom.exit()。它不會干擾劇本的其餘部分。

您可能沒有看到這些請求,因爲您過早退出。我的理解是,不安全...線路在您退出時仍執行某些操作時會打印出來。當您點擊鏈接並立即退出時,這適合您的情況。你應該讓頁面至少通過延遲exitsetTimeout發送請求:

page.evaluate(...); 
setTimeout(function(){ 
    phantom.exit(); 
}, 1000);