CasperJS 选择器
发布在前端自动化测试之路2015年3月8日view:5233
在文章任何区域双击击即可给文章添加【评注】!浮到评注点上可以查看详情。

预备知识:

CasperJS使用了大量的选择器,方便你操作DOM,以及透明的使用CSS3或者XPath表达式。

后面的例子都基于下面的HTML结构:

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>My page</title>
</head>
<body>
    <h1 class="page-title">Hello</h1>
    <ul>
        <li>one</li>
        <li>two</li>
        <li>three</li>
    </ul>
    <footer><p>©2012 myself</p></footer>
</body>
</html>

CSS3

默认情况下,CasperJS接受CSS3选择器字符串来检索DOM元素。

要检索元素<h1 class="page-title">是否存在,你可以用下面的方式:

var casper = require('casper').create();

casper.start('http://domain.tld/page.html', function() {
    if (this.exists('h1.page-title')) {
        this.echo('the heading exists');
    }
});

casper.run();

或者使用测试框架:

casper.test.begin('The heading exists', 1, function suite(test) {
    casper.start('http://domain.tld/page.html', function() {
        test.assertExists('h1.page-title');
    }).run(function() {
        test.done();
    });
});

有一些方便的测试方法也依赖于选择器:

casper.test.begin('Page content tests', 3, function suite(test) {
    casper.start('http://domain.tld/page.html', function() {
        test.assertExists('h1.page-title');
        test.assertSelectorHasText('h1.page-title', 'Hello');
        test.assertVisible('footer');
    }).run(function() {
        test.done();
    });
});

XPath

你还可以使用XPath表达式:

casper.start('http://domain.tld/page.html', function() {
    this.test.assertExists({
        type: 'xpath',
        path: '//*[@class="plop"]'
    }, 'the element exists');
});

为了方便使用和阅读XPath表达式,casper模块提供了一个辅助方法selectXPath:

var x = require('casper').selectXPath;

casper.start('http://domain.tld/page.html', function() {
    this.test.assertExists(x('//*[@id="plop"]'), 'the element exists');
});

警告:在CasperJS里使用XPath唯一的限制就是当你想用casper.fill()方法填充file fields时;PhantomJS原生upload方法只允许使用CSS3选择器。

评论
发表评论
暂无评论
WRITTEN BY
HtmlCssJs
Keep your sanity in this crazy frontend world
TA的新浪微博
PUBLISHED IN
前端自动化测试之路

探索前端自动化测试相关技术

友情链接 大搜车前端团队博客
我的收藏