Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write Protractor test scripts using Typescript along with Jasmine framework in Visual studio Code?

Cannot find name DescribeMy project is going from standalone to Web, Our new WebSite is getting created in AngularJS so Protractor is the tool selected for Test Automation.

I want to Integrate Typescript with dependencies of Jasmine and Node so that I don't get errors such as

cannot find name Describe
cannot find name it
cannot find name Expect

Can Anyone tell me how to add Jasmine and Protractor dependencies, so that when I hit ctrl + space i'll get all options available.

I have installed Typescript. And I am getting protractor dependencies such as browser, element, by etc.

What should i do for describe,it,expect (Jasmine stuffs) ?

like image 384
Kishan Patel Avatar asked Sep 06 '16 11:09

Kishan Patel


2 Answers

I use Visual Studio Code everyday to write my scripts, it's my current favourite editor for Protractor because of its built in support for TypeScript!

Here are the following things which can come into my mind which could help you setup your framework-

  • Download the latest VS Code version - https://code.visualstudio.com/download
  • Install typescript globally npm install -g typescript
  • Install protractor globally npm install -g protractor
  • Create your project folder
  • Setup you project folder for git, node and typescript -

    npm init -f // will create default package.json stating its nodejs project
    git init  // will create .git file, you project is now git project
    tsc --init  // will create tsconfig.json stating its typescript project
    
  • Install typings and dev dependencies-

    npm install --save-dev protractor // this will install protractor as a dev dependency
    npm install --save-dev typescript // this will install typescript as a dev dependency
    npm install --save-dev @types/jasmine // jasmine typings
    npm install --save-dev @types/node    // node typings
    
  • At this point you have setup your basic protractor-typescript project and you can see all the typings and dependencies in package.json. Now you are good to write your typed scripts :).
  • Now compile your scripts by running -

    tsc or tsc -w 
    
  • After successfull compilation all your javascript files would be generated.
  • The run protractor

    protractor config.js
    
  • You can also setup your vs code for debugging with protractor which I have mentioned here - Protractor -VS Code Debugging

For more details pls refer the TypeScript Tutorial, Protractor API

The Typescript error you are observing this is due to VS Code not recognizing global typescript 2.0 version.

To solve this open vscode go to preferences--> user settings --> settings.json will be opened and enter the highlighted path as shown

enter image description here Save your file and restart VSCode now you are good to go :)

like image 97
Ram Pasala Avatar answered Oct 31 '22 00:10

Ram Pasala


I agree with the answers given. Just want to share a hack with you.

You don't need to transpile your Typescript codes to JavaScript anymore.

Create a launch.js file

require('ts-node').register({
  compilerOptions: {
    module: 'commonjs'
  },
  disableWarnings: true,
  fast: true
});
exports.config = require('./config/protractor.conf.ts').config;

And kick start protractor execution like:

> protractor launch 

You can save yourself from the headache of transpiling every time you make a change to typescript files.

Happy testng!

like image 35
Abhinaba Avatar answered Oct 31 '22 00:10

Abhinaba