Skip to content

Commit 7bf43c5

Browse files
committed
udpate everything
1 parent 13c2bc6 commit 7bf43c5

19 files changed

+277
-194
lines changed

README.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# TypeScript + Selenium example
2+
3+
This is a working example of using `Selenium`, `TypeScript` and `Page Object Pattern`.
4+
These are real test cases that run against a [Fider](http://getfider.com) instance.
5+
Check out a running example of the application at [http://demo.fider.io](http://demo.fider.io).
6+
7+
The tests are available in both TDD and BDD style.
8+
9+
# How it works
10+
11+
```
12+
.
13+
├── lib/
14+
├── pages/
15+
├── specs/
16+
├── tests/
17+
└── config.ts
18+
```
19+
20+
- `lib`: Think of this as a "framework". Ideally should be a separate module distributed on `npm`;
21+
- `pages`: These are your `Page Object` models. Each Page of your application should have a PageObject that maps all the elements and actions.
22+
- `specs`: All your BDD-style tests goes here
23+
- `tests`: All your TDD-style/classic tests goes here
24+
- `config.ts`: General configuration and settings that are read by tests.
25+
26+
## Test Case #1: Unauthenticated cannot submit ideas
27+
28+
`Unauthenticated users` should NOT be see `Description field` and `Submit button` after typing the title.
29+
30+
## Test Case #2: Authenticated can submit ideas
31+
32+
`Authenticated users` should be able to submit new ideas by clicking on `Submit button` after typing the title.
33+
After new idea is submitted, user should be redirect to the new idea's display page.
34+
35+
# See the tests
36+
37+
- TDD: [test.ts](specs/spec.ts)
38+
- BDD: [spec.ts](tests/test.ts)
39+
40+
# TODO:
41+
42+
Write about How it works (- TDD (tests) or BDD (specs) style, differences, - @findBy decorator)
43+
Write about benefits like (- Both use same objects, - Tests are decoupled from UI, - Easy to write new tests by reusing Page objects)
44+
45+
explain each folder

config.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const config = {
2+
baseUrl: process.env.BASE_URL || 'http://demo.dev.fider.io:3000',
3+
users: {
4+
darthvader: {
5+
email: 'darthvader.fider@gmail.com',
6+
password: process.env.DARTHVADER_PASSWORD!,
7+
},
8+
},
9+
};
10+
11+
export default config;

lib/browser.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
require('chromedriver');
2-
3-
import { Builder, ThenableWebDriver, WebElement, By, WebElementPromise, promise } from 'selenium-webdriver';
1+
import 'chromedriver';
2+
import { Builder, ThenableWebDriver, WebElement, By, WebElementPromise } from 'selenium-webdriver';
43
import { NewablePage, Page } from './page';
54

6-
export { WebElementPromise };
7-
85
export class Browser {
96
private driver: ThenableWebDriver;
107
public constructor(private browserName: string) {

lib/ensure.ts

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { WebElementPromise } from 'selenium-webdriver';
2+
3+
class Ensurer {
4+
private selector: string;
5+
constructor(private element: WebElementPromise) {
6+
this.selector = (this.element as any).selector;
7+
}
8+
9+
public async textIs(expected: string) {
10+
const text = await this.element.getText();
11+
12+
if (expected.trim() !== text.trim()) {
13+
throw new Error(`Element ${this.selector} text is '${text}'. Expected value is '${expected}'`);
14+
}
15+
}
16+
17+
public async isNotVisible() {
18+
let displayed = false;
19+
const selector = (this.element as any).selector;
20+
21+
try {
22+
displayed = await this.element.isDisplayed();
23+
} catch (ex) {
24+
displayed = false;
25+
}
26+
27+
if (displayed) {
28+
throw new Error(`Element ${this.selector} is visible`);
29+
}
30+
}
31+
}
32+
33+
export const ensure = (element: WebElementPromise) => {
34+
return new Ensurer(element);
35+
};

lib/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export * from './page';
22
export * from './browser';
33
export * from './utils';
4+
export * from './ensure';
5+
export { WebElementPromise } from 'selenium-webdriver';

lib/mocha-bdd.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { ISuiteCallbackContext } from 'mocha';
2+
3+
export const specification = (name: string, callback: (this: ISuiteCallbackContext) => void) => {
4+
return describe(`Specification: ${name}`, callback);
5+
};
6+
7+
export const when = (condition: string, callback: (this: ISuiteCallbackContext) => void) => {
8+
return describe(`when ${condition}`, callback);
9+
};
10+
11+
export const then = (condition: string, callback: (this: ISuiteCallbackContext) => void) => {
12+
return it(`then ${condition}`, callback);
13+
};
14+
15+
export const action = before;

lib/mocha.ts

-11
This file was deleted.

lib/utils.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ export function findBy(selector: string) {
88
configurable: true,
99
enumerable: true,
1010
get: function() {
11-
return (this as any).browser.findElement(selector);
11+
const promise = (this as any).browser.findElement(selector);
12+
promise.selector = selector;
13+
return promise;
1214
},
1315
});
1416
};

package-lock.json

+33-81
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"test": "rm -rf dist && tsc && mocha -t 10000 dist/specs"
7+
"tdd": "rm -rf dist && tsc && mocha -t 10000 dist/tests",
8+
"bdd": "rm -rf dist && tsc && mocha -t 10000 dist/specs"
89
},
910
"keywords": [],
10-
"author": "",
11+
"author": "Guilherme Oenning",
1112
"license": "ISC",
1213
"devDependencies": {
13-
"@types/chai": "^4.0.1",
1414
"@types/mocha": "^2.2.41",
1515
"@types/node": "^8.0.6",
1616
"@types/selenium-webdriver": "^3.0.4",
17-
"chai": "^4.0.2",
1817
"chromedriver": "^2.30.1",
1918
"mocha": "^3.4.2",
2019
"selenium-webdriver": "^3.4.0",

0 commit comments

Comments
 (0)