1- import * as portastic from "portastic" ;
21import { AppiumServer } from "./lib/appium-server" ;
32import { AppiumDriver } from "./lib/appium-driver" ;
43import { ElementHelper } from "./lib/element-helper" ;
54import { NsCapabilities } from "./lib/ns-capabilities" ;
6- import { shutdown } from "./lib/utils" ;
5+ import { IDeviceManager } from "./lib/interfaces/device-manager" ;
6+ import { shutdown , findFreePort } from "./lib/utils" ;
77
88export { AppiumDriver } from "./lib/appium-driver" ;
9+ export { AppiumServer } from "./lib/appium-server" ;
910export { ElementHelper } from "./lib/element-helper" ;
1011export { UIElement } from "./lib/ui-element" ;
1112export { Point } from "./lib/point" ;
@@ -14,52 +15,27 @@ export { Locator } from "./lib/locators";
1415export { Direction } from "./lib/direction" ;
1516export { DeviceManger } from "./lib/device-controller" ;
1617export { IRectangle } from "./lib/interfaces/rectangle" ;
18+ export { IDeviceManager } from "./lib/interfaces/device-manager" ;
1719
1820const nsCapabilities = new NsCapabilities ( ) ;
1921const appiumServer = new AppiumServer ( nsCapabilities ) ;
2022
2123let appiumDriver = null ;
22- export async function startServer ( port ?: number ) {
23- appiumServer . port = port || nsCapabilities . port ;
24- let retry = false ;
25- if ( ! appiumServer . port ) {
26- appiumServer . port = ( await portastic . find ( { min : 8600 , max : 9080 } ) ) [ 0 ] ;
27- retry = true ;
28- }
29-
30- let hasStarted = await appiumServer . start ( ) ;
31- let retryCount = 0 ;
32- while ( retry && ! hasStarted && retryCount < 10 ) {
33- let tempPort = appiumServer . port + 10 ;
34- tempPort = ( await portastic . find ( { min : tempPort , max : 9180 } ) ) [ 0 ] ;
35- console . log ( "Trying to use port: " , tempPort ) ;
36- appiumServer . port = tempPort ;
37- hasStarted = await appiumServer . start ( ) ;
38- retryCount ++ ;
39- }
40-
41- if ( ! hasStarted ) {
42- throw new Error ( "Appium driver failed to start!!! Run with --verbose option for more info!" ) ;
43- }
44-
45- appiumServer . hasStarted = hasStarted ;
46-
47- process . on ( "uncaughtException" , ( ) => shutdown ( appiumServer . server , nsCapabilities . verbose ) ) ;
48- process . on ( "exit" , ( ) => shutdown ( appiumServer . server , nsCapabilities . verbose ) ) ;
49- process . on ( "SIGINT" , ( ) => shutdown ( appiumServer . server , nsCapabilities . verbose ) ) ;
24+ export async function startServer ( port ?: number , deviceManager ?: IDeviceManager ) {
25+ await appiumServer . start ( port || 8300 , deviceManager ) ;
5026} ;
5127
5228export async function stopServer ( ) {
5329 if ( appiumDriver !== null && appiumDriver . isAlive ) {
5430 await appiumDriver . quit ( ) ;
5531 }
56- if ( appiumServer !== null && appiumServer . hasStarted ) {
32+ if ( appiumServer !== null && appiumServer . server && ! appiumServer . server . killed ) {
5733 await appiumServer . stop ( ) ;
5834 }
5935} ;
6036
6137export async function createDriver ( ) {
62- if ( ! appiumServer . hasStarted ) {
38+ if ( ! appiumServer . server ) {
6339 throw new Error ( "Server is not available!" ) ;
6440 }
6541 if ( ! nsCapabilities . appiumCapsLocation ) {
@@ -79,3 +55,15 @@ export async function createDriver() {
7955
8056 return appiumDriver ;
8157}
58+
59+ const killProcesses = async ( code ) => {
60+ if ( appiumServer ) {
61+ return await stopServer ( ) ;
62+ }
63+ }
64+
65+ process . on ( "exit" , async ( code ) => await killProcesses ( code ) ) ;
66+ process . on ( "close" , async ( code ) => await killProcesses ( code ) ) ;
67+ process . on ( "SIGINT" , async ( code ) => await killProcesses ( code ) ) ;
68+ process . on ( "error" , async ( code ) => await killProcesses ( code ) ) ;
69+ process . on ( "uncaughtException" , ( ) => async ( code ) => await killProcesses ( code ) ) ;
0 commit comments