About Frisby:
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.
Setup:
- 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
var myFrisby = require('C:/Users/Abhishek/AppData/Roaming/npm/node_modules/frisby/lib/frisby');
Note: Please update the Frisby path to match the installation location on your machine.
- Set the following headers as shown below:
myFrisby.globalSetup({ request: { headers: { 'X-Ms-Source':'api','X-Ms-Format':'xml','Authorization':'Basic c2hyZXlhIGdveWFsOm0jbWY4cDlMZ2ZAMU1xUTg='} } });
- 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.
var test1 = myFrisby.create("User should login with valid credentials")
- 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).
test1.get("http://your-api-url/login?username=<some username>&password=<some password>")
Note: You can also pass the parameter in your post request using following code:
test1.post(login_url, {email: email_for_login, password: password_for_login})
- Then if you want you can set the timeout.
test1.timeout(10000)
- Then if you want you can print your response.
test1.inspectBody()
- Then you have to specify assertions.
test1.expectStatus(200)
- Then you have to end test using toss() method.
test1.toss();
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.
.expectJSONTypes({
error: String
status: String
}) - expectJSON: Through this you can verify expected JSON.
.expectJSON({
error: “Username or password not provided.”,
status: “error”
}) - expectHeaderContains: Through this you can verify expected header of API.
.expectHeaderContains(‘Content-Type’, ‘text/plain’) - 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.
Timeout:
You can also set timeout of your API response by following method:
.timeout(10000)
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().
describe()
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.
it()
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.
describe("Test Suite Name", function(){ it("Spec Title 1", function(){ frisby.create("Test 1") ---- --- .toss(); }) it("Spec Title 2", function(){ frisby.create("Test 1") ---- --- .toss(); }) })
Sample Test Suite code:
var frisby = require('C:/Users/Abhishek/AppData/Roaming/npm/node_modules/frisby/lib/frisby'); describe("Test Login", function(){ it("Invalid Login", function(){ frisby.create("http://your-api-url/login?username=<some username>&password=<some password>") .get(rec_incorrect_password_login_url) .timeout(10000) .expectHeaderContains('Content-Type', 'text/plain') .expectBodyContains("Password didn't match.") .expectStatus(200) .expectJSONTypes({ error: String, status: String }) .expectJSON({ error: "Password didn't match.", status: "error" }) .toss(); }) it("Valid Login", function(){ frisby.create("Verify Data On Valid Login") .get("http://your-api-url/login?username=<some username>&password=<some password>") .timeout(10000) .inspectHeaders() .inspectBody() .expectHeaderContains('Content-Type', 'text/plain') .expectStatus(200) .expectJSONTypes({ canAccessLive: String, canAccessOneWay: String, canReviewOneWay: String, fullName: String, loginid: String, status: String }) .expectJSON({ canAccessLive: "true", canAccessOneWay: "true", canReviewOneWay: "true", fullName: "Abhishek Verma", loginid: "8301", status: "ok" }) .toss(); }) })
Hope this blog post will help you in your Frisby framework. Happy Testing!