前端自动化测试工具

PhantomjsCasperJS

大名鼎鼎的PhantomJS当然要隆重介绍啦!前面界面对比测试基本都是基于PhantomJS开发的, Phantom JS是一个服务器端的 JavaScript API 的 WebKit。其支持各种Web标准: DOM 处理, CSS 选择器, JSON, Canvas, 和 SVG。对于web测试、界面、网络捕获、页面自动化访问等等方面可以说是信手拈来。

casperjs是对PhantomJS的封装,提供了更加易用的API, 增强了测试等方面的支持。例如通过CasperJS可以轻松实现贴吧的自动发帖功能:

casper.test.begin('测试发帖功能', function suite(test) {   
    //登录百度
    casper.loginBaidu();//实现略,可以通过cookie或者表单登录实现
    casper.thenOpen('http://tieba.baidu.com/p/3817915520', function () {  
        var text = "楼主好人";
        //等待发帖框出现
        this.waitForSelector(
            '#ueditor_replace', 
            function() {
                //开始发帖
                this.echo("开始发帖。发帖内容: " + text,"INFO");
                //执行js
                this.page.evaluate(function(text) {
                    $("#ueditor_replace").text(text);
                    $("a.poster_submit").click();//点击提交
                },text);
            },function(){
                test.fail("找不到发帖框#ueditor_replace");
            }
        );
    })
    .run(function () {
        test.done();
    });
});

  

具体可参考文章:

http://fex.baidu.com/blog/2015/07/front-end-test/

猜你喜欢

转载自jm1999.iteye.com/blog/2301650