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 | Note that this example uses TypeScript 2.0.
|
6 |
|
7 | ## Examples
|
8 |
|
9 | There are two examples in this directory:
|
10 |
|
11 | * Simple Protractor example
|
12 | * Similar to the [github protractor example](https://github.com/angular/protractor/tree/master/example)
|
13 | * Files `conf.ts` and `spec.ts`
|
14 | * Page objects example
|
15 | * Follows the [protractortest.org page objects example](http://www.protractortest.org/#/page-objects)
|
16 | * Files `confPageObjects.ts`, `specPageObjects.ts`, and `angularPage.ts`
|
17 |
|
18 | ## File organization
|
19 |
|
20 | ```
|
21 | exampleTypescript/
|
22 | |- node_modules/ // downloaded node modules
|
23 | |- tmp/ // compiled javascript output
|
24 | |
|
25 | |- .gitignore // since typescript produces javascript, we should not
|
26 | | // commit javascript to the repo
|
27 | |- angularPage.ts // page object example
|
28 | |- confPageObjects.ts // configuration for the page objects example
|
29 | |- package.json // node dependencies for the project
|
30 | |- README.md // this file
|
31 | |- spec.ts // spec for the simple protractor example
|
32 | |- specPageObjects.ts // spec for the page objects example
|
33 | |- tsconfig.json // typescript transpile configuration
|
34 | ```
|
35 |
|
36 |
|
37 | ## Getting started
|
38 |
|
39 | This package.json references the local protractor directory with `"protractor": "file: ../"`. For the type declarations to work, from the protractor directory run an `npm install` to generate the declarations file.
|
40 |
|
41 | Next, install the exampleTypescript node_modules with:
|
42 |
|
43 | ```
|
44 | npm install
|
45 | ```
|
46 |
|
47 |
|
48 | ## Protractor typings
|
49 |
|
50 | To use Protractor types, you'll need to import `protractor`. After this is imported, you should have autocompletion hints when typing.
|
51 |
|
52 | ```
|
53 | import {browser, element, by, By, $, $$, ExpectedConditions} from 'protractor';
|
54 | ```
|
55 |
|
56 | 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:
|
57 |
|
58 | ```
|
59 | import {Config} from 'protractor';
|
60 |
|
61 | export let config: Config = {
|
62 | ...
|
63 | }
|
64 | ```
|
65 |
|
66 | ## Ambient typings
|
67 |
|
68 | Protractor also uses ambient types including jasmine and node. These are brought in via the `tsconfig.json` file, which uses npm module resolution to get types from `node_modules/@types`.
|
69 |
|
70 |
|
71 | ## Compiling your code
|
72 |
|
73 | To convert your typescript to javascript (transpiling), you'll use the Typescript compiler (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`.
|
74 |
|
75 | ## Running Protractor
|
76 |
|
77 | After transpiling your code to javascript, you'll run Protractor like before: `protractor conf.js`
|
78 |
|
79 | ## Helpful links
|
80 |
|
81 | * [TypescriptLang.org tutorial](http://www.typescriptlang.org/docs/tutorial.html)
|
82 | * [TypescriptLang.org tsconfig.json](http://www.typescriptlang.org/docs/handbook/tsconfig-json.html)
|
83 | * [Typescript gitter](https://gitter.im/Microsoft/TypeScript)
|
84 | * [Typescript stackoverflow](http://stackoverflow.com/questions/tagged/typescript)
|