Skip to content
This repository was archived by the owner on Jul 29, 2024. It is now read-only.

Commit f5dc4f9

Browse files
authored
chore(example): add a protractor typescript example (#3323)
1 parent 8b124cf commit f5dc4f9

11 files changed

+229
-1
lines changed

exampleTypescript/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.js

exampleTypescript/README.md

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+

exampleTypescript/angularPage.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Because this file references protractor, you'll need to have it as a project
2+
// dependency to use 'protractor/globals'. Here is the full list of imports:
3+
//
4+
// import {browser, element, by, By, $, $$, ExpectedConditions}
5+
// from 'protractor/globals';
6+
//
7+
import {browser, element, by} from 'protractor/globals';
8+
9+
export class AngularHomepage {
10+
nameInput = element(by.model('yourName'));
11+
greeting = element(by.binding('yourName'));
12+
13+
get() {
14+
browser.get('http://www.angularjs.org');
15+
}
16+
17+
setName(name: string) {
18+
this.nameInput.sendKeys(name);
19+
}
20+
21+
// getGreeting returns a webdriver.promise.Promise.<string>. For simplicity
22+
// setting the return value to any
23+
getGreeting(): any {
24+
return this.greeting.getText();
25+
}
26+
}

exampleTypescript/conf.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Because this file imports from protractor, you'll need to have it as a
2+
// project dependency. Please see the reference config: lib/config.ts for more
3+
// information.
4+
//
5+
// Why you might want to create your config with typescript:
6+
// edtiors like Microsoft Visual Studio Code will have autocomplete and
7+
// description hints.
8+
//
9+
// To run this example, run 'npm run tsc' to transpile the typescript to
10+
// javascript. run with 'protractor conf.js'
11+
import {Config} from 'protractor';
12+
13+
export let config: Config = {
14+
framework: 'jasmine',
15+
capabilities: {
16+
browserName: 'chrome'
17+
},
18+
specs: [ 'spec.js' ],
19+
seleniumAddress: 'http://localhost:4444/wd/hub'
20+
};

exampleTypescript/confPageObjects.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Because this file imports from protractor, you'll need to have it as a
2+
// project dependency. Please see the reference config: lib/config.ts for more
3+
// information.
4+
//
5+
// Why you might want to create your config with typescript:
6+
// edtiors like Microsoft Visual Studio Code will have autocomplete and
7+
// description hints.
8+
//
9+
// To run this example, run 'npm run tsc' to transpile the typescript to
10+
// javascript. run with 'protractor conf_withPageObjects.js'
11+
import {Config} from 'protractor';
12+
13+
export let config: Config = {
14+
framework: 'jasmine',
15+
capabilities: {
16+
browserName: 'chrome'
17+
},
18+
specs: [ 'specPageObjects.js' ],
19+
seleniumAddress: 'http://localhost:4444/wd/hub'
20+
};

exampleTypescript/package.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "example-typescript",
3+
"version": "1.0.0",
4+
"description": "a typescript example",
5+
"author": "",
6+
"license": "MIT",
7+
"scripts": {
8+
"tsc": "tsc",
9+
"typings": "typings install",
10+
"postinstall": "typings install"
11+
},
12+
"dependencies": {
13+
"jasmine": "^2.4.1",
14+
"protractor": "^4.0.0",
15+
"typescript": "^1.8.10",
16+
"typings": "^1.0.5"
17+
}
18+
}

exampleTypescript/spec.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Because this file references protractor, you'll need to have it as a project
2+
// dependency to use 'protractor/globals'. Here is the full list of imports:
3+
//
4+
// import {browser, element, by, By, $, $$, ExpectedConditions}
5+
// from 'protractor/globals';
6+
//
7+
// The jasmine typings are brought in via DefinitelyTyped ambient typings.
8+
import {browser, element, by, By, $, $$, ExpectedConditions} from 'protractor/globals';
9+
10+
describe('protractor with typescript typings', () => {
11+
beforeEach(() => {
12+
browser.get('http://www.angularjs.org');
13+
});
14+
15+
it('should greet the named user', () => {
16+
element(by.model('yourName')).sendKeys('Julie');
17+
let greeting = element(by.binding('yourName'));
18+
expect(greeting.getText()).toEqual('Hello Julie!');
19+
});
20+
21+
it('should list todos', function() {
22+
let todoList = element.all(by.repeater('todo in todoList.todos'));
23+
expect(todoList.count()).toEqual(2);
24+
expect(todoList.get(1).getText()).toEqual('build an angular app');
25+
});
26+
});

exampleTypescript/specPageObjects.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// local import of the exported AngularPage class
2+
import {AngularHomepage} from './angularPage';
3+
4+
// The jasmine typings are brought in via DefinitelyTyped ambient typings.
5+
describe('angularjs homepage', () => {
6+
it('should greet the named user', () => {
7+
let angularHomepage = new AngularHomepage();
8+
angularHomepage.get();
9+
angularHomepage.setName('Julie');
10+
expect(angularHomepage.getGreeting()).toEqual('Hello Julie!');
11+
});
12+
});

exampleTypescript/tsconfig.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "commonjs",
5+
"moduleResolution": "node",
6+
"sourceMap": false,
7+
"declaration": false,
8+
"noImplicitAny": false
9+
},
10+
"exclude": [
11+
"node_modules",
12+
"typings/globals"
13+
]
14+
}

exampleTypescript/typings.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"globalDependencies": {
3+
"jasmine": "registry:dt/jasmine#2.2.0+20160412134438",
4+
"node": "registry:dt/node#4.0.0+20160423143914"
5+
}
6+
}

tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"website",
1717
"typings/globals",
1818
"scripts",
19-
"globals.ts"
19+
"globals.ts",
20+
"exampleTypescript"
2021
]
2122
}

0 commit comments

Comments
 (0)