`shouldAvoidProxy` Method Not Working in Custom Appium Plugin

Description

I am developing a custom Appium plugin similar to appium-altunity-plugin. In my plugin, I have implemented a custom context and expect that when switching to this context, related element operation requests will not be proxied to the uiautomator2 driver. However, I found that the shouldAvoidProxy method does not seem to be working as expected, and the requests are still being proxied to uiautomator2.

Environment

Appium Version: 2.11.3
Node.js Version: v18.14.1

Code

import { BasePlugin } from 'appium/plugin';
import type { NextHandler } from './types';
// import AltUnityClient, { AltBy, AltElement, AltKeyCode } from './sample_client';
import MyCustomClient from './client';

import { getContexts, getCurrentContext, setContext, findElOrEls, click, getText, getAttribute, execute } from "./commands";

import { ExternalDriver } from '@appium/types';

const EXTRA_CAPS = {
    DriverPort: { isNumber: true },
    DriverHost: { isString: true }
};

export type Size = {
    width: number,
    height: number,
}

export const MyCustom_CONTEXT = 'MyCustom';

export const enum Platform {
    Mac = 'mac',
    Windows = 'windows',
    Linux = 'linux',
    iOS = 'ios',
    Android = 'android',
}

class MyCustomPlugin extends BasePlugin {

    client: MyCustomClient;
    platform?: Platform;
    isInMyCustomContext: boolean;
    getContexts = getContexts
    getCurrentContext = getCurrentContext
    setContext = setContext
    click = click
    getText = getText
    getAttribute = getAttribute
    findElOrEls = findElOrEls
    execute = execute


    constructor(name: string) {
        super(name);
        this.isInMyCustomContext = false;
    }

    shouldAvoidProxy(method: string, route: string) {

        this.logger.info(`shouldAvoidProxy: ${method} ${route}`)
        if (this.isInMyCustomContext) {
            return true;
        }
        if (route.match(/\/contexts?$/)) {
            return true;
        }
        return false;
    }

    async createSession(next: NextHandler, driver: ExternalDriver) {
        driver.desiredCapConstraints = { ...driver.desiredCapConstraints, ...EXTRA_CAPS };
        this.logger.info(`start session frm MyCustom`)


        // run the actual driver's create session now create session is weird and returns webdriver
        // protocol response, not a raw result, so we have to handle that
        const { value, error } = await next();
        if (error) {
            throw error;
        }
        const [id, caps] = value;
        this.platform = this.getPlatform(caps.platformName);

        // instantiate our unity client
        const { DriverHost, DriverPort } = caps;
        this.client = new MyCustomClient(this.logger, DriverHost, DriverPort);
        // await this.client.start();

        return [id, caps];
    }

    getPlatform(platformName: string) {
        switch (platformName.toLowerCase()) {
            case 'mac': return Platform.Mac;
            case 'windows': return Platform.Windows;
            case 'linux': return Platform.Linux;
            case 'ios': return Platform.iOS;
            case 'android': return Platform.Android;
            default: throw new Error(`Can't find platform for '${platformName}'`);
        }
    }



    async deleteSession(next: NextHandler) {
        try {
            await this.client.stop();
        } finally {
            await next();
        }
    }
}

logs

[Appium] Attempting to load plugin myCustom...
[Appium] Welcome to Appium v2.12.0
[Appium] Non-default server args:
[Appium] {
  loglevel: 'info',
  usePlugins: [
    'myCustom'
  ]
}
[Appium] Attempting to load driver uiautomator2...
[Appium] Appium REST http interface listener started on http://0.0.0.0:4723
[Appium] You can provide the following URLs in your client code to connect to this server:
        http://127.0.0.1:4723/ (only accessible from the same host)
        http://30.211.153.93:4723/
[Appium] Available drivers:
[Appium]   - [email protected] (automationName 'UiAutomator2')
[Appium] Available plugins:
[Appium]   - [email protected] (ACTIVE)
[HTTP] --> POST /session {"capabilities":{"alwaysMatch":{"platformName":"Android","appium:automationName":"UiAutomator2","appium:myCustomDriverHost":"127.0.0.1","appium:myCustomDriverPort":13000},"firstMatch":[{}]},"desiredCapabilities":{"platformName":"Android","appium:automationName":"UiAutomator2","appium:myCustomDriverHost":"127.0.0.1","appium:myCustomDriverPort":13000}}
[AppiumDriver@6572] Plugins which can handle cmd 'createSession': myCustom
[AppiumDriver@6572] Plugin myCustom is now handling cmd 'createSession'
[Plugin [myCustom]] start session frm myCustomx
[AppiumDriver@6572] Executing default handling behavior for command 'createSession'
[Appium] Attempting to find matching driver for automationName 'UiAutomator2' and platformName 'Android'
[Appium] The 'uiautomator2' driver was installed and matched caps.
[Appium] Will require it at /Users/cc/Documents/code/naja/appium2/appium-myCustom-plugin/node_modules/appium-uiautomator2-driver
[AppiumDriver@6572] Appium v2.12.0 creating new AndroidUiautomator2Driver (v2.29.2) session
[AppiumDriver@6572] Checking BaseDriver versions for Appium and AndroidUiautomator2Driver
[AppiumDriver@6572] Appium's BaseDriver version is 9.12.0
[AppiumDriver@6572] AndroidUiautomator2Driver's BaseDriver version is 9.12.0
[AndroidUiautomator2Driver@5d3d] The following provided capabilities were not recognized by this driver:
[AndroidUiautomator2Driver@5d3d]   myCustomDriverHost
[AndroidUiautomator2Driver@5d3d]   myCustomDriverPort
[30abc46c][AndroidUiautomator2Driver@5d3d] Session created with session id: 30abc46c-a30b-4b0c-b31e-501d66ff846a
[30abc46c][AndroidUiautomator2Driver@5d3d] Neither 'app' nor 'appPackage' was set. Starting UiAutomator2 without the target application
[30abc46c][ADB] Found 2 'build-tools' folders under '/Users/cc/Library/Android/sdk' (newest first):
[30abc46c][ADB]     /Users/cc/Library/Android/sdk/build-tools/33.0.2
[30abc46c][ADB]     /Users/cc/Library/Android/sdk/build-tools/30.0.3
[30abc46c][ADB] Using 'adb' from '/Users/cc/Library/Android/sdk/platform-tools/adb'
[30abc46c][AndroidDriver] Retrieving device list
[30abc46c][AndroidDriver] Using device: 30.103.233.139:24009
[30abc46c][ADB] Using 'adb' from '/Users/cc/Library/Android/sdk/platform-tools/adb'
[30abc46c][ADB] Getting device platform version
[30abc46c][AndroidUiautomator2Driver@5d3d] Relaxing hidden api policy
[30abc46c][AndroidDriver] No app sent in, not parsing package/activity
[30abc46c][ADB] Using 'apksigner.jar' from '/Users/cc/Library/Android/sdk/build-tools/33.0.2/lib/apksigner.jar'
[30abc46c][ADB] '/Users/cc/Documents/code/naja/appium2/appium-myCustomui-plugin/node_modules/appium-uiautomator2-driver/node_modules/appium-uiautomator2-server/apks/appium-uiautomator2-server-debug-androidTest.apk' is signed with the default certificate
[30abc46c][ADB] '/Users/cc/Documents/code/naja/appium2/appium-myCustomui-plugin/node_modules/appium-uiautomator2-driver/node_modules/appium-uiautomator2-server/apks/appium-uiautomator2-server-v5.12.2.apk' is signed with the default certificate
[30abc46c][AndroidUiautomator2Driver@5d3d] Server packages are not going to be (re)installed
[30abc46c][ADB] Adding packages ["io.appium.settings","io.appium.uiautomator2.server","io.appium.uiautomator2.server.test"] to Doze whitelist
[30abc46c][AndroidUiautomator2Driver@5d3d] Starting UIAutomator2 server 5.12.2
[30abc46c][AndroidUiautomator2Driver@5d3d] Using UIAutomator2 server from '/Users/cc/Documents/code/naja/appium2/appium-myCustomui-plugin/node_modules/appium-uiautomator2-driver/node_modules/appium-uiautomator2-server/apks/appium-uiautomator2-server-v5.12.2.apk' and test from '/Users/cc/Documents/code/naja/appium2/appium-myCustomui-plugin/node_modules/appium-uiautomator2-driver/node_modules/appium-uiautomator2-server/apks/appium-uiautomator2-server-debug-androidTest.apk'
[30abc46c][AndroidUiautomator2Driver@5d3d] Waiting up to 30000ms for UiAutomator2 to be online...
[30abc46c][AndroidUiautomator2Driver@5d3d] socket hang up
[30abc46c][AndroidUiautomator2Driver@5d3d] socket hang up
[30abc46c][AndroidUiautomator2Driver@5d3d] socket hang up
[30abc46c][AndroidUiautomator2Driver@5d3d] Determined the downstream protocol as 'W3C'
[30abc46c][AndroidDriver] Screen already unlocked, doing nothing
[30abc46c][AppiumDriver@6572] New AndroidUiautomator2Driver session created successfully, session 30abc46c-a30b-4b0c-b31e-501d66ff846a added to master session list
[30abc46c][AppiumDriver@6572] Promoting 1 sessionless plugins to be attached to session ID 30abc46c-a30b-4b0c-b31e-501d66ff846a
[30abc46c][HTTP] <-- POST /session 200 8634 ms - 831 
[30abc46c][HTTP] --> GET /session/30abc46c-a30b-4b0c-b31e-501d66ff846a/contexts {}
[30abc46c][AppiumDriver@6572] Plugins which can handle cmd 'getContexts': myCustom
[30abc46c][AppiumDriver@6572] Plugin myCustom is now handling cmd 'getContexts'
[30abc46c][Plugin [myCustom]] 第一个插件getContexts
[30abc46c][AppiumDriver@6572] Executing default handling behavior for command 'getContexts'
[30abc46c][HTTP] <-- GET /session/30abc46c-a30b-4b0c-b31e-501d66ff846a/contexts 200 115 ms - 34 
[30abc46c][HTTP] --> POST /session/30abc46c-a30b-4b0c-b31e-501d66ff846a/context {"name":"myCustomX"}
[30abc46c][AppiumDriver@6572] Plugins which can handle cmd 'setContext': myCustom
[30abc46c][AppiumDriver@6572] Plugin myCustom is now handling cmd 'setContext'
[30abc46c][Plugin [myCustom]] lizhicai
myCustomX
[30abc46c][AppiumDriver@6572] Command 'setContext' was *not* handled by the following behaviours or plugins, even though they were registered to handle it: ["default"]. The command *was* handled by these: ["myCustom"].
[30abc46c][HTTP] <-- POST /session/30abc46c-a30b-4b0c-b31e-501d66ff846a/context 200 5 ms - 14 
[30abc46c][HTTP] --> GET /session/30abc46c-a30b-4b0c-b31e-501d66ff846a/context {}
[30abc46c][AppiumDriver@6572] Plugins which can handle cmd 'getCurrentContext': myCustom
[30abc46c][AppiumDriver@6572] Plugin myCustom is now handling cmd 'getCurrentContext'
getCurrentContext: returning myCustomX_CONTEXT
true
[30abc46c][AppiumDriver@6572] Command 'getCurrentContext' was *not* handled by the following behaviours or plugins, even though they were registered to handle it: ["default"]. The command *was* handled by these: ["myCustom"].
[30abc46c][HTTP] <-- GET /session/30abc46c-a30b-4b0c-b31e-501d66ff846a/context 200 1 ms - 19 
[30abc46c][HTTP] --> GET /session/30abc46c-a30b-4b0c-b31e-501d66ff846a/context {}
[30abc46c][AppiumDriver@6572] Plugins which can handle cmd 'getCurrentContext': myCustom
[30abc46c][AppiumDriver@6572] Plugin myCustom is now handling cmd 'getCurrentContext'
getCurrentContext: returning myCustomX_CONTEXT
true
[30abc46c][AppiumDriver@6572] Command 'getCurrentContext' was *not* handled by the following behaviours or plugins, even though they were registered to handle it: ["default"]. The command *was* handled by these: ["myCustom"].
[30abc46c][HTTP] <-- GET /session/30abc46c-a30b-4b0c-b31e-501d66ff846a/context 200 1 ms - 19 
[30abc46c][HTTP] --> POST /session/30abc46c-a30b-4b0c-b31e-501d66ff846a/element {"using":"xpath","value":"//ui_cheat"}
[30abc46c][AndroidUiautomator2Driver@5d3d] Got response with status 404: {"sessionId":"818f9285-b93f-4d8a-a88b-d5623e47a88f","value":{"error":"no such element","message":"An element could not be located on the page using the given search parameters","stacktrace":"io.appium.uiautomator2.common.exceptions.ElementNotFoundException: An element could not be located on the page using the given search parameters\n\tat io.appium.uiautomator2.utils.ElementLocationHelpers.findElement(ElementLocationHelpers.java:156)\n\tat io.appium.uiautomator2.handler.FindElement.safeHandle(FindElement.java:60)\n\tat io.appium.uiautomator2.handler.request.SafeRequestHandler.handle(SafeRequestHandler.java:59)\n\tat io.appium.uiautomator2.server.AppiumServlet.handleRequest(AppiumServlet.java:259)\n\tat io.appium.uiautomator2.server.AppiumServlet.handleHttpRequest(AppiumServlet.java:253)\n\tat io.appium.uiautomator2.http.ServerHandler.channelRead(ServerHandler.java:77)\n\tat io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366)\n\tat io.netty.channel.Abstr...
[30abc46c][HTTP] <-- POST /session/30abc46c-a30b-4b0c-b31e-501d66ff846a/element 404 802 ms - 914 
[30abc46c][HTTP] --> POST /session/30abc46c-a30b-4b0c-b31e-501d66ff846a/elements {"using":"xpath","value":"//ui_cheat"}
[30abc46c][HTTP] <-- POST /session/30abc46c-a30b-4b0c-b31e-501d66ff846a/elements 200 134 ms - 12 
[30abc46c][HTTP] --> POST /session/30abc46c-a30b-4b0c-b31e-501d66ff846a/elements {"using":"xpath","value":"//ui_cheat"}
[30abc46c][HTTP] <-- POST /session/30abc46c-a30b-4b0c-b31e-501d66ff846a/elements 200 187 ms - 12 
[30abc46c][HTTP] --> POST /session/30abc46c-a30b-4b0c-b31e-501d66ff846a/elements {"using":"xpath","value":"//ui_cheat"}
[30abc46c][HTTP] <-- POST /session/30abc46c-a30b-4b0c-b31e-501d66ff846a/elements 200 207 ms - 12 
[30abc46c][HTTP] --> POST /session/30abc46c-a30b-4b0c-b31e-501d66ff846a/elements {"using":"xpath","value":"//ui_cheat"}
[30abc46c][HTTP] <-- POST /session/30abc46c-a30b-4b0c-b31e-501d66ff846a/elements 200 197 ms - 12 
[30abc46c][HTTP] --> POST /session/30abc46c-a30b-4b0c-b31e-501d66ff846a/elements {"using":"xpath","value":"//ui_cheat"}
[30abc46c][HTTP] <-- POST /session/30abc46c-a30b-4b0c-b31e-501d66ff846a/elements 200 176 ms - 12 
[30abc46c][HTTP] --> POST /session/30abc46c-a30b-4b0c-b31e-501d66ff846a/elements {"using":"xpath","value":"//ui_cheat"}
[30abc46c][HTTP] <-- POST /session/30abc46c-a30b-4b0c-b31e-501d66ff846a/elements 200 114 ms - 12 
[30abc46c][HTTP] --> POST /session/30abc46c-a30b-4b0c-b31e-501d66ff846a/elements {"using":"xpath","value":"//ui_cheat"}
[30abc46c][HTTP] --> DELETE /session/30abc46c-a30b-4b0c-b31e-501d66ff846a {}
[30abc46c][AppiumDriver@6572] Plugins which can handle cmd 'deleteSession': myCustom
[30abc46c][AppiumDriver@6572] Plugin myCustom is now handling cmd 'deleteSession'
[30abc46c][Plugin [myCustom]] Ending myCustomuidriver sesson first
[30abc46c][HTTP] <-- POST /session/30abc46c-a30b-4b0c-b31e-501d66ff846a/elements 200 187 ms - 12 
[30abc46c][AndroidUiautomator2Driver@5d3d] Shutting down because we waited 60 seconds for a command
[30abc46c][AppiumDriver@6572] Ending session, cause was 'New Command Timeout of 60 seconds expired. Try customizing the timeout using the 'newCommandTimeout' desired capability'
[30abc46c][AppiumDriver@6572] Removing session '30abc46c-a30b-4b0c-b31e-501d66ff846a' from our master session list
[30abc46c][AndroidUiautomator2Driver@5d3d] socket hang up
[30abc46c][AndroidUiautomator2Driver@5d3d] Did not get confirmation UiAutomator2 deleteSession worked; Error was: UnknownError: An unknown server-side error occurred while processing the command. Original error: Could not proxy command to the remote server. Original error: socket hang up
[30abc46c][AndroidUiautomator2Driver@5d3d] Restoring hidden api policy to the device default configuration
[HTTP] <-- DELETE /session/30abc46c-a30b-4b0c-b31e-501d66ff846a - - ms