Skip to content Skip to sidebar Skip to footer

Casperjs Check That Title Exists And Has Any Value So Test Not Empty

This seems like an easy question but I am new to casperjs. I want to check that title exists and that it has a value e.g. not =''. Also same for meta tags that they exist and also

Solution 1:

    casper.test.begin('title test',4, function(test)
{
    var url = "http://www.unospark.com/";
    casper.start(url).then(function()
    {
        test.assert(this.getTitle()!='');
        test.assertExists('meta[name="description"]');
        test.assertExists('meta[name="keywords"]');
        test.assertExists('meta[name="author"]');

        descrip = this.evaluate(function()
        {
            returndocument.querySelector('meta[name="description"]').content;
        });

        keywords = this.evaluate(function()
        {
            returndocument.querySelector('meta[name="keywords"]').content;
        });

        author = this.evaluate(function()
        {
            returndocument.querySelector('meta[name="author"]').content;
        });

        console.log("Meta " + descrip);
        console.log("Meta " + keywords);
        console.log("Meta " + author);

        test.assertNotEquals(descrip, "", "Meta description not empty");
        test.assertNotEquals(keywords,"", "Meta keywords not empty");
        test.assertNotEquals(author,  "", "Meta author not empty");


    }).run(function()
    {
        test.done();
    });
});

Solution 2:

You can run XUnit type tests with the casper test script.js command. Keep in mind that the test command uses an internal casper instance so you don't need to create one.

// script.jsvar x = require('casper').selectXPath;
var url = "http://www.unospark.com/";
casper.test.begin('title test', function(test) {
    casper.start(url).then(function() {
        test.assert(this.getTitle()!=''); // straight forward
    }).run(function() {
        test.done();
    });
});

casper.test.begin('meta test', function(test) {
    casper.start(url).then(function() {
        // The following selector tries to select 'meta' elements which are empty// This is achieved through the :empty CSS pseudo-class
        test.assertDoesntExist('meta[content=""]');
        // or using XPath
        test.assertDoesntExist(x('//meta[not(@content)]'));

        test.assertExists("*[data-widget-id='CMSHEADER']")
        // or as XPath but more general
        test.assertExists("//*[@*='CMSHEADER']")
    }).run(function() {
        test.done();
    });
});

It is probably a good idea to combine some tests into one method for performance reasons.

// script.jsvar url = "http://www.unospark.com/";
casper.test.begin('header tests', function(test) {
    casper.start(url).then(function() {
        test.assert(this.getTitle()!='');
        test.assertDoesntExist('meta[content=""]');
    }).run(function() {
        test.done();
    });
});

Post a Comment for "Casperjs Check That Title Exists And Has Any Value So Test Not Empty"