This repository has been archived by the owner on Jan 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
123 lines (96 loc) · 2.71 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env node
/// <reference path="../@types/index.d.ts" />
import tap = require('tap');
import {sync} from 'glob';
import {Builder, WebDriver} from 'selenium-webdriver';
import {Accessor} from './accessor';
import {Action} from './action';
import {Browser} from './browser';
import {config} from './config';
import {Description} from './description';
import {Element} from './element';
import {PredicateBuilder} from './predicate';
import {Test} from './test';
export {
Accessor, Action, Browser, Description, Element, PredicateBuilder, Test
};
export type Implementation = (t: Test) => Promise<void>;
export function defineElement(selector: string): Element {
return new Element(selector);
}
export class It {
public get should(): PredicateBuilder {
return new PredicateBuilder();
}
}
export const it = new It();
export const browser = new Browser();
class TapTest extends Test {
private readonly t: Tap.Test;
public constructor(driver: WebDriver, stepTimeout: number, t: Tap.Test) {
super(driver, stepTimeout);
this.t = t;
}
public fail(message: string, cause: Error): void {
throw new Error(`${message} (Cause: ${cause.message})`);
}
public pass(message: string): void {
this.t.pass(message);
}
}
const tasks: (() => void)[] = [];
export function test(
name: string,
implementation?: Implementation,
stepTimeout: number = config.stepTimeout
): void {
tasks.push(() => {
// tslint:disable-next-line no-floating-promises
tap.test(name, {
diagnostic: false, timeout: 0, todo: !implementation
}, async t => {
if (implementation) {
const driver = await new Builder().withCapabilities(
config.capabilities
).build();
try {
await implementation(new TapTest(driver, stepTimeout, t));
} finally {
await driver.quit();
}
}
}).catch((error: Error) => {
tap.fail(error.message);
});
});
}
export function skip(
name: string, implementation: Implementation, stepTimeout?: number
): void {
tasks.push(() => {
tap.test(name, {skip: true}); // tslint:disable-line no-floating-promises
});
}
if (require.main !== module) {
const packageName = require('../package.json').name;
console.error(`\nError: Please run your tests only via ${packageName} CLI`);
process.exit(1);
}
try {
for (const dependency of config.dependencies) {
require(dependency);
}
const filenames = sync(config.include, {
ignore: config.exclude, nodir: true, realpath: true
});
for (const filename of filenames) {
require(filename);
}
} catch (e) {
console.error(`\nError: ${e.message}`);
process.exit(1);
}
tap.jobs = config.concurrency;
for (const task of tasks) {
task();
}