测试自动化_博客已迁移软件测试群集·测试人在路上

Chakram学习笔记 - chakram-expectatio

2017-11-23  本文已影响11人  做测试的DanteYu

这篇文章会通过具体例子介绍chakram-expectation模块的常用验证方法。

状态码验证 status code

使用chakram-expectation.status(code)检查response的状态码

it("should allow checking of the response's status code", function () {
    return chakram.get("https://www.hupu.com/").then(function(response){
      return expect(response).to.have.status(200);
    });
});

响应时间验证 Response times

使用chakram-expectation.responsetime(milliseconds)检查响应时间是小于或等于给出的millisecond value

it("should response time less than or equal to 900", function () {
    return chakram.get("https://api.douban.com/v2/movie/subject/26363254").then(function(response){
      expect(response).to.have.responsetime(900);
    });

JSON内容验证

使用 chakram-expectation.json(subelement, expectedValue) 去检查返回body里JSON对象的内容。默认情况下,检查返回的JSON对象与给出的对象是否完全匹配;如果在chain里加了comprise,那么就允许部分匹配。json()第一个参数subelement是optional的,这个参数的作用是允许sub object的检查。

const chakram = require('../node_modules/chakram/lib/chakram.js'),
    expect = chakram.expect;

const testURL = "https://api.douban.com/v2/movie/subject/26363254";
const expectedValue = {
    "rating": {
        "max": 10,
        "average": 9.5,
        "stars": "50",
        "min": 0
    },
    "reviews_count": 488,
    "wish_count": 66074,
    "douban_site": "",
    "year": "2016",
    "images": {
        "small": "https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2380810795.jpg",
        "large": "https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2380810795.jpg",
        "medium": "https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2380810795.jpg"
    },
    "alt": "https://movie.douban.com/subject/26794215/",
    "id": "26794215",
    "mobile_url": "https://movie.douban.com/subject/26794215/mobile",
    "title": "我们这一天 第一季",
    "do_count": 17496,
    "share_url": "https://m.douban.com/movie/subject/26794215",
    "seasons_count": 3,
    "schedule_url": "",
    "episodes_count": "18",
    "countries": [
        "美国"
    ],
    "genres": [
        "喜剧"
    ],
    "collect_count": 54903,
    "casts": [
        {
            "alt": "https://movie.douban.com/celebrity/1000013/",
            "avatars": {
                "small": "https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p23017.jpg",
                "large": "https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p23017.jpg",
                "medium": "https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p23017.jpg"
            },
            "name": "曼迪·摩尔",
            "id": "1000013"
        },
        {
            "alt": "https://movie.douban.com/celebrity/1009262/",
            "avatars": {
                "small": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1214.jpg",
                "large": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1214.jpg",
                "medium": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1214.jpg"
            },
            "name": "米洛·文堤米利亚",
            "id": "1009262"
        },
        {
            "alt": "https://movie.douban.com/celebrity/1004662/",
            "avatars": {
                "small": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1475766359.42.jpg",
                "large": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1475766359.42.jpg",
                "medium": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1475766359.42.jpg"
            },
            "name": "贾斯汀·哈特雷",
            "id": "1004662"
        },
        {
            "alt": "https://movie.douban.com/celebrity/1363698/",
            "avatars": {
                "small": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1477289499.85.jpg",
                "large": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1477289499.85.jpg",
                "medium": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1477289499.85.jpg"
            },
            "name": "克丽丝·梅斯",
            "id": "1363698"
        }
    ],
    "current_season": "1",
    "original_title": "This Is Us",
    "summary": "讲述一群不同的人,他们其中一些人的共同点就是「同一天生日」,他们的生活故事以有趣的方式交叉在一起。\nMandy Moore扮演Rebecca, Jack可爱的妻子也是最好的朋友。搬到匹兹堡后生了三胞胎。Milo Ventimiglia饰演Jack。\nJustin Har tley 扮演Kevin, 一个英俊且当红的电视演员,有着异常无趣没有终期的单身生活。\nSterling Brown扮演Randall,衣着光鲜亮丽在纽约工作的顾家生意人。与老婆Beth\n合作无间地养育着两个女儿。\nSusan Kelechi Watson 扮演Beth,Randall老婆\nRon Cephas Jones 扮演William, Randall的生父。在Randall一出生的时候就遗弃了他。\nChrissy Metz 扮演Kate, 跟很多女人一样烦恼着饮食和身材. 她用便条贴贴满整个公寓来体型自己要控制饮食。她是Kevin的孪生姐姐,但是各方面跟Kevin完全相反。",
    "subtype": "tv",
    "directors": [
        {
            "alt": "https://movie.douban.com/celebrity/1317350/",
            "avatars": {
                "small": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p46966.jpg",
                "large": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p46966.jpg",
                "medium": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p46966.jpg"
            },
            "name": "约翰·里夸",
            "id": "1317350"
        },
        {
            "alt": "https://movie.douban.com/celebrity/1317347/",
            "avatars": {
                "small": "https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p46967.jpg",
                "large": "https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p46967.jpg",
                "medium": "https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p46967.jpg"
            },
            "name": "格伦·费卡拉",
            "id": "1317347"
        },
        {
            "alt": "https://movie.douban.com/celebrity/1022804/",
            "avatars": {
                "small": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1399135203.4.jpg",
                "large": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1399135203.4.jpg",
                "medium": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1399135203.4.jpg"
            },
            "name": "肯·奥林",
            "id": "1022804"
        },
        {
            "alt": "https://movie.douban.com/celebrity/1005023/",
            "avatars": {
                "small": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p47175.jpg",
                "large": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p47175.jpg",
                "medium": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p47175.jpg"
            },
            "name": "小乔治·提尔曼",
            "id": "1005023"
        },
        {
            "alt": "https://movie.douban.com/celebrity/1301767/",
            "avatars": {
                "small": "https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1375348180.38.jpg",
                "large": "https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1375348180.38.jpg",
                "medium": "https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1375348180.38.jpg"
            },
            "name": "克雷格·兹斯克",
            "id": "1301767"
        },
        {
            "alt": "https://movie.douban.com/celebrity/1302681/",
            "avatars": {
                "small": "https://img1.doubanio.com/f/movie/ca527386eb8c4e325611e22dfcb04cc116d6b423/pics/movie/celebrity-default-small.png",
                "large": "https://img3.doubanio.com/f/movie/63acc16ca6309ef191f0378faf793d1096a3e606/pics/movie/celebrity-default-large.png",
                "medium": "https://img1.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png"
            },
            "name": "西拉斯霍华德",
            "id": "1302681"
        },
        {
            "alt": "https://movie.douban.com/celebrity/1350502/",
            "avatars": {
                "small": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1460537179.25.jpg",
                "large": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1460537179.25.jpg",
                "medium": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1460537179.25.jpg"
            },
            "name": "莎拉·皮亚·安德森",
            "id": "1350502"
        },
        {
            "alt": "https://movie.douban.com/celebrity/1360492/",
            "avatars": {
                "small": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1507708620.86.jpg",
                "large": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1507708620.86.jpg",
                "medium": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1507708620.86.jpg"
            },
            "name": "乌塔·布里兹维茨",
            "id": "1360492"
        },
        {
            "alt": "https://movie.douban.com/celebrity/1019017/",
            "avatars": {
                "small": "https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p39678.jpg",
                "large": "https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p39678.jpg",
                "medium": "https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p39678.jpg"
            },
            "name": "海伦·亨特",
            "id": "1019017"
        },
        {
            "alt": "https://movie.douban.com/celebrity/1019078/",
            "avatars": {
                "small": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p25251.jpg",
                "large": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p25251.jpg",
                "medium": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p25251.jpg"
            },
            "name": "蒂莫西·布斯菲尔德",
            "id": "1019078"
        },
        {
            "alt": "https://movie.douban.com/celebrity/1362730/",
            "avatars": {
                "small": "https://img1.doubanio.com/f/movie/ca527386eb8c4e325611e22dfcb04cc116d6b423/pics/movie/celebrity-default-small.png",
                "large": "https://img3.doubanio.com/f/movie/63acc16ca6309ef191f0378faf793d1096a3e606/pics/movie/celebrity-default-large.png",
                "medium": "https://img1.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png"
            },
            "name": "克里斯·科赫",
            "id": "1362730"
        },
        {
            "alt": "https://movie.douban.com/celebrity/1276696/",
            "avatars": {
                "small": "https://img1.doubanio.com/f/movie/ca527386eb8c4e325611e22dfcb04cc116d6b423/pics/movie/celebrity-default-small.png",
                "large": "https://img3.doubanio.com/f/movie/63acc16ca6309ef191f0378faf793d1096a3e606/pics/movie/celebrity-default-large.png",
                "medium": "https://img1.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png"
            },
            "name": "温蒂·斯坦齐勒",
            "id": "1276696"
        }
    ],
    "comments_count": 21994,
    "ratings_count": 64950,
    "aka": [
        "这就是我们",
        "我们的生活",
        "同一生日下",
        "同一天的我们",
        "我们",
        "我们在一起"
    ]
};

const expectedValue_casts = [
    {
        "alt": "https://movie.douban.com/celebrity/1000013/",
        "avatars": {
            "small": "https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p23017.jpg",
            "large": "https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p23017.jpg",
            "medium": "https://img1.doubanio.com/view/celebrity/s_ratio_celebrity/public/p23017.jpg"
        },
        "name": "曼迪·摩尔",
        "id": "1000013"
    },
    {
        "alt": "https://movie.douban.com/celebrity/1009262/",
        "avatars": {
            "small": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1214.jpg",
            "large": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1214.jpg",
            "medium": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1214.jpg"
        },
        "name": "米洛·文堤米利亚",
        "id": "1009262"
    },
    {
        "alt": "https://movie.douban.com/celebrity/1004662/",
        "avatars": {
            "small": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1475766359.42.jpg",
            "large": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1475766359.42.jpg",
            "medium": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1475766359.42.jpg"
        },
        "name": "贾斯汀·哈特雷",
        "id": "1004662"
    },
    {
        "alt": "https://movie.douban.com/celebrity/1363698/",
        "avatars": {
            "small": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1477289499.85.jpg",
            "large": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1477289499.85.jpg",
            "medium": "https://img3.doubanio.com/view/celebrity/s_ratio_celebrity/public/p1477289499.85.jpg"
        },
        "name": "克丽丝·梅斯",
        "id": "1363698"
    }
];

describe("describe", function(){

  before("Get response", function(){
    response = chakram.get("https://api.douban.com/v2/movie/subject/26794215");
  });

  it("should check the content of JSON ", function () {
      expect(response).to.have.json(expectedValue);
      return chakram.wait();
  });

  it("should be a TV show", function(){
    expect(response).to.have.json('subtype', 'tv');
    return chakram.wait();
  });

  it("should check sub object casts", function(){
    expect(response).to.have.json("casts", expectedValue_casts);
    return chakram.wait();
  });

  it("should check specified object is contained within the body JSON", function(){
    expect(response).to.comprise.of.json({
      "current_season": "1",
      "original_title": "This Is Us",
      "year": "2016"
    });
    return chakram.wait();
  })
})

JSON Schema验证

使用chakram-expectation.schema(subelement, expectedSchema)来检查response JSON的schema。这个验证使用了tv4schema()第一个参数subelement是optional的,这个参数的作用是允许sub object的检查。

const chakram = require('../node_modules/chakram/lib/chakram.js'),
    expect = chakram.expect;

describe("Check JSON Schema", function(){
  it("should check that the returned JSON object satisifies a JSON schema", function () {
    let response = chakram.get("http://httpbin.org/get");
    let expectedSchema = {
        "type": "object",
        properties: {
            url: {
                type: "string"
            },
            headers: {
                type: "object"
            }
        }
    };
    expect(response).to.have.schema('headers', {"required": ["Host", "Accept"]});
    expect(response).to.have.schema(expectedSchema);
    return chakram.wait();
});
})

Cookie验证 Cookie presence and value

使用chakram-expectation.cookie(name, value)检查cookie是否存在以及cookie的值是否匹配。第二个参数value是optional的,检查cookie的值是否匹配期待的字符串或是正则。

const chakram = require('../node_modules/chakram/lib/chakram.js'),
    expect = chakram.expect;

describe("Check cookie", function(){
  it("should allow checking of HTTP cookies", function () {
      var response = chakram.get("http://httpbin.org/cookies/set?chakram=testval");
      expect(response).to.have.cookie('chakram');
      expect(response).to.have.cookie('chakram', 'testval');
      expect(response).to.have.cookie('chakram', /val/);
      return chakram.wait();
  });
})

Header验证 Header presence and value

使用chakram-expectation.header(name, value)来检查header是否存在以及header的值是否匹配。第二个参数value是optional的,检查header的值是否匹配期待的字符串或是正则。

const chakram = require('../node_modules/chakram/lib/chakram.js'),
    expect = chakram.expect;

describe("Check Header", function(){
  it("should allow checking of HTTP headers", function () {
      var response = chakram.get("https://api.douban.com/v2/movie/subject/26794215");
      expect(response).to.have.header('content-type');
      expect(response).to.have.header('content-type', 'application/json; charset=utf-8');
      expect(response).to.have.header('content-type', /json/);
      expect(response).to.have.header('content-type', function(contentType) {
          expect(contentType).to.equal('application/json; charset=utf-8');
      });
      return chakram.wait();
  });
})

HTTP compression

使用chakram-expectation.deflate()chakram-expectation.gzip来检查HTTP数据压缩的方式是deflate还是gzip。

const chakram = require('../node_modules/chakram/lib/chakram.js'),
    expect = chakram.expect;

describe("Check compression", function(){
  it("should detect deflate compression", function () {
          var deflate = chakram.get("http://httpbin.org/deflate");
          return expect(deflate).to.be.encoded.with.deflate;
      });

  it("should detect gzip compression", function () {
            var gzip = chakram.get("http://httpbin.org/gzip");
            return expect(gzip).to.be.encoded.with.gzip;
        });

  it("should support shorter language chains", function () {
            var deflate = chakram.get("http://httpbin.org/deflate");
            expect(deflate).not.to.be.gzip;
            expect(deflate).to.be.deflate;
            return chakram.wait();
        });
})

上一篇下一篇

猜你喜欢

热点阅读