casperjs check that title exists and has any value so test not empty

穿精又带淫゛_ 提交于 2019-12-25 02:38:51

问题


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 have been populated with something. I don't want a specific value just test they exist and not empty e.g. not ="".

I thought this would be simple in casperjs 1.1 test suite

Site:

<head>
    <title>UnoSpark</title>
    <link rel="stylesheet" href="stylesheets/foundation.css">

    <meta name="description" content="Free Dating WebSiteWeb">
    <meta name="keywords"    content="dating, love">
    <meta name="author"      content="Paul">
</head>
<body>
<div class="sharedWidget" data-widget="sharedWidget" data-widget-id="CMSHEADER"> 

Tests:

casper.test.begin('title test',2, function(test)
{
    var url = "http://www.unospark.com/";
    casper.start(url).then(function()
    {
    this.test.assert(this.getTitle()!='');
    this.test.assertDoesntExist('meta:empty');

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

回答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()
        {
            return document.querySelector('meta[name="description"]').content;
        });

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

        author = this.evaluate(function()
        {
            return document.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();
    });
});



回答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.js
var 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.js
var 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();
    });
});


来源:https://stackoverflow.com/questions/23372959/casperjs-check-that-title-exists-and-has-any-value-so-test-not-empty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!