@@ -5,82 +5,78 @@ import { INsCapabilities } from "./ins-capabilities";
55import {
66 IDevice ,
77 Device ,
8- DeviceManager ,
8+ DeviceController ,
99 Platform ,
1010 Status ,
1111 DeviceType
1212} from "mobile-devices-controller" ;
1313
1414
15- export class DeviceController {
15+ export class DeviceManger {
1616 private static _emulators : Map < string , IDevice > = new Map ( ) ;
1717
1818 public static async startDevice ( args : INsCapabilities ) {
19+ let device : IDevice = DeviceManger . getDefaultDevice ( args ) ;
1920 if ( args . isSauceLab || args . ignoreDeviceController ) {
20- return DeviceController . getDefaultDevice ( args ) ;
21+ return device ;
2122 }
2223
23- const allDevices = ( await DeviceManager . getAllDevices ( args . appiumCaps . platformName . toLowerCase ( ) ) ) ;
24- if ( ! allDevices || allDevices === null || allDevices . size === 0 ) {
25- console . log ( "We couldn't find any devices. We will try to prossede to appium! Maybe avd manager is missing" )
24+ const allDevices = ( await DeviceController . getDivices ( { platform : args . appiumCaps . platformName } ) ) ;
25+ if ( ! allDevices || allDevices === null || allDevices . length === 0 ) {
26+ console . log ( "We couldn't find any devices. We will try to proceed to appium! Maybe avd manager is missing" )
2627 console . log ( "Available devices:\n" , allDevices ) ;
2728 }
2829
29- let searchedDevices = allDevices . get ( args . appiumCaps . deviceName ) ;
30+ let searchedDevices = DeviceController . filter ( allDevices , { name : args . appiumCaps . deviceName , apiLevel : args . appiumCaps . platformVersion } ) ;
3031 if ( ! searchedDevices || searchedDevices . length === 0 ) {
3132 console . log ( `No such device ${ args . appiumCaps . deviceName } !!!\n Check your device name!!!` ) ;
3233 console . log ( "Available devices:\n" , allDevices ) ;
3334 }
3435
35- let device : IDevice ;
36-
3736 if ( searchedDevices && searchedDevices . length > 0 ) {
3837
3938 // Should find new device
4039 if ( ! args . reuseDevice ) {
41- device = DeviceController . getDevicesByStatus ( searchedDevices , Status . SHUTDOWN ) ;
40+ device = DeviceController . filter ( searchedDevices , { status : Status . SHUTDOWN } ) [ 0 ] ;
4241 }
4342
4443 // If there is no shutdown device
45- if ( ! device || device === null ) {
46- device = DeviceController . getDevicesByStatus ( searchedDevices , Status . BOOTED ) ;
44+ if ( ! device || device === null || ! device . status ) {
45+ device = DeviceController . filter ( searchedDevices , { status : Status . BOOTED } ) [ 0 ] ;
4746 }
4847
4948 // In case reuse device is true but there weren't any booted devices. We need to fall back and boot new one.
5049 if ( ! device || device === null && args . reuseDevice ) {
51- device = DeviceController . getDevicesByStatus ( searchedDevices , Status . SHUTDOWN ) ;
50+ device = DeviceController . filter ( searchedDevices , { status : Status . SHUTDOWN } ) [ 0 ] ;
5251 }
5352
5453 if ( device . status === Status . SHUTDOWN ) {
55- await DeviceManager . startDevice ( device ) ;
54+ await DeviceController . startDevice ( device ) ;
5655 console . log ( "Started device: " , device ) ;
5756 } else {
5857 console . log ( "Device is already started" , device ) ;
59- if ( ! args . reuseDevice ) {
58+ if ( ! args . reuseDevice && device . type !== DeviceType . EMULATOR && device . type !== DeviceType . SIMULATOR ) {
59+ console . log ( "Since is it specified without reusing, the device would be shut down and restart!" ) ;
6060 DeviceController . kill ( device ) ;
61- await DeviceManager . startDevice ( device ) ;
61+ await DeviceController . startDevice ( device ) ;
6262 }
6363 }
6464 }
6565
66- if ( ! device || device === null ) {
67- device = DeviceController . getDefaultDevice ( args ) ;
68- }
69-
70- DeviceController . _emulators . set ( args . runType , device ) ;
66+ DeviceManger . _emulators . set ( args . runType , device ) ;
7167
7268 return device ;
7369 }
7470
7571 public static async stop ( args : INsCapabilities ) {
76- if ( DeviceController . _emulators . has ( args . runType ) && ! args . reuseDevice && ! args . isSauceLab && ! args . ignoreDeviceController ) {
77- const device = DeviceController . _emulators . get ( args . runType ) ;
78- await DeviceManager . kill ( device ) ;
72+ if ( DeviceManger . _emulators . has ( args . runType ) && ! args . reuseDevice && ! args . isSauceLab && ! args . ignoreDeviceController ) {
73+ const device = DeviceManger . _emulators . get ( args . runType ) ;
74+ await DeviceManger . kill ( device ) ;
7975 }
8076 }
8177
8278 public static async kill ( device : IDevice ) {
83- await DeviceManager . kill ( device ) ;
79+ await DeviceController . kill ( device ) ;
8480 }
8581
8682
@@ -89,19 +85,19 @@ export class DeviceController {
8985 }
9086
9187 private static device ( runType ) {
92- return DeviceController . _emulators . get ( runType ) ;
88+ return DeviceManger . _emulators . get ( runType ) ;
9389 }
9490
95- private static getDevicesByStatus ( devices : Array < IDevice > , status ) {
96- let device : IDevice ;
97- const shutdownDeivces = devices . filter ( dev => {
98- return dev . status === status ;
99- } ) ;
91+ // private static getDevicesByStatus(devices: Array<IDevice>, status) {
92+ // let device: IDevice;
93+ // const shutdownDeivces = devices.filter(dev => {
94+ // return dev.status === status;
95+ // });
10096
101- if ( shutdownDeivces && shutdownDeivces !== null && shutdownDeivces . length > 0 ) {
102- device = shutdownDeivces [ 0 ] ;
103- }
97+ // if (shutdownDeivces && shutdownDeivces !== null && shutdownDeivces.length > 0) {
98+ // device = shutdownDeivces[0];
99+ // }
104100
105- return device ;
106- }
101+ // return device;
102+ // }
107103}
0 commit comments