Frisby is a rest API automation test framework. It allows us to write automated tests using Node.js and Jasmine js to test the integrity of Rest APIs.
- Frisby is a node.js package so it must be installed on your machine. It can be downloaded from the following location: https://nodejs.org/en/download/
- Now to install frisby.js from command line using npm (node package manager), use the following command:
npm install -g frisby
Note: It will install frisby at following location: “C:\Users\Abhishek\AppData\Roaming\npm\node_modules\frisby”. This location will be required in 1st line of frisby test spec.
- Frisby also requires jasmine. Install it using the following command: npm install -g jasmine-node
How to write a frisby test:
- On 1st line of your spec you have to import frisby.js
Note: Please update the Frisby path to match the installation location on your machine.
- Set the following headers as shown below:
- Now, we’ll start with our first test case. Using the create method we will set the test description/name. This will be printed in the logs/report when this test will pass or fail.
- Then you have to specify your request type. Each frisby test must be associated with a request type like (get, post, put, delete, or head).
Note: You can also pass the parameter in your post request using following code:
- Then if you want you can set the timeout.
- Then if you want you can print your response.
- Then you have to specify assertions.
- Then you have to end test using toss() method.
Some built-in assertion/verification test helpers provided by Frisby:
- expectStatus: This method is used to verify expected HTTP status code of response from API.
.expectStatus(200), .expectStatus(500) etc.
- expectJSONTypes: This method is used to verify expected JSON key type of API.
- expectJSON: Through this you can verify expected JSON.
error: “Username or password not provided.”,
- expectHeaderContains: Through this you can verify expected header of API.
- expectBodyContains: Through this you can verify expected body of API.
.expectBodyContains(“Username or password not provided.”)
Some of the most commonly used built-in inspectors:
- inspectHeaders(): Print response header in console.
- inspectJSON(): Dumps parsed JSON body in console.
- inspectBody(): Dumps the raw response body without any parsing.
You can also set timeout of your API response by following method:
Naming convention of Frisby test file/spec:
Frisby uses jasmine so name of test/spec file must end with keyword ‘spec’ and file extension should be ‘.js’.
e.g. test1spec.js, test2_spec.js etc.
Run Frisby Tests:
Frisby is built on top of Jasmine BDD framework. It uses jasmine-node to run the tests.
Syntax– jasmine-node ‘js filename with spec keyword’
Example– jasmine-node test1spec.js
Generate Junit report:
The jasmine-node test runner has an option that generates test run reports in JUnit XML format. You can achieve it by following syntax.
Syntax– jasmine-node ‘js filename with spec keyword’ –junitreport
Example– jasmine-node test1spec.js –junitreport
How to run multiple test as a suite:
You can run multiple frisby test as a suite using Jasmine functions describe() and it().
Test suite should start with Jasmine function ‘describe’ with two parameters: a string and a function. Here string is a name or title for a spec suite. The function is a block of code that implements the suite.
Spec/test case are defined by calling jasmine function ‘it’ with two parameters: a string and a function. The string is the title of the spec and the function is the spec, or test.
Sample Test Suite code:
Hope this blog post will help you in your Frisby framework. Happy Testing!