REST API Automation using request npm, Protractor, Jasmine, and TypeScript

0

Functional automation is done when you want to be sure everything on UI is working fine and there are no regression issues. But there is a need to test backend too so that you are sure the request is handled by the server. There are some great tools available to achieve this and I have selected ‘request’ npm so that it can easily work with my functional automation framework. Even if you are targeting the only backend this tutorial will help you achieve that.

Functional automation is done when you want to be sure everything on UI is working fine and there are no regression issues. But there is a need to test backend too so that you are sure the request is handled by the server. There are some great tools available to achieve this and I have selected ‘request’ npm so that it can easily work with my functional automation framework. Even if you are targeting the only backend this tutorial will help you in achieving that.

We will be using request npm which will make http calls. It supports HTTPS and follows redirects by default.

Let’s go through step by step process of creating API automation framework using request npmProtractorJasmine, and TypeScript. At the end of this tutorial, you will be having knowledge of setting up the framework from scratch. Follow below steps one by one, and if you know any step you can skip it and move to next.

Install Node.JS

Install Visual Studio Code

Open Visual Studio Code

Create a folder named api-testing-protractor-typescript

Create a file named .gitignore and add below content

node_modules/
temp/
test-results/
downloads/*
log/*

Create a file named tsconfig.json and add below content

{
	"compilerOptions": {
		"target": "es5",
		"lib": [
			"es5",
			"es6",
			"dom"
		],
		"module": "commonjs",
		"moduleResolution": "node",
		"sourceMap": false,
		"inlineSourceMap": true,
		"declaration": false,
		"removeComments": false,
		"noImplicitAny": false,
		"outDir": "temp",
		"types": [
			"jasmine",
			"node"
		]
	},
	"exclude": [
		"node_modules"
	]
}

Create a file named tslint.json and add below content

{
    "rulesDirectory": [
        "node_modules/codelyzer"
    ],
    "rules": {
        "arrow-return-shorthand": true,
        "callable-types": true,
        "class-name": true,
        "comment-format": [
            true,
            "check-space"
        ],
        "curly": true,
        "eofline": true,
        "forin": true,
        "import-blacklist": [
            true,
            "rxjs"
        ],
        "import-spacing": true,
        "indent": [
            true,
            "spaces"
        ],
        "interface-over-type-literal": true,
        "label-position": true,
        "max-line-length": [
            true,
            140
        ],
        "member-access": false,
        "member-ordering": [
            true,
            {
                "order": [
                    "static-field",
                    "instance-field",
                    "static-method",
                    "instance-method"
                ]
            }
        ],
        "no-arg": true,
        "no-bitwise": true,
        "no-console": [
            true,
            "debug",
            "info",
            "time",
            "timeEnd",
            "trace"
        ],
        "no-construct": true,
        "no-debugger": true,
        "no-duplicate-super": true,
        "no-empty-interface": true,
        "no-eval": true,
        "no-inferrable-types": [
            true,
            "ignore-params"
        ],
        "no-misused-new": true,
        "no-non-null-assertion": true,
        "no-shadowed-variable": true,
        "no-string-literal": false,
        "no-string-throw": true,
        "no-switch-case-fall-through": true,
        "no-trailing-whitespace": true,
        "no-unnecessary-initializer": true,
        "no-unused-expression": true,
        "no-use-before-declare": true,
        "no-var-keyword": true,
        "object-literal-sort-keys": false,
        "one-line": [
            true,
            "check-open-brace",
            "check-catch",
            "check-else",
            "check-whitespace"
        ],
        "prefer-const": true,
        "quotemark": [
            true,
            "single"
        ],
        "radix": true,
        "semicolon": [
            true,
            "always"
        ],
        "triple-equals": [
            true,
            "allow-null-check"
        ],
        "typedef-whitespace": [
            true,
            {
                "call-signature": "nospace",
                "index-signature": "nospace",
                "parameter": "nospace",
                "property-declaration": "nospace",
                "variable-declaration": "nospace"
            }
        ],
        "typeof-compare": true,
        "unified-signatures": true,
        "variable-name": false,
        "whitespace": [
            true,
            "check-branch",
            "check-decl",
            "check-operator",
            "check-separator",
            "check-type"
        ],
        "directive-selector": [
            true,
            "attribute",
            "app",
            "camelCase"
        ],
        "component-selector": [
            true,
            "element",
            "app",
            "kebab-case"
        ],
        "use-input-property-decorator": true,
        "use-output-property-decorator": true,
        "use-host-property-decorator": true,
        "no-input-rename": true,
        "no-output-rename": true,
        "use-life-cycle-interface": true,
        "use-pipe-transform-interface": true,
        "component-class-suffix": true,
        "directive-class-suffix": true,
        "invoke-injectable": true,
        "no-consecutive-blank-lines": true,
        "no-empty": [
            true,
            "allow-empty-catch"
        ],
        "no-unnecessary-class": [
            "allow-constructor-only"
        ],
        "align": [
            true,
            "parameters",
            "statements"
        ]
    }
}

Create a file named package.json and add below content

{
  "name": "api-testing-protractor-typescript",
  "version": "1.0.0",
  "description": "This API Test Automation framework is designed using Request node module, Protractor, Jasmine and TypeScript",
  "homepage": "https://github.com/codewithmmak/api-testing-protractor-typescript",
  "main": "conf.js",
  "dependencies": {
    "jasmine": "^3.3.1",
    "protractor": "^5.4.1",
    "protractor-beautiful-reporter": "^1.2.7",
    "request": "^2.88.0",
    "rimraf": "^2.6.2",
    "typescript": "^3.2.2"
  },
  "devDependencies": {
    "@types/jasmine": "^3.3.4",
    "codelyzer": "^4.5.0",
    "tslint": "^5.12.0"
  },
  "scripts": {
    "tsc": "tsc",
    "clean": "rimraf temp/ && rimraf download/",
    "pretest": "npm run clean && npm run tsc",
    "test": "protractor temp/conf.js",
    "webdriver-update": "node node_modules/protractor/bin/webdriver-manager update"
  },
  "keywords": [
    "request",
    "protractor",
    "typescript",
    "javascript",
    "angular",
    "angularjs",
    "vscode",
    "testing",
    "api testing"
  ],
  "author": "Code with MMAK",
  "license": "MIT",
  "repository": {
    "type": "git",
    "url": "https://github.com/codewithmmak/api-testing-protractor-typescript.git"
  }
}

Open Terminal and go to your Project root directory which is api-testing-protractor-typescript and install dependency by running the command.

npm install --save request protractor jasmine typescript protractor-beautiful-reporter rimraf 

Ignore any warning you see.

Now install dev dependency run command

­npm install --save-dev @types/jasmine tslint codelyzer 

­ Ignore any warning you see.

CODE REPOSITORY

The sample framework is hosted on GitHub: api-testing-protractor-typescript

Have a suggestion or found a bug? Fork this project to help make this even better.

Star the repo and follow me to get latest updates

WHAT DO YOU THINK?

Did this work for you?

Could I have done something better?

Have I missed something?

Please share your thoughts using comments on this post. Also, let me know if there are particular things that you would enjoy reading further. 

Cheers!!!

Leave a Reply

Your email address will not be published. Required fields are marked *