2013-05-31 79 views
29

我已經設置了一個腳本來創建我們的應用程序的網頁截圖。 它完美地運行,直到我遇到一個像一個破碎的URL所有的罰款:phantomJS網頁超時

"<img src='http://testserver.our.intranet/fetch/image/373e8fd2339696e2feeb680b765d626e' />" 

我已成功地打破了腳本中使用低於6秒後,它只是循環永遠之前。

但是,是否有可能忽略網絡請求(AKA取出來的圖像的DOM),然後進行無圖像創建拇指,(或注入的圖像丟失的形象!)

var page = require('webpage').create(), 
    system = require('system'), 
    address, output, size; 

if (system.args.length < 3 || system.args.length > 5) { 
    phantom.exit(1); 
} else { 
    address = system.args[1]; 
    output = system.args[2]; 
    page.viewportSize = { width: 640, height: 640 }; 
    page.zoomFactor = 0.75; 
    page.clipRect = { top: 10, left: 0, width: 640, height: 490 }; 
    try{ 
     page.open(address, function (status) { 
      if (status !== 'success') { 
       console.log('Unable to load the address!'); 
       phantom.exit(); 
      } else { 
       window.setTimeout(function() { 
        page.render(output); 
        phantom.exit(); 
       }, 200); 
      } 
     });  
    } finally{ 
     setTimeout(function() { 
      console.log("Max execution time " + Math.round(6000) + " seconds exceeded"); 
      phantom.exit(1); 
     }, 6000); 
    } 
} 
+0

這是一個很好的問題。我們有一個類似的問題,除了有點難以解決。我們有一個客戶的網站,它的自定義JavaScript寫得不好。這似乎是導致webkit掛起。我們有大量想要測試的網站,但如果客戶的自定義js正在破壞webkit,則它不起作用。計時器OBJ是一個很好的解決方案,但我想知道是否有人有差異解決方案。 ...? – cliffbarnes

回答

60

PhantomJS 1.9推出了一個新的設置,resourceTimeout,它控制了請求在被取消前可以持續多久。除此之外,還有一個onResourceTimeout事件在請求超時時觸發。

這裏的說明上述所有的代碼片段:

var page = require('webpage').create(); 
page.settings.resourceTimeout = 5000; // 5 seconds 
page.onResourceTimeout = function(e) { 
    console.log(e.errorCode); // it'll probably be 408 
    console.log(e.errorString); // it'll probably be 'Network timeout on resource' 
    console.log(e.url);   // the url whose request timed out 
    phantom.exit(1); 
}; 

page.open('http://...', function (status) { 
... 
} 

不幸的是這些選項的不良記錄現在。我必須通過GitHub discussionsPhantomJS source code才能找到它們。

+2

resourceTimeout應該終止被調用的phantomjs進程。因爲對我而言,事實並非如此。 phantomjs流程只能無限期地掛起。 – Donato

+0

@Donato我認爲resourceTimeout只產生一些事件,可以在'page.onResourceTimeout'中處理 – Scadge

+1

至少在版本1.9.8中resourceTimeout中斷了進程 - 所以PDF被破壞,但進程沒有掛起。我還通過使用page.onResourceTimeout = function(request)來記錄哪個資源導致了問題... – Gerfried