frisby用例动态链

frisby是一个nodejs的rest api测试框架。一般来讲,因为nodejs是众所周知的异步编程模式,api以.after()方法一步步走:

frisby.create('login')
  .get('http://httpbin.org/login',{usernmae:xxx,password:xxx})
  after(function(err, res, body) {
    frisby.create('logout')
      .get('http://httpbin.org/logout')
    .toss()
  });
.toss()

在一些较为复杂的测试流程中,需要根据上一部的返回动态决定下一步需要做什么。比如:

frisby.create('login')
  .get('http://httpbin.org/login',{usernmae:xxx,password:xxx})
  afterJSON(function(rsp) {
    if(rsp.ok){
       frisby.create('logout')
         .get('http://httpbin.org/logout')
       .toss()
    }else{
       frisby.create('login again')
         .get('http://httpbin.org/login',{username:xxx,password:xxxx})
       .toss()
    }
  });
.toss()

上面的例子,如果第二个frisby toss的时候还有更多的after操作,代码会非常难组织。为此写了一个简单的pipeLine函数用于将多个frisby串起来。

function toss(f, msg){
    return function(){
        //console.log('toss ' + msg);
        f.toss();
    }
}

//frisby.after() just add a function to array and invoke them when .toss()
function pipeLine(works){
    for(var i = 0; i < works.length - 1; i++){
        works[i].after(toss(works[i+1], ''+(i+1)));
    }
    return works[0];
}

var testsPipline = function() {
    var f1 = frisby.create('test 1')
        .get('http://192.168.5.120:8080/static/3ef9f60c/images/title.png');
    var f2 = frisby.create('test 2')
        .get('http://192.168.5.120:8080/static/3ef9f60c/images/title.png');
    var f3 = frisby.create('test 3')
        .get('http://192.168.5.120:8080/static/3ef9f60c/images/title.png');
    toss(pipeLine([f1, f2, f3]), 0)();
    toss(pipeLine([f2,f3]), 0)();
}

可以很方便的重用预定义的frisby测试过程。

原文地址:https://www.cnblogs.com/jan4984/p/4500753.html