6
6
TEST_APK_PATH as testApkPath ,
7
7
version as serverVersion
8
8
} from 'appium-uiautomator2-server' ;
9
- import { util , logger , timing } from 'appium/support' ;
9
+ import { util , timing } from 'appium/support' ;
10
10
import B from 'bluebird' ;
11
11
import axios from 'axios' ;
12
12
@@ -17,7 +17,6 @@ const SERVICES_LAUNCH_TIMEOUT = 30000;
17
17
const SERVER_PACKAGE_ID = 'io.appium.uiautomator2.server' ;
18
18
const SERVER_TEST_PACKAGE_ID = `${ SERVER_PACKAGE_ID } .test` ;
19
19
const INSTRUMENTATION_TARGET = `${ SERVER_TEST_PACKAGE_ID } /androidx.test.runner.AndroidJUnitRunner` ;
20
- const instrumentationLogger = logger . getLogger ( 'Instrumentation' ) ;
21
20
22
21
class UIA2Proxy extends JWProxy {
23
22
/** @type {boolean } */
@@ -50,6 +49,9 @@ class UiAutomator2Server {
50
49
/** @type {boolean|undefined } */
51
50
disableSuppressAccessibilityService ;
52
51
52
+ /** @type {import('teen_process').SubProcess|null } */
53
+ instrumentationProcess ;
54
+
53
55
constructor ( log , opts = { } ) {
54
56
for ( let req of REQD_PARAMS ) {
55
57
if ( ! opts || ! util . hasValue ( opts [ req ] ) ) {
@@ -72,6 +74,7 @@ class UiAutomator2Server {
72
74
this . proxyReqRes = this . jwproxy . proxyReqRes . bind ( this . jwproxy ) ;
73
75
this . proxyCommand = this . jwproxy . command . bind ( this . jwproxy ) ;
74
76
this . jwproxy . didInstrumentationExit = false ;
77
+ this . instrumentationProcess = null ;
75
78
}
76
79
77
80
/**
@@ -255,6 +258,9 @@ class UiAutomator2Server {
255
258
while ( retries < maxRetries ) {
256
259
this . log . info ( `Waiting up to ${ timeout } ms for UiAutomator2 to be online...` ) ;
257
260
this . jwproxy . didInstrumentationExit = false ;
261
+ try {
262
+ await this . stopInstrumentationProcess ( ) ;
263
+ } catch ( ign ) { }
258
264
await this . startInstrumentationProcess ( ) ;
259
265
if ( ! this . jwproxy . didInstrumentationExit ) {
260
266
try {
@@ -312,18 +318,30 @@ class UiAutomator2Server {
312
318
// Disable Google analytics to prevent possible fatal exception
313
319
cmd . push ( '-e' , 'disableAnalytics' , 'true' ) ;
314
320
cmd . push ( INSTRUMENTATION_TARGET ) ;
315
- const instrumentationProcess = this . adb . createSubProcess ( [ 'shell' , ...cmd ] ) ;
316
- instrumentationProcess . on ( 'output' , ( stdout , stderr ) => {
317
- const output = _ . trim ( stdout || stderr ) ;
318
- if ( output ) {
319
- instrumentationLogger . debug ( output ) ;
320
- }
321
- } ) ;
322
- instrumentationProcess . on ( 'exit' , ( code ) => {
323
- instrumentationLogger . debug ( `The process has exited with code ${ code } ` ) ;
321
+ this . instrumentationProcess = this . adb . createSubProcess ( [ 'shell' , ...cmd ] ) ;
322
+ for ( const streamName of [ 'stderr' , 'stdout' ] ) {
323
+ this . instrumentationProcess . on ( `line-${ streamName } ` , ( line ) => this . log . debug ( `[Instrumentation] ${ line } ` ) ) ;
324
+ }
325
+ this . instrumentationProcess . once ( 'exit' , ( code , signal ) => {
326
+ this . log . debug ( `[Instrumentation] The process has exited with code ${ code } , signal ${ signal } ` ) ;
324
327
this . jwproxy . didInstrumentationExit = true ;
325
328
} ) ;
326
- await instrumentationProcess . start ( 0 ) ;
329
+ await this . instrumentationProcess . start ( 0 ) ;
330
+ }
331
+
332
+ async stopInstrumentationProcess ( ) {
333
+ if ( ! this . instrumentationProcess ) {
334
+ return ;
335
+ }
336
+
337
+ try {
338
+ if ( this . instrumentationProcess . isRunning ) {
339
+ await this . instrumentationProcess . stop ( ) ;
340
+ }
341
+ } finally {
342
+ this . instrumentationProcess . removeAllListeners ( ) ;
343
+ this . instrumentationProcess = null ;
344
+ }
327
345
}
328
346
329
347
async deleteSession ( ) {
@@ -336,6 +354,11 @@ class UiAutomator2Server {
336
354
this . log . warn ( `Did not get confirmation UiAutomator2 deleteSession worked; ` +
337
355
`Error was: ${ err } ` ) ;
338
356
}
357
+ try {
358
+ await this . stopInstrumentationProcess ( ) ;
359
+ } catch ( err ) {
360
+ this . log . warn ( `Could not stop the instrumentation process. Original error: ${ err . message } ` ) ;
361
+ }
339
362
}
340
363
341
364
async cleanupAutomationLeftovers ( strictCleanup = false ) {
0 commit comments