@@ -13,7 +13,7 @@ import { DEFAULT_ADB_PORT } from 'appium-adb';
13
13
import {
14
14
androidHelpers , SETTINGS_HELPER_PKG_ID , AndroidDriver
15
15
} from 'appium-android-driver' ;
16
- import { desiredCapConstraints } from './desired-caps ' ;
16
+ import { ESPRESSO_CONSTRAINTS } from './constraints ' ;
17
17
import { findAPortNotInUse } from 'portscanner' ;
18
18
import { retryInterval } from 'asyncbox' ;
19
19
import { qualifyActivityName , getPackageInfo } from './utils' ;
@@ -133,8 +133,21 @@ const APK_EXT = '.apk';
133
133
const AAB_EXT = '.aab' ;
134
134
const SUPPORTED_EXTENSIONS = [ APK_EXT , AAB_EXT ] ;
135
135
136
+ /**
137
+ * @satisfies {import('@appium/types').ExternalDriver<
138
+ * import('./constraints').EspressoConstraints,
139
+ * string,
140
+ * import('@appium/types').StringRecord
141
+ * >}
142
+ */
136
143
class EspressoDriver extends AndroidDriver {
137
144
145
+ /** @type {import('./types').EspressoDriverOpts } */
146
+ opts ;
147
+
148
+ /** @type {import('./types').EspressoDriverCaps } */
149
+ caps ;
150
+
138
151
/** @type {EspressoRunner } */
139
152
espresso ;
140
153
@@ -144,22 +157,21 @@ class EspressoDriver extends AndroidDriver {
144
157
// `shell` overwrites adb.shell, so remove
145
158
delete opts . shell ;
146
159
147
- // @ts -ignore Options are ok
148
- super ( opts , shouldValidateCaps ) ;
160
+ super ( /** @type {import('@appium/types').InitialOpts } */ ( opts ) , shouldValidateCaps ) ;
149
161
this . locatorStrategies = [
150
162
'id' ,
151
163
'class name' ,
152
164
'accessibility id' ,
153
165
] ;
154
- this . desiredCapConstraints = desiredCapConstraints ;
166
+ this . desiredCapConstraints = ESPRESSO_CONSTRAINTS ;
155
167
this . jwpProxyAvoid = NO_PROXY ;
156
168
157
169
this . settings = new DeviceSettings ( { } , this . onSettingsUpdate . bind ( this ) ) ;
158
170
159
- this . chromedriver = null ;
171
+ this . chromedriver = undefined ;
160
172
}
161
173
162
- // @ts -ignore it's ok
174
+ // @ts -ignore it's ok to not care about exact args
163
175
async createSession ( ...args ) {
164
176
try {
165
177
// @ts -ignore it's ok to not care about exact args
@@ -200,24 +212,18 @@ class EspressoDriver extends AndroidDriver {
200
212
}
201
213
}
202
214
203
- // @ts -ignore TODO: Make it typed
204
- if ( this . opts . reboot ) {
205
- this . setAvdFromCapabilities ( caps ) ;
206
- this . addWipeDataToAvdArgs ( ) ;
207
- }
208
-
209
- // @ts -ignore TODO: Make it typed
210
215
this . opts . systemPort = this . opts . systemPort
211
216
|| await findAPortNotInUse ( SYSTEM_PORT_RANGE [ 0 ] , SYSTEM_PORT_RANGE [ 1 ] ) ;
212
217
this . opts . adbPort = this . opts . adbPort || DEFAULT_ADB_PORT ;
213
218
// get device udid for this session
214
219
const { udid, emPort} = await helpers . getDeviceInfoFromCaps ( this . opts ) ;
215
220
this . opts . udid = udid ;
216
- // @ts -ignore TODO: Make it typed
217
- this . opts . emPort = emPort ;
218
221
// now that we know our java version and device info, we can create our
219
222
// ADB instance
220
- this . adb = await androidHelpers . createADB ( this . opts ) ;
223
+ this . adb = await androidHelpers . createADB ( {
224
+ ...this . opts ,
225
+ emPort,
226
+ } ) ;
221
227
222
228
if ( this . opts . app ) {
223
229
// find and copy, or download and unzip an app url or path
@@ -295,7 +301,6 @@ class EspressoDriver extends AndroidDriver {
295
301
const hasApkExt = ( appPath ) => _ . endsWith ( _ . toLower ( appPath ) , APK_EXT ) ;
296
302
const hasAabExt = ( appPath ) => _ . endsWith ( _ . toLower ( appPath ) , AAB_EXT ) ;
297
303
const extractUniversalApk = async ( shouldExtract , appPath ) =>
298
- // @ts -ignore extractUniversalApk is there
299
304
shouldExtract ? appPath : await this . adb . extractUniversalApk ( appPath ) ;
300
305
301
306
let pathInCache = null ;
@@ -353,32 +358,6 @@ class EspressoDriver extends AndroidDriver {
353
358
return { } ;
354
359
}
355
360
356
- // TODO this method is duplicated from uiautomator2-driver; consolidate
357
- setAvdFromCapabilities ( caps ) {
358
- if ( this . opts . avd ) {
359
- this . log . info ( 'avd name defined, ignoring device name and platform version' ) ;
360
- } else {
361
- if ( ! caps . deviceName ) {
362
- this . log . errorAndThrow ( 'avd or deviceName should be specified when reboot option is enables' ) ;
363
- }
364
- if ( ! caps . platformVersion ) {
365
- this . log . errorAndThrow ( 'avd or platformVersion should be specified when reboot option is enabled' ) ;
366
- }
367
- let avdDevice = caps . deviceName . replace ( / [ ^ a - z A - Z 0 - 9 _ . ] / g, '-' ) ;
368
- this . opts . avd = `${ avdDevice } __${ caps . platformVersion } ` ;
369
- }
370
- }
371
-
372
- // TODO this method is duplicated from uiautomator2-driver; consolidate
373
- addWipeDataToAvdArgs ( ) {
374
- if ( ! this . opts . avdArgs ) {
375
- this . opts . avdArgs = '-wipe-data' ;
376
- // @ts -ignore This check will work
377
- } else if ( ! _ . includes ( this . opts . avdArgs , '-wipe-data' ) ) {
378
- this . opts . avdArgs += ' -wipe-data' ;
379
- }
380
- }
381
-
382
361
// TODO much of this logic is duplicated from uiautomator2
383
362
async startEspressoSession ( ) {
384
363
const { manifestPayload} = await getPackageInfo ( ) ;
@@ -396,13 +375,11 @@ class EspressoDriver extends AndroidDriver {
396
375
// and get it onto our 'opts' object so we use it from now on
397
376
Object . assign ( this . opts , appInfo ) ;
398
377
} else {
399
- // @ts -ignore This is ok
400
378
appInfo = this . opts ;
401
379
}
402
380
403
381
// start an avd, set the language/locale, pick an emulator, etc...
404
382
// TODO with multiple devices we'll need to parameterize this
405
- // @ts -ignore This is ok
406
383
await helpers . initDevice ( this . adb , this . opts ) ;
407
384
408
385
// https://github.com/appium/appium-espresso-driver/issues/72
@@ -411,16 +388,13 @@ class EspressoDriver extends AndroidDriver {
411
388
412
389
// set actual device name, udid
413
390
this . caps . deviceName = this . adb . curDeviceId ;
414
- // @ts -ignore This is ok
415
- this . caps . deviceUDID = this . opts . udid ;
391
+ this . caps . deviceUDID = /** @type {string } */ ( this . opts . udid ) ;
416
392
417
393
// set up the modified espresso server etc
418
394
this . initEspressoServer ( ) ;
419
395
// Further prepare the device by forwarding the espresso port
420
- // @ts -ignore TODO: Make the module to .ts
421
396
this . log . debug ( `Forwarding Espresso Server port ${ DEVICE_PORT } to ${ this . opts . systemPort } ` ) ;
422
- // @ts -ignore TODO: Make the module to .ts
423
- await this . adb . forwardPort ( this . opts . systemPort , DEVICE_PORT ) ;
397
+ await this . adb . forwardPort ( /** @type {number } */ ( this . opts . systemPort ) , DEVICE_PORT ) ;
424
398
425
399
if ( ! this . opts . skipUnlock ) {
426
400
// unlock the device to prepare it for testing
@@ -517,64 +491,45 @@ class EspressoDriver extends AndroidDriver {
517
491
realDisplaySize,
518
492
displayDensity,
519
493
} = await this . mobileGetDeviceInfo ( ) ;
520
- // @ts -ignore TODO: Make the module to .ts
521
494
this . caps . deviceApiLevel = parseInt ( apiVersion , 10 ) ;
522
495
this . caps . platformVersion = platformVersion ;
523
- // @ts -ignore TODO: Make the module to .ts
524
496
this . caps . deviceScreenSize = realDisplaySize ;
525
- // @ts -ignore TODO: Make the module to .ts
526
497
this . caps . deviceScreenDensity = displayDensity ;
527
- // @ts -ignore TODO: Make the module to .ts
528
498
this . caps . deviceModel = model ;
529
- // @ts -ignore TODO: Make the module to .ts
530
499
this . caps . deviceManufacturer = manufacturer ;
531
500
}
532
501
533
502
initEspressoServer ( ) {
534
503
// now that we have package and activity, we can create an instance of
535
504
// espresso with the appropriate data
536
505
this . espresso = new EspressoRunner ( this . log , {
537
- // @ts -ignore TODO: Make the module to .ts
506
+ // @ts -ignore TODO: Is .host a legacy property?
538
507
host : this . opts . remoteAdbHost || this . opts . host || '127.0.0.1' ,
539
- // @ts -ignore TODO: Make the module to .ts
540
508
systemPort : this . opts . systemPort ,
541
509
devicePort : DEVICE_PORT ,
542
510
adb : this . adb ,
543
511
apk : this . opts . app ,
544
512
tmpDir : this . opts . tmpDir ,
545
513
appPackage : this . opts . appPackage ,
546
514
appActivity : this . opts . appActivity ,
547
- // @ts -ignore TODO: Make the module to .ts
548
515
forceEspressoRebuild : ! ! this . opts . forceEspressoRebuild ,
549
- // @ts -ignore TODO: Make the module to .ts
550
516
espressoBuildConfig : this . opts . espressoBuildConfig ,
551
- // @ts -ignore TODO: Make the module to .ts
552
517
showGradleLog : ! ! this . opts . showGradleLog ,
553
- // @ts -ignore TODO: Make the module to .ts
554
518
serverLaunchTimeout : this . opts . espressoServerLaunchTimeout ,
555
519
androidInstallTimeout : this . opts . androidInstallTimeout ,
556
- // @ts -ignore TODO: Make the module to .ts
557
520
skipServerInstallation : this . opts . skipServerInstallation ,
558
521
useKeystore : this . opts . useKeystore ,
559
522
keystorePath : this . opts . keystorePath ,
560
523
keystorePassword : this . opts . keystorePassword ,
561
524
keyAlias : this . opts . keyAlias ,
562
525
keyPassword : this . opts . keyPassword ,
563
- // @ts -ignore TODO: Make the module to .ts
564
526
disableSuppressAccessibilityService : this . opts . disableSuppressAccessibilityService ,
565
527
} ) ;
566
528
this . proxyReqRes = this . espresso . proxyReqRes . bind ( this . espresso ) ;
567
529
this . proxyCommand = this . espresso . proxyCommand . bind ( this . espresso ) ;
568
530
}
569
531
570
- // TODO this method is mostly duplicated from uiautomator2
571
532
async initAUT ( ) {
572
- // set the localized strings for the current language from the apk
573
- // TODO: incorporate changes from appium#5308 which fix a race cond-
574
- // ition bug in old appium and need to be replicated here
575
- // this.apkStrings[this.opts.language] = await androidHelpers.pushStrings(
576
- // this.opts.language, this.adb, this.opts);
577
-
578
533
// Uninstall any uninstallOtherPackages which were specified in caps
579
534
if ( this . opts . uninstallOtherPackages ) {
580
535
await helpers . uninstallOtherPackages (
@@ -600,7 +555,6 @@ class EspressoDriver extends AndroidDriver {
600
555
if ( this . opts . app ) {
601
556
await helpers . installApk ( this . adb , this . opts ) ;
602
557
}
603
- // @ts -ignore TODO: Make the module to .ts
604
558
if ( this . opts . skipServerInstallation ) {
605
559
this . log . debug ( 'skipServerInstallation capability is set. Not installig espresso-server ' ) ;
606
560
} else {
@@ -665,22 +619,14 @@ class EspressoDriver extends AndroidDriver {
665
619
await this . adb . uninstallApk ( /** @type {string } */ ( this . opts . appPackage ) ) ;
666
620
}
667
621
await this . adb . stopLogcat ( ) ;
668
- // @ts -ignore TODO: Make the module to .ts
669
- if ( this . opts . reboot && this . opts . avd ) {
670
- let avdName = this . opts . avd . replace ( '@' , '' ) ;
671
- this . log . debug ( `closing emulator '${ avdName } '` ) ;
672
- await this . adb . killEmulator ( avdName ) ;
673
- }
674
622
if ( await this . adb . getApiLevel ( ) >= 28 ) { // Android P
675
623
this . log . info ( 'Restoring hidden api policy to the device default configuration' ) ;
676
624
await this . adb . setDefaultHiddenApiPolicy ( ! ! this . opts . ignoreHiddenApiPolicyError ) ;
677
625
}
678
626
}
679
627
await super . deleteSession ( ) ;
680
- // @ts -ignore TODO: Make the module to .ts
681
- if ( this . opts . systemPort !== undefined ) {
628
+ if ( this . opts . systemPort ) {
682
629
try {
683
- // @ts -ignore TODO: Make the module to .ts
684
630
await this . adb . removePortForward ( this . opts . systemPort ) ;
685
631
} catch ( error ) {
686
632
this . log . warn ( `Unable to remove port forward '${ error . message } '` ) ;
@@ -723,8 +669,7 @@ class EspressoDriver extends AndroidDriver {
723
669
}
724
670
725
671
get appOnDevice ( ) {
726
- // @ts -ignore appPackage should always be defined
727
- return ! this . opts . app && this . helpers . isPackageOrBundle ( this . opts . appPackage ) ;
672
+ return ! this . opts . app && this . helpers . isPackageOrBundle ( /** @type {string } */ ( this . opts . appPackage ) ) ;
728
673
}
729
674
730
675
/**
@@ -733,7 +678,7 @@ class EspressoDriver extends AndroidDriver {
733
678
* @returns {void }
734
679
*/
735
680
suspendChromedriverProxy ( ) {
736
- this . chromedriver = null ;
681
+ this . chromedriver = undefined ;
737
682
this . proxyReqRes = this . espresso . proxyReqRes . bind ( this . espresso ) ;
738
683
this . proxyCommand = this . espresso . proxyCommand . bind ( this . espresso ) ;
739
684
this . jwpProxyActive = true ;
0 commit comments