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

Commit 005ae09

Browse files
authored
Merge pull request #1 from react-native-windows/refactor
Refactor
2 parents d94b47e + b467f07 commit 005ae09

File tree

5 files changed

+131
-47
lines changed

5 files changed

+131
-47
lines changed

package.json

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "selenium-appium",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"description": "A brdige to make selenium-webdriver to drive appium to do native app testing",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -11,14 +11,24 @@
1111
"scripts": {
1212
"prepublishOnly": "tsc -p ./ --outDir dist/"
1313
},
14-
"keywords": ["selenium-webdriver", "appium", "jest", "e2e test"],
14+
"keywords": [
15+
"selenium-webdriver",
16+
"appium",
17+
"jest",
18+
"e2e test"
19+
],
1520
"author": "Canhua Li",
1621
"license": "MIT",
1722
"devDependencies": {
1823
"@types/selenium-webdriver": "^4.0.1",
19-
"typescript": "^3.5.3"
24+
"typescript": "^3.5.3"
2025
},
2126
"peerDependencies": {
2227
"selenium-webdriver": "^4.0.0-alpha.4"
23-
}
28+
},
29+
"bugs": {
30+
"url": "https://github.com/react-native-windows/selenium-appium/issues"
31+
},
32+
"homepage": "https://github.com/react-native-windows/selenium-appium#readme",
33+
"dependencies": {}
2434
}

src/appiumdriver.ts

Lines changed: 93 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,118 @@
44
*/
55

66
'use strict';
7+
import { WebDriver, Builder, until, Capabilities, WebElementPromise } from 'selenium-webdriver'
8+
import { By2 } from './by2';
9+
import { By } from 'selenium-webdriver'
710

8-
import { WebDriver, Builder, until } from 'selenium-webdriver'
9-
import { By } from './by';
11+
interface IAppiumWaitUntilFound {
12+
get(by: By, timeout?: number, message?: string): WebElementPromise;
13+
getByAccessibilityId(id: string, timeout?: number, message?: string): WebElementPromise;
14+
getByName(name: string, timeout?: number, message?: string): WebElementPromise;
15+
getById(id: string, timeout?: number, message?: string): WebElementPromise;
16+
getByclassName(className: string, timeout?: number, message?: string): WebElementPromise;
17+
}
1018

11-
export class AppiumDriver {
12-
private static windowsCapabilities(appName: string) {
13-
return {
14-
browserName: '',
15-
platformName: 'windows',
16-
deviceName: 'WindowsPC',
17-
app: appName
18-
};
19+
export interface IAppiumDriver extends IAppiumWaitUntilFound
20+
{
21+
start(): Promise<void>;
22+
stop(): Promise<void>;
23+
restart(): Promise<void>;
24+
isActive(): Promise<boolean>;
25+
seleniumDriver(): WebDriver;
26+
lastError(): any;
27+
sleep(ms: number): Promise<void>;
28+
}
29+
30+
class AppiumDriver implements IAppiumDriver {
31+
get(by: By, timeout?: number, message?: string | undefined): WebElementPromise {
32+
if (this.webDriver)
33+
return this.webDriver.wait(until.elementLocated(by), timeout, message);
34+
throw new Error("no valid connection");
1935
}
2036

21-
driver_: WebDriver;
37+
getByAccessibilityId(id: string, timeout?: number | undefined, message?: string | undefined): WebElementPromise {
38+
return this.get(By2.accessibilityId(id), timeout, message);
39+
}
2240

23-
static createWinAppDriver(appName: string, url = "http://localhost:4723/wd/hub"): Promise<AppiumDriver> {
24-
return new Promise<AppiumDriver>(resolve => {
25-
new Builder()
26-
.usingServer(url)
27-
.withCapabilities(AppiumDriver.windowsCapabilities(appName))
28-
.build().
29-
then((driver) => { resolve(new AppiumDriver(driver)); });
30-
});
41+
getByName(name: string, timeout?: number | undefined, message?: string | undefined): WebElementPromise {
42+
return this.get(By2.name(name), timeout, message);
3143
}
3244

33-
webDriver(): WebDriver {
34-
return this.driver_;
45+
getById(id: string, timeout?: number | undefined, message?: string | undefined): WebElementPromise {
46+
return this.get(By2.id(id), timeout, message);
3547
}
3648

37-
constructor(driver: WebDriver) {
38-
this.driver_ = driver;
49+
getByclassName(className: string, timeout?: number | undefined, message?: string | undefined): WebElementPromise {
50+
return this.get(By2.className(className), timeout, message);
3951
}
4052

41-
getByAccessibilityId(id: string, timeout = 0, message = undefined) {
42-
return this.driver_.wait(until.elementLocated(By.accessibilityId(id)), timeout, message);
53+
sleep(ms: number): Promise<void> {
54+
if (this.webDriver)
55+
return this.webDriver.sleep(ms);
56+
throw new Error("no valid connection");
4357
}
4458

45-
getByName(name: string, timeout = 0, message = undefined) {
46-
return this.driver_.wait(until.elementLocated(By.name(name)), timeout, message);
59+
seleniumDriver(): WebDriver {
60+
if (this.webDriver)
61+
return this.webDriver;
62+
throw new Error("no valid connection");
4763
}
4864

49-
getById(id: string, timeout = 0, message = undefined) {
50-
return this.driver_.wait(until.elementLocated(By.id(id)), timeout, message);
65+
lastError() {
66+
return this.error_;
5167
}
5268

53-
getByclassName(className: string, timeout = 0, message = undefined) {
54-
return this.driver_.wait(until.elementLocated(By.className(className)), timeout, message);
69+
start(): Promise<void> {
70+
return new Promise<void>(resolve => {
71+
new Builder()
72+
.usingServer(this.url_)
73+
.withCapabilities(this.capabilities_)
74+
.build()
75+
.then(driver => { this.webDriver = driver; resolve(); })
76+
.catch(e => { this.error_ = e; throw e; });
77+
});
5578
}
5679

57-
sleep(ms: number) {
58-
return this.driver_.sleep(ms);
80+
stop(): Promise<void> {
81+
return new Promise<void>(resolve => {
82+
this.webDriver && this.webDriver
83+
.quit()
84+
.then(() => resolve())
85+
.catch(e => { this.error_ = e; throw e });
86+
resolve();
87+
});
5988
}
60-
quit() {
61-
return this.driver_.quit();
89+
90+
restart(): Promise<void> {
91+
return this._restart();
6292
}
93+
94+
private async _restart() {
95+
await this.stop().catch(); //ignore stop error
96+
await this.start();
97+
}
98+
99+
private capabilities_: Capabilities | {};
100+
private error_?: any;
101+
private url_: string
102+
private webDriver?: WebDriver;
103+
104+
constructor(capabilities: Capabilities | {}, url: string = "http://localhost:4723/wd/hub") {
105+
this.capabilities_ = capabilities;
106+
this.url_ = url;
107+
}
108+
109+
isActive(): Promise<boolean> {
110+
return new Promise<boolean>(resolve => {
111+
this.webDriver && this.webDriver
112+
.getSession()
113+
.then(() => resolve(true), reason => resolve(false))
114+
resolve(false);
115+
});
116+
}
117+
}
118+
119+
export function createAppiumWebDriver(capabilities: Capabilities | {}, url?: string): IAppiumDriver {
120+
return new AppiumDriver(capabilities, url);
63121
}

src/appiumdriver.windows.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License.
4+
*/
5+
6+
'use strict';
7+
8+
export function windowsNativeAppCapabilities(appName: string) {
9+
return {
10+
browserName: '',
11+
platformName: 'windows',
12+
deviceName: 'WindowsPC',
13+
app: appName
14+
}
15+
};

src/by.ts renamed to src/by2.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,23 @@
44
*/
55

66
'use strict';
7-
import { By as SeleniumBy } from 'selenium-webdriver'
7+
import { By } from 'selenium-webdriver'
88

9-
export class By {
9+
export class By2 {
1010
static id(id: string) {
11-
return new SeleniumBy('id', id);
11+
return new By('id', id);
1212
}
1313

1414
static accessibilityId(id: string) {
15-
return new SeleniumBy('accessibility id', id);
15+
return new By('accessibility id', id);
1616
}
1717

1818
// @ts-ignore: error TS2699: Static property 'name' conflicts with built-in property 'Function.name'
1919
static name(name: string) {
20-
return new SeleniumBy('name', name);
20+
return new By('name', name);
2121
}
2222

2323
static className(name: string) {
24-
return new SeleniumBy('class name', name);
24+
return new By('class name', name);
2525
}
2626
}

src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
* Licensed under the MIT License.
44
*/
55

6-
export { By } from './by';
7-
export { AppiumDriver } from './appiumdriver'
6+
export { By2 } from './by2';
7+
export { IAppiumDriver, createAppiumWebDriver } from './appiumdriver'
8+
export { windowsNativeAppCapabilities } from './appiumdriver.windows'

0 commit comments

Comments
 (0)