|
| 1 | +# Protractor with Typescript |
| 2 | + |
| 3 | +Typescript provides code auto completion and helpful hints with a text editor like Microsoft's Visual Studio Code or another text editor with Typescript support. |
| 4 | + |
| 5 | +## Examples |
| 6 | + |
| 7 | +There are two examples in this directory: |
| 8 | + |
| 9 | +* Simple Protractor example |
| 10 | + * Similar to the [github protractor example](https://github.com/angular/protractor/tree/master/example) |
| 11 | + * Files `conf.ts` and `spec.ts` |
| 12 | +* Page objects example |
| 13 | + * Follows the [protractortest.org page objects example](http://www.protractortest.org/#/page-objects) |
| 14 | + * Files `confPageObjects.ts`, `specPageObjects.ts`, and `angularPage.ts` |
| 15 | + |
| 16 | +## File organization |
| 17 | + |
| 18 | +``` |
| 19 | +exampleTypescript/ |
| 20 | +|- node_modules/ // downloaded node modules |
| 21 | +|- typings/ // downloaded ambient types |
| 22 | +| |- index.d.ts // generated typings file |
| 23 | +|- .gitignore // since typescript produces javascript, we should not |
| 24 | +| // commit javascript to the repo |
| 25 | +|- angularPage.ts // page object example |
| 26 | +|- confPageObjects.ts // configuration for the page objects example |
| 27 | +|- package.json // node dependencies for the project |
| 28 | +|- readme.md // this file |
| 29 | +|- spec.ts // spec for the simple protractor example |
| 30 | +|- specPageObjects.ts // spec for the page objects example |
| 31 | +|- tsconfig.json // typescript transpile configuration |
| 32 | +|- typings.json // ambient typing imports (jasmine, node, etc) |
| 33 | +``` |
| 34 | + |
| 35 | + |
| 36 | +## Getting started |
| 37 | + |
| 38 | +Install the node_modules and ambient typings: |
| 39 | + |
| 40 | +``` |
| 41 | +npm install |
| 42 | +``` |
| 43 | + |
| 44 | +The ambient typings are downloaded from DefinitelyTyped in the `postinstall` step. The files that are downloaded are listed in the `typings.json` file. |
| 45 | + |
| 46 | + |
| 47 | +## Protractor typings |
| 48 | + |
| 49 | +To use Protractor types, you'll need to import `protractor/globals`. After this is imported, you should have auto completition hints when typing. |
| 50 | + |
| 51 | +``` |
| 52 | +import {browser, element, by, By, $, $$, ExpectedConditions} from 'protractor/globals'; |
| 53 | +``` |
| 54 | + |
| 55 | +Although the Protractor configuration file can be written in javascript, creating it in typescript will have some hints. These hints and the reference configuration can be found in `lib/config.ts`. Below we are importing the Config interface and applying that interface to the config variable: |
| 56 | + |
| 57 | +``` |
| 58 | +import {Config} from 'protractor'; |
| 59 | +
|
| 60 | +export let config: Config = { |
| 61 | + ... |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +## Ambient typings |
| 66 | + |
| 67 | +Protractor also uses ambient types including jasmine and node. These are brought in via the `typings.json` file. The ambient typings files are imported from the `typings/index.d.ts` generated file and are included in the project via the tsconfig.json configuration file. |
| 68 | + |
| 69 | + |
| 70 | +## Compiling your code |
| 71 | + |
| 72 | +To convert your typescript to javascript (transpiling), you'll use the Typescript comipler (tsc). If you install typescript globally, the command is `tsc`. If it is not installed globally, the typescript compiler can be executed with `npm run tsc`. |
| 73 | + |
| 74 | +## Running Protractor |
| 75 | + |
| 76 | +After transpiling your code to javascript, you'll run Protractor like before: `protractor conf.js` |
| 77 | + |
| 78 | +## Helpful links |
| 79 | + |
| 80 | +* [TypescriptLang.org tutorial](http://www.typescriptlang.org/docs/tutorial.html) |
| 81 | +* [TypescriptLang.org tsconfig.json](http://www.typescriptlang.org/docs/handbook/tsconfig-json.html) |
| 82 | +* [Typescript gitter](https://gitter.im/Microsoft/TypeScript) |
| 83 | +* [Typescript stackoverflow](http://stackoverflow.com/questions/tagged/typescript) |
| 84 | + |
0 commit comments