Click here to Skip to main content
14,382,515 members

Rest API/Web API Automated test Using Frisby

Rate this:
4.25 (3 votes)
Please Sign up or sign in to vote.
4.25 (3 votes)
12 Sep 2016CPOL
Rest API/Web API Automated test Using Frisby

Introduction

I have not seen many tutorials on Web Api test using Frisby , most of the article available on the web starts with coding and writing your frisby tests. But here I has written this tutorial for explaining configuartion of the environment ,creating sample WebAPI using Visula Studio 2012 and writing frisby Tests.

Background

Frisby is a REST API testing framework built on node.js and Jasmine that makes
testing API endpoints easy, fast, and fun.

  1. Environement Setup
  2. Create Sample WebAPI for Test
  3. Create Frisby Tests for Sample Web API

Environment Setup

Install below

  1. NodeJs (do not install in default folder i.e. c:\Program File\, install folder like c:\NodeJs (Refer to https://nodejs.org/en/)

    Image 1

  2. Jasmine-Node (Refer to https://github.com/mhevery/jasmine-node)

    Image 2

  3. Frisby (Refer to http://frisbyjs.com/)

    Image 3

Create Sample WebAPI Service

Create a web API project using Visual Studio or download the attachment as zip and run the web API.

UserController

using FrsibyTest.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace FrsibyTest.Controllers
{
    [Route("api/User")]
    public class UserController : ApiController
    {
        List<userviewmodel> dataUserViewModel = new List<userviewmodel>
        {
            new UserViewModel{ Id=1, FirstName ="FirstName1" , LastName="LastName1"},
            new UserViewModel{ Id=2, FirstName ="FirstName2" , LastName="LastName2"},
            new UserViewModel{ Id=3, FirstName ="FirstName3" , LastName="LastName3"},            
        };

        // GET api/user
        public IEnumerable<userviewmodel> Get()
        {
            return dataUserViewModel.ToList();
        }

        // GET api/user/5
        public UserViewModel Get(int id)
        {
            return  dataUserViewModel.Where(x=>x.Id.Equals(id)).First();
        }

        // POST api/user
        [HttpPost]
        public UserViewModel Post(UserViewModel userViewModel)
        {
            return dataUserViewModel.First();
        }

        // PUT api/user/5
         [HttpPut]
        public UserViewModel Put(int id, UserViewModel userViewModel)
        {
          var data =  dataUserViewModel.Where(x => x.Id.Equals(id)).First();
          data.FirstName = "Name-Updated" ;
          return data;
        }

        // DELETE api/user/5
         [HttpDelete]
        public string Delete(int id)
        {
            return "Deleted successfully";
        }
    }
}

View Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace FrsibyTest.Models
{
    public class UserViewModel
    {
        public int Id { get; set; }

        public string FirstName { get; set; }

        public string LastName { get; set; }      

    }
}    

Verify Your Service is up and running before frisby tests.

Image 4

Frisby Test

Write the Firsby Test in user_spec.js file:

    //used for JSON type Verification 
var expectedGETMethodTypejson = [{
    "Id": 0,
    "FirstName": "string",
    "LastName": "string"
}];

//Used for single record JSOn type verification
var exepectJSONTypeForSingleRecord = {
    "Id": 0,
    "FirstName": "string",
    "LastName": "string"
};

//Used for Data verifcation
var expectedJson = [
  {
      "Id": 1,
      "FirstName": "FirstName1",
      "LastName": "LastName1"
  },
  {
      "Id": 2,
      "FirstName": "FirstName2",
      "LastName": "LastName2"
  },
  {
      "Id": 3,
      "FirstName": "FirstName3",
      "LastName": "LastName3"
  }
];

//Created frisby object
var frisby = require('frisby');

// GlobalSetup is for ALL requests
frisby.globalSetup({
    request: {
        headers: { 'Accept': 'application/json' },
        timeout: (30 * 1000)
    }
});

// Check service resouce is up and running
frisby.create('Ensure user service is up and running')
  .get('http://localhost:1623/api/User')
  .expectStatus(200)
.toss();

//Ensure response has a proper JSON Content-Type header
frisby.create('Ensure response has a proper JSON Content-Type header')
  .get('http://localhost:1623/api/User')
  .expectHeaderContains('Content-Type', 'application/json')
.toss();

//Ensure response has proper JSON types in specified keys
frisby.create('Ensure response has proper JSON types in specified keys')
  .get('http://localhost:1623/api/User')
  .expectJSONTypes(expectedGETMethodTypejson)
.toss()

//Ensure response has proper JSON data
frisby.create('Ensure response has proper JSON data')
  .get('http://localhost:1623/api/User')
    .expectJSONLength(3)
  .expectJSON(expectedJson)
.toss()

// POST -Ensure response has proper JSON types in specified keys
frisby.create('Ensure response has proper JSON types in specified keys')
  .post('http://localhost:1623/api/User', {
      "Id": 8,
      "FirstName": "FirstName8",
      "LastName": "LastName8"
  })
  .expectStatus(200)
  .expectJSONTypes(exepectJSONTypeForSingleRecord)
    .expectJSONLength(3)
.toss()

// PUT -Ensure response has proper JSON types in specified keys
frisby.create('Ensure response has proper JSON types in specified keys for update')
  .put('http://localhost:1623/api/User/1', {
      "Id": 1,
      "FirstName": "Name-Updated",
      "LastName": "LastName1"
  })
  .expectStatus(200)
  .expectJSONTypes(exepectJSONTypeForSingleRecord)
    .expectJSONLength(3)
.toss()

// DELETE -Ensure response has proper JSON types in specified keys
frisby.create('Ensure response has proper JSON types in specified keys for update')
  .delete('http://localhost:1623/api/User/1')
  .expectStatus(200)
.toss()    

Save the above file in the folder:

C:\NODEJS\node_modules\frisby\examples

Run the Service and Then Run the Test As Below

C:\NODEJS>jasmine-node \node_modules\frisby\examples\user_spec.js

Image 5

Create Report of Test Result using Junitreport

Image 6

Points of Interest

REST API/Web API Automation test using frisby JavaScript.

History

  • 12th September, 2016: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kunwar Bahadur Singh
Technical Lead
India India
MCPD, MCAD- Having 12+ years of experience on MVC , WEB API,NodeJs, AngularJS, ASP.Net, C#, JQuery, VB.Net, ADO.NET ,WCF , Web Services, REST API , AJAX , MOQ, NUnit, RHINO MOCK,JSON, JSONP ,Window Service , LINQ to SQL, JavaScript, JQGrid, DataTable.js and Entity Framework 4.0.

Comments and Discussions

 
GeneralMy vote of 4 Pin
Ian Klek13-Sep-16 23:24
memberIan Klek13-Sep-16 23:24 
Questioninteresting Pin
Member 1048548712-Sep-16 12:07
memberMember 1048548712-Sep-16 12:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Tip/Trick
Posted 12 Sep 2016

Stats

16.6K views
6 bookmarked