• [译文]Casperjs1.1.0参考文档-快速开始


    快速开始

    只要casperjs被正确安装,你就可以开始写你的第一个脚本,你可以使用javascript或者coffiescript编译脚本。

    提示:

    如果你对javascript不是很熟悉,最好先看专用的FAQ条目

    一个简单的scriping脚本:

    打开你熟悉的编辑器,写下下面一段代码,并保存为sample.js

    var casper = require('casper').create();
    casper.start('http://casperjs.org/', function() {
        this.echo(this.getTitle());
    });

    casper.thenOpen('http://phantomjs.org', function() {
        this.echo(this.getTitle());
    });

    casper.run();
    运行它:
    $ casperjs sample.js
     
    你将得到如下结果:
    $ casperjs sample.js
    CasperJS, a navigation scripting and testing utility for PhantomJS
    PhantomJS: Headless WebKit with JavaScript API
     
    我们刚才做了什么?
     
    1、我们创建了一个新的casperjs实例
    2、我们运行它并且打开了'http://phantomjs.org'
    3、当页面被加载后,我们打印出了网页标题
    4、然后我们打开了另一个URL,'http://phantomjs.org'
    5、当页面被加载后,我们同样打印出了网页标题
    6、我们执行了整个进程
     
    现在我们测试一下google的网页:
     
    在以下了例子中,我们在google搜索框查询‘casperjs’和’phantomjs’,将得到的结果数组输出到控制台。
     
    打开你熟悉的编辑器,用javascript写下以下代码,并保存为googlelink.js
    var links = [];
    var casper = require('casper').create();
    
    function getLinks() {
        var links = document.querySelectorAll('h3.r a');
        return Array.prototype.map.call(links, function(e) {
            return e.getAttribute('href');
        });
    }
    
    casper.start('http://google.fr/', function() {
        // search for 'casperjs' from google form
        this.fill('form[action="/search"]', { q: 'casperjs' }, true);
    });
    
    casper.then(function() {
        // aggregate results for the 'casperjs' search
        links = this.evaluate(getLinks);
        // now search for 'phantomjs' by filling the form again
        this.fill('form[action="/search"]', { q: 'phantomjs' }, true);
    });
    
    casper.then(function() {
        // aggregate results for the 'phantomjs' search
        links = links.concat(this.evaluate(getLinks));
    });
    
    casper.run(function() {
        // echo results in some pretty fashion
        this.echo(links.length + ' links found:');
        this.echo(' - ' + links.join('
     - ')).exit();
    });
     
    结果如下:
    $ casperjs googlelinks.js
    20 links found:
     - https://github.com/n1k0/casperjs
     - https://github.com/n1k0/casperjs/issues/2
     - https://github.com/n1k0/casperjs/tree/master/samples
     - https://github.com/n1k0/casperjs/commits/master/
     - http://www.facebook.com/people/Casper-Js/100000337260665
     - http://www.facebook.com/public/Casper-Js
     - http://hashtags.org/tag/CasperJS/
     - http://www.zerotohundred.com/newforums/members/casper-js.html
     - http://www.yellowpages.com/casper-wy/j-s-enterprises
     - http://local.trib.com/casper+wy/j+s+chinese+restaurant.zq.html
     - http://www.phantomjs.org/
     - http://code.google.com/p/phantomjs/
     - http://code.google.com/p/phantomjs/wiki/QuickStart
     - http://svay.com/blog/index/post/2011/08/31/Paris-JS-10-%3A-Introduction-%C3%A0-PhantomJS
     - https://github.com/ariya/phantomjs
     - http://dailyjs.com/2011/01/28/phantoms/
     - http://css.dzone.com/articles/phantom-js-alternative
     - http://pilvee.com/blog/tag/phantom-js/
     - http://ariya.blogspot.com/2011/01/phantomjs-minimalistic-headless-webkit.html
     - http://www.readwriteweb.com/hack/2011/03/phantomjs-the-power-of-webkit.php
     
    coffiescript版本的实例:
    你也可以使用coffiescript晚上上面那个例子
    getLinks = ->
      links = document.querySelectorAll "h3.r a"
      Array::map.call links, (e) -> e.getAttribute "href"
    
    links = []
    casper = require('casper').create()
    
    casper.start "http://google.fr/", ->
      # search for 'casperjs' from google form
      @fill "form[action='/search']", q: "casperjs", true
    
    casper.then ->
      # aggregate results for the 'casperjs' search
      links = @evaluate getLinks
      # search for 'phantomjs' from google form
      @fill "form[action='/search']", q: "phantomjs", true
    
    casper.then ->
      # concat results for the 'phantomjs' search
      links = links.concat @evaluate(getLinks)
    
    casper.run ->
      # display results
      @echo links.length + " links found:"
      @echo(" - " + links.join("
     - ")).exit()
    唯一的不同是文件的后缀为.coffie
     
    一个简单的测试脚本:
    casperjs也是一个测试框架,测试脚本有一些不同,它有更多独有的API.
    一个简单的测试脚本:
    // hello-test.js
    casper.test.begin("Hello, Test!", 1, function(test) {
      test.assert(true);
      test.done();
    });
     
    使用casperjs test命令来运行他:
    $ casperjs test hello-test.js
    Test file: hello-test.js
    # Hello, Test!
    PASS Subject is strictly true
    PASS 1 test executed in 0.023s, 1 passed, 0 failed, 0 dubious, 0 skipped.

    注意:

    如同你上面所看到的,你不能创建一个casper取代测试脚本中预配置的那一个,

    关于测试模块,在它专有的章节你可以知道更多。

  • 相关阅读:
    浅析深度优先和广度优先遍历实现过程、区别及使用场景
    浅析为什么要用setTimeout模拟setInterval
    app弹出软键盘获取键盘高度不准确的原因及导致底部定位的元素无法贴近键盘的问题
    App平台iOS设备上因内存不足导致白屏、闪退的原因及其解决方案
    浅谈移动端开发技术
    浅析Console命令调试常用方法
    js正则表达式中的正向肯定预查和正向否定预查, 反向肯定和反向否定(这个翻译不准确)
    javascript的版本查看及js的历史
    【转】JS-正则表达式的反向引用
    【转】Linux虚拟网络设备之tun/tap
  • 原文地址:https://www.cnblogs.com/reach296/p/3768199.html
Copyright © 2020-2023  润新知