Reading list Switch to dark mode

    Get The Http Status Using Casperjs

    Updated 13 April 2018

    In testing we do have lots of urls to test. To check the http status of those urls it will be easy to put all the urls in an array and you can check the http responses of all the urls by opening them one by one. For this, best practice is to use an array with a loop which will minimize your code writing and execution time as well.

    You may check the blog Casperjs : How To Save All Href And Title In An Array to save data in an array.

    Here, I have used a predefined set of urls. To check that I have used a loop and opened urls one by one and checked the http status of all the urls.

    /**
    * Webkul Software.
    *
    * @category Webkul
    * @package Webkul_CasperJS
    * @author Webkul
    * @copyright Copyright (c) 2010-2018 Webkul Software Private Limited (https://webkul.com)
    * @license https://store.webkul.com/license.html
    */
    
    var casper = require("casper").create();
    var http = require('http');
    var fs = require('fs');
    var j = 0;
    var links = ['https://store.webkul.com/', 'https://store.webkul.com/ggg'];
    
    casper.start();
    
    for (i = 0; i < 2; i++) {
    	casper.thenOpen(links[j], function (response) {
    		if (response == undefined || response.status >= 400) {
    			this.echo(response.status + ': found at ' + response.url, 'ERROR');
    		} else {
    			this.echo(response.status + ': found at ' + response.url, 'GREEN_BAR');
    		}
    		this.on('http.status.404', function (resource) {
    			this.echo('404 found at : ' + resource.url, 'WARNING');
    		});
    	});
    	j++;
    }
    
    casper.run(function () {
    	this.exit();
    });
    

    Now you have to run the command- casperjs your_filename.js

    You may check the output on the terminal as shown below –

    Start your headless eCommerce
    now.
    Find out More

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home