casperjs检查标题是否存在并具有任何值,因此测试不为空 [英] casperjs check that title exists and has any value so test not empty

查看:82
本文介绍了casperjs检查标题是否存在并具有任何值,因此测试不为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个简单的问题,但是我是casperjs的新手。

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 ="".

我认为在casperjs 1.1测试套件中这很简单

I thought this would be simple in casperjs 1.1 test suite

网站:

<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"> 

测试:

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();
    });
});


推荐答案

您可以使用<$ c运行XUnit类型测试$ c> casper test script.js 命令。请记住, test 命令使用内部Casper实例,因此您无需创建一个实例。

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();
    });
});

这篇关于casperjs检查标题是否存在并具有任何值,因此测试不为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆