2013-07-12 40 views
0

我想使用phantomjs(版本1.9.1,在Windows上)訪問一些QUnit測試網址。我在公司網站的代理背後,但我試圖訪問的URL是從我的本地開發工作站提供的,另外我甚至嘗試在沒有必要的情況下使用其他兩個瀏覽器(Hv3和Dooble)訪問相同的URL代理設置,即使他們無法執行QUnit JavaScript,他們也會獲得HTML響應。爲什麼我沒有收到phantomjs頁面響應?

所以我甚至試着調整javascriptEnabled設置(加上另外幾個設置,請參閱下面的代碼)爲false,試圖獲取原始HTML,但無濟於事。我在try/catch中將我的調用封裝到了page.open中,但顯然這不是因爲異常;而是在最終的phantom.exit()語句執行之前立即使用console.log語句。

此外,我遵循https://github.com/ariya/phantomjs/wiki/Network-Monitoring的建議,包括從page.onResourceRequested,page.onError和page.onResourceReceived進行日誌記錄,並且只有onResourceReceived的回調被執行。而我指定--proxy-type = none命令行參數,都無濟於事。

代碼和輸出如下,在此先感謝。我無所適從;也許這是一個幻影問題?只是想在報告之前排除所有事情。

CODE:

var page = require('webpage').create(); 

page.onResourceRequested = function (request) { 
    console.log('Request ' + JSON.stringify(request, undefined, 4)); 
}; 

page.onResourceReceived = function (response) { 
    console.log('Receive ' + JSON.stringify(response, undefined, 4)); 
}; 

page.onError = function (msg, trace) { 
    console.log(msg); 
    trace.forEach(function(item) { 
     console.log(' ', item.file, ':', item.line); 
    }) 
} 

page.settings.webSecurityEnabled = false; 
page.settings.localToRemoteUrlAccessEnabled = true; 
//page.settings.javascriptEnabled = false; 

for (var setting in page.settings) { 
    console.log(setting + ": " + page.settings[setting]); 
} 

try { 
    page.open('http://local.example.com:9001/test/workflow', function() { 
     console.log('page opened'); 
    }); 
} 
catch(xcep) { 
    console.log(xcep); 
} 

console.log('before exit'); 
phantom.exit(); 

OUTPUT:

XSSAuditingEnabled: false 
javascriptCanCloseWindows: true 
javascriptCanOpenWindows: true 
javascriptEnabled: true 
loadImages: true 
localToRemoteUrlAccessEnabled: true 
userAgent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.1 Safari/534.34 
webSecurityEnabled: false 
Request { 
    "headers": [ 
     { 
      "name": "User-Agent", 
      "value": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.34 (KHTML, like Gecko) PhantomJS/1.9.1 Safari/534.34" 
     }, 
     { 
      "name": "Accept", 
      "value": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" 
     } 
    ], 
    "id": 1, 
    "method": "GET", 
    "time": "2013-07-12T09:49:58.262Z", 
    "url": "http://local.example.com:9001/test/workflow" 
} 
before exit 

回答

4

page.open()是異步的。因此,phantom.exit()的行將在頁面加載之前執行,並停止PhantomJS進程。將phantom.exit()移回page.open()的回調中,並將其放置在回調結束時(即處理代碼之後)。所以基本上你都會有這樣的:

page.open('http://local.example.com:9001/test/workflow', function() { 
     console.log('page opened'); 

     phantom.exit(); 
}); 
+0

感謝,做課程的伎倆。我的想法是我不想在與page.open回調相同的塊內使用phantom.exit(),因爲我實際上想要連續打開幾個頁面。我想我可以想辦法做到這一點,使用關閉和減少櫃檯。 –

+1

@GeorgeJempty它對異步操作有點棘手,但是,是的,你可以做到這一點。即使有多個異步操作,您只需要知道何時達到了停止條件。正如你所建議的那樣,你可以通過多種方式來應對。 –

1

只是想這裏是這樣一個笨蛋,忘記回調page.open挽救自己執行asyncrhonously。正如在我接受的答案的評論中提到的那樣,我很討厭從該回調中調用phantom.exit(),因爲我實際上需要在循環中進行。

那麼這裏有一個關閉的方法,包括使用try/catch來確保phantom.exit()將始終被調用。

注:而不是在頂部宣佈

page = require('webpage').create() 

,我們不是僅僅創造了網頁模塊的引用,然後通過webpage.create()爲在每次循環的閉合。

CODE:

var fs = require('fs'), 
    webpage = require('webpage'); 

var publicJsDir = [fs.workingDirectory, '..', 'public', 'js'].join(fs.separator), 
    testNames = fs.list(publicJsDir).map(function(file){ 
     return (file.match(/^(.*)\.test\.js$/) || [])[1]; 
    }).filter(function(val) {return val}); 

for (var i=testNames.length; i--;) { 
    (function(i, testName, page){ 
     try { 
      console.log(testName); //page.open(... 
      if (!i) phantom.exit(); 
     } 
     catch(xcep) { 
      console.log(xcep); 
      phantom.exit(); 
     } 
    })(i, testNames[i], webpage.create()); 
} 

OUTPUT:

workflow 
workflow.cloning 
utils.trains 
utils.stations 
models.trains.processors 
models.stations 
gis 
fp 
flow 
ds 
相關問題