Frisby is a REST API testing framework built on node.js and Jasmine that makes testing API endpoints easy, fast, and fun. Its as simple as tossing a frisby. Lets quickly setup Frisby on a Linux system.
Install NodeJS and Jasmine node:
curl --silent --location https://rpm.nodesource.com/setup_6.x | sudo bash -
yum install -y nodejs
npm install -g jasmine-node
Install the required modules:
cd /usr/lib/node_modules/jasmine-node
#Form-Data for uploading files using REST API
npm install --save form-data
#For sending and receiving WEB requests
npm install --save request
#Express is a minimal and flexible Node.js web application framework that
#provides a robust set of features for web and mobile applications.
npm install --save express
#For reading and validate the raw body of a readable stream
npm install --save raw-body
#Our core framework for playing with REST APIs
npm install --save-dev frisby
#Used for sequencing REST calls or rather for sending synchronous
#REST calls to the server
npm install --save sequenty
#Used to create a readable report from all the generated XML junit reports
npm install junit-viewer -g
Done. Frisby is ready to be tossed.
Lets quickly write a simple frisby test case and toss it.
//Load the frisby module
var frisby = require('frisby');
//Define our URL for making a REST request
var URL =
'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="setup frisby on linux"';
// globalSetup is for ALL requests
frisby.globalSetup({
request: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
timeout: (60 * 1000)
});
frisby.create("GET_Google_Search") // Create a frisby and name it
.get(URL) // Make a GET http request
.afterJSON(function(json) { // Response converted JSON
// Post processing, after the request
})
.expectStatus(200) // Request success criteria
.inspectBody() // Print the response to console
.toss(); // Just throw the frisby
Project Setup:
Copy the above code into a new file and name it helloFrisby_spec.js
Since firsby.js is using jasmine, so the name of the test case file must end with spec.js
You need to create a link to the modules that were installed using npm.
This step is required only once and make sure that all your spec files are stored in that directory for execution.
mkdir -p /cubicrace/jasmine/rester
cd /cubicrace/jasmine/rester
npm link express form-data raw-body frisby request
cd /cubicrace/jasmine/rester
npm link express form-data raw-body frisby request
Setup Directory Structure:
[cubicrace@textbox rester]$ ls -al
-rw-rw-r-- 1 cubicrace cubicrace 907 Apr 21 20:47 helloFrisby_spec.js
drwxr-xr-x 9 cubicrace cubicrace 4096 Apr 7 13:06 node_modules
-rw-rw-r-- 1 cubicrace cubicrace 907 Apr 21 20:47 helloFrisby_spec.js
drwxr-xr-x 9 cubicrace cubicrace 4096 Apr 7 13:06 node_modules
Execution:
[cubicrace@testbox rester]$ which jasmine-node
/usr/bin/jasmine-node
[cubicrace@testbox rester]$ jasmine-node helloFrisby_spec.js
{"responseData": {"results":[{"GsearchResultClass":"GwebSearch","unescapedUrl":"http://www.cubicrace.com/","url":"http://www.cubicrace.com/","visibleUrl":"www.cubicrace.com","cacheUrl":"http://www.google.com/search?q\u003dcache:KW0LoXuB_rUJ:www.cubicrace.com","title":"cubicrace","titleNoFormatting":"cubicrace","content":"How to \u003cb\u003eSetup Frisby on Linux\u003c/b\u003e. Apr 21, 2016. Short video about IBM past, present \nand future. Welcome to the cognitive era! This video reminds us as to why IBM ..."}],"cursor":{"resultCount":"1","pages":[{"start":"0","label":1}],"estimatedResultCount":"1","currentPageIndex":0,"moreResultsUrl":"http://www.google.com/search?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0\u0026hl\u003den\u0026q\u003d%22setup+frisby+on+linux%22","searchResultTime":"0.13"}}, "responseDetails": null, "responseStatus": 200}
.
Finished in 0.992 seconds
1 test, 1 assertion, 0 failures, 0 skipped
The JSON data in the output is a result of the .inspectBody() statement in the above code./usr/bin/jasmine-node
[cubicrace@testbox rester]$ jasmine-node helloFrisby_spec.js
{"responseData": {"results":[{"GsearchResultClass":"GwebSearch","unescapedUrl":"http://www.cubicrace.com/","url":"http://www.cubicrace.com/","visibleUrl":"www.cubicrace.com","cacheUrl":"http://www.google.com/search?q\u003dcache:KW0LoXuB_rUJ:www.cubicrace.com","title":"cubicrace","titleNoFormatting":"cubicrace","content":"How to \u003cb\u003eSetup Frisby on Linux\u003c/b\u003e. Apr 21, 2016. Short video about IBM past, present \nand future. Welcome to the cognitive era! This video reminds us as to why IBM ..."}],"cursor":{"resultCount":"1","pages":[{"start":"0","label":1}],"estimatedResultCount":"1","currentPageIndex":0,"moreResultsUrl":"http://www.google.com/search?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0\u0026hl\u003den\u0026q\u003d%22setup+frisby+on+linux%22","searchResultTime":"0.13"}}, "responseDetails": null, "responseStatus": 200}
.
Finished in 0.992 seconds
1 test, 1 assertion, 0 failures, 0 skipped
1 test and 1 assertion, is because of the .expectStatus(200) statement in the above code.
If while executing the frisby script, you get an error like "Cannot find module '<module_name>'", then follow the below resolution step:
npm install --save module_name
For example, if the error is: { [Error: Cannot find module '/root/.npm/form-data'] code: 'MODULE_NOT_FOUND' } then you can resolve this issue by executing the command:
npm install --save form-data.
Link to my stackoverflow resolution steps: MODULE_NOT_FOUND
For example, if the error is: { [Error: Cannot find module '/root/.npm/form-data'] code: 'MODULE_NOT_FOUND' } then you can resolve this issue by executing the command:
npm install --save form-data.
0 comments:
Post a Comment