@@ -32,11 +32,12 @@ export class DeviceManager implements IDeviceManager {
32
32
public async startDevice ( args : INsCapabilities ) : Promise < IDevice > {
33
33
args . appiumCaps . platformName = args . appiumCaps . platformName . toLowerCase ( ) ;
34
34
let device : IDevice = DeviceManager . getDefaultDevice ( args ) ;
35
- if ( process . env [ "DEVICE_TOKEN" ] ) {
36
- device . token = process . env [ "DEVICE_TOKEN" ] ;
35
+ console . log ( "Default device: " , device ) ;
36
+ const token = process . env [ "DEVICE_TOKEN" ] || process . env . npm_config_deviceToken ;
37
+ if ( token ) {
38
+ device . token = token ;
37
39
device . name = process . env [ "DEVICE_NAME" ] || device . name ;
38
- const allDevices = await DeviceController . getDevices ( { platform : device . platform } ) ;
39
- const foundDevice = DeviceController . filter ( allDevices , { token : device . token . replace ( "emulator-" , "" ) } ) [ 0 ] ;
40
+ const foundDevice = await DeviceController . getDevices ( { token : device . token . replace ( "emulator-" , "" ) } ) [ 0 ] ;
40
41
logInfo ( "Device: " , foundDevice ) ;
41
42
return foundDevice ;
42
43
}
@@ -48,62 +49,61 @@ export class DeviceManager implements IDeviceManager {
48
49
return device ;
49
50
}
50
51
51
- const allDevices = await DeviceController . getDevices ( { platform : args . appiumCaps . platformName } ) ;
52
- if ( ! allDevices || allDevices === null || allDevices . length === 0 ) {
53
- logError ( "We couldn't find any devices. We will try to proceed to appium!" )
54
- console . log ( "Available devices:\n" , allDevices ) ;
52
+ const searchQuery = args . appiumCaps . udid ? { token : args . appiumCaps . udid } : device ;
53
+
54
+ const foundDevices = await DeviceController . getDevices ( searchQuery ) ;
55
+ if ( ! foundDevices || foundDevices . length === 0 ) {
56
+ logError ( "We couldn't find any devices of type: " , searchQuery ) ;
57
+ logError ( "We will try to proceed to appium!" ) ;
58
+ if ( device . platform ) {
59
+ console . log ( "Available devices:\t\t\t\t" , await DeviceController . getDevices ( { platform : device . platform } ) ) ;
60
+ } else {
61
+ console . log ( "Available devices:\t\t\t\t" , await DeviceController . getDevices ( { } ) ) ;
62
+ }
55
63
logWarn ( `We couldn't find any devices. We will try to proceed to appium!` ) ;
56
64
if ( args . appiumCaps . platformVersion . toLowerCase ( ) === Platform . ANDROID ) {
57
65
const errMsg = `1. Check if ANDROID_HOME environment variable is set correctly!\n
58
66
2. Check if avd manager is available!
59
- 3. Check appium capabilites and provide correct device options!` ;
67
+ 3. Check appium capabilities and provide correct device options!` ;
60
68
logWarn ( errMsg ) ;
61
69
}
62
70
args . ignoreDeviceController = true ;
71
+ return device ;
63
72
}
64
73
65
- const searchObj = args . appiumCaps . udid ? { token : args . appiumCaps . udid } : { name : args . appiumCaps . deviceName , apiLevel : args . appiumCaps . platformVersion } ;
66
- let searchedDevices = DeviceController . filter ( allDevices , searchObj ) ;
67
- if ( ! searchedDevices || searchedDevices . length === 0 ) {
68
- logError ( `No such device ${ args . appiumCaps . deviceName } !!!` ) ;
69
- logWarn ( "All properties like platformVersion, deviceName etc should match!" ) ;
70
- logInfo ( "Available devices:\t\t\t\t" ) ;
71
- console . log ( '' , allDevices ) ;
74
+ if ( args . verbose ) {
75
+ console . log ( "Found devices: " , foundDevices ) ;
72
76
}
73
77
74
- if ( searchedDevices && searchedDevices . length > 0 ) {
75
-
76
- // Should find new device
77
- if ( ! args . reuseDevice ) {
78
- device = DeviceController . filter ( searchedDevices , { status : Status . SHUTDOWN } ) [ 0 ] ;
79
- }
78
+ if ( foundDevices && foundDevices . length > 0 ) {
79
+ let deviceStatus = args . reuseDevice ? Status . BOOTED : Status . SHUTDOWN ;
80
+ device = DeviceController . filter ( foundDevices , { status : deviceStatus } ) [ 0 ] ;
80
81
81
82
// If there is no shutdown device
82
- if ( ! device || device === null || ! device . status ) {
83
- device = DeviceController . filter ( searchedDevices , { status : Status . BOOTED } ) [ 0 ] ;
83
+ if ( ! device || ! device . status ) {
84
+ deviceStatus = args . reuseDevice ? Status . SHUTDOWN : Status . BOOTED ;
85
+ device = DeviceController . filter ( foundDevices , { status : deviceStatus } ) [ 0 ] ;
84
86
}
85
87
86
- // In case reuse device is true but there weren't any booted devices. We need to fall back and boot new one.
87
- if ( ! device || device === null && args . reuseDevice ) {
88
- device = DeviceController . filter ( searchedDevices , { status : Status . SHUTDOWN } ) [ 0 ] ;
88
+ // If the device should not be reused we need to shutdown device and boot a clean instance
89
+ let startDeviceOptions = args . startDeviceOptions || undefined ;
90
+ if ( ! args . reuseDevice && device . status !== Status . SHUTDOWN ) {
91
+ await DeviceController . kill ( device ) ;
92
+ device . status = Status . SHUTDOWN ;
93
+ startDeviceOptions = device . type === DeviceType . EMULATOR ? "-wipe-data -no-snapshot-load -no-boot-anim -no-audio" : "" ;
94
+ logInfo ( "Change appium config to fullReset: false if no restart of the device needed!" ) ;
89
95
}
90
96
97
+ if ( device . type === DeviceType . DEVICE ) {
98
+ logInfo ( "Device is connected:" , device )
99
+ }
91
100
if ( device . status === Status . SHUTDOWN ) {
92
- await DeviceController . startDevice ( device ) ;
101
+ await DeviceController . startDevice ( device , startDeviceOptions ) ;
93
102
try {
94
103
delete device . process ;
95
104
} catch ( error ) { }
96
-
97
- await DeviceController . startDevice ( device , args . startDeviceOptions ) ;
105
+
98
106
logInfo ( "Started device: " , device ) ;
99
- } else {
100
- device . type === DeviceType . DEVICE ? logInfo ( "Device is connected:" , device ) : logInfo ( "Device is already started" , device )
101
- if ( ! args . reuseDevice && device . type !== DeviceType . DEVICE ) {
102
- logInfo ( "Option --reuseDevice is set to false, the device would be shut down and restart!" ) ;
103
- logInfo ( "Use --reuseDevice to preserve device state!" ) ;
104
- DeviceController . kill ( device ) ;
105
- await DeviceController . startDevice ( device ) ;
106
- }
107
107
}
108
108
}
109
109
@@ -114,7 +114,7 @@ export class DeviceManager implements IDeviceManager {
114
114
DeviceManager . _emulators . set ( args . runType , device ) ;
115
115
116
116
if ( ! device || ! device . token ) {
117
- console . error ( "Check appium capabilites and provide correct device options!" ) ;
117
+ console . error ( "Check appium capabilities and provide correct device options!" ) ;
118
118
process . exit ( 1 ) ;
119
119
}
120
120
return device ;
@@ -152,10 +152,20 @@ export class DeviceManager implements IDeviceManager {
152
152
}
153
153
154
154
public static getDefaultDevice ( args : INsCapabilities , deviceName ?: string , token ?: string , type ?: DeviceType , platformVersion ?: number ) {
155
- let device = new Device ( deviceName || args . appiumCaps . deviceName , platformVersion || args . appiumCaps . platformVersion , type , args . appiumCaps . platformName . toLowerCase ( ) , token , undefined , undefined ) ;
156
- device . config = { "density" : args . appiumCaps . density , "offsetPixels" : args . appiumCaps . offsetPixels } ;
155
+ let device : IDevice = {
156
+ name : deviceName || args . appiumCaps . deviceName ,
157
+ type : type ,
158
+ platform : args . appiumCaps . platformName . toLowerCase ( ) ,
159
+ token : token ,
160
+ apiLevel : platformVersion || args . appiumCaps . platformVersion ,
161
+ config : { "density" : args . appiumCaps . density , "offsetPixels" : args . appiumCaps . offsetPixels }
162
+ }
163
+
157
164
delete args . appiumCaps . density ;
158
165
delete args . appiumCaps . offsetPixels ;
166
+
167
+ Object . getOwnPropertyNames ( device ) . forEach ( prop => ! device [ prop ] && delete device [ prop ] ) ;
168
+
159
169
return device ;
160
170
}
161
171
0 commit comments