How can I verify that a webpage returns 404/500 using PhantomJS?

I am new to PhantomJS and Javascript, and I am working on a script that test load time, and I would like it to detect if a 404/500 error was encountered during testing and display and message in console.log. The code is as follows:

var page = require('webpage').create(), t, address; t = Date.now(); var testArray = ['someURL']; function loadTest(testURL) { address = testURL; page.open(address, function (status) { if (status !== 'success') { console.log('FAIL to load the address' + address); return; } }); } for(var i = 0; i < testArray.length; i++) { loadTest(testArray[i]); t = Date.now() - t; console.log('Testing ' + testArray[i]); console.log('Loading time ' + t + ' msec\n'); } phantom.exit(); 

Help is much appreciated. Thanks

+6
source share
3 answers

You might want to look at the onResourceReceived on the page object, you can get what you need from it. ( API docs ... )

This is a slightly contrived example, and it will return a status code for each resource received as part of the request, but the page itself will be the first (i.e., unlike JS or CSS support, etc.):

 var page = require('webpage').create(); page.onResourceReceived = function(res) { if (res.stage === 'end') { console.log('Status code: ' + res.status); } }; page.open('http://some.url/that/does-not-exist', function() { phantom.exit(); }); 

Of course, this assumes that the server will actually return you 404 (unlike 200 masquerades, for example, 404), but something in this direction should give you what you want.

+8
source

I do not believe that PhantomJS supports returning HTTP response codes at this time. For this to be possible, the WebPage object would have to open the QNetworkReply object and get its HTTP response code.

The HTTP response code can be obtained as follows in C ++ code:

 int statusCode = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); 

As long as this is not integrated into the PhantomJS source, I think you just need to check for success or failure. If you really need to see the HTTP response code, you can run the CURL script to check the response from the server. I suspect that β€œsuccess” or β€œfailure” will be good for your goals.

0
source

complementing @founddrama's answer, if you have a stylized 404 page and load some assets, onResourceReceived will show the status of all these assets, so I would recommend changing your code to something like

 var definedStatus = false; page.onResourceReceived = function(res) { if (res.stage === 'end' && definedStatus === false) { definedStatus = res.status; } }; page.open(url, function(status) { if (status == 'success' && definedStatus == 200) { // do something phantom.exit(); } else { console.log("Erro") phantom.exit(1); } }); 

therefore, you will only have the status of the page you requested.

0
source

Source: https://habr.com/ru/post/915428/


All Articles