java-client icon indicating copy to clipboard operation
java-client copied to clipboard

bug: unable to intercept click action using appium proxy

Open Jayarams99 opened this issue 1 year ago • 3 comments

Do I have the most recent component updates?

  • [X] I use the most recent available driver/plugin and server versions

Is the component officially supported by the Appium team?

  • [X] I have verified the component repository is present under the Appium organization in GitHub

Is there an existing issue for this?

  • [X] I have searched the existing issues

Current Behavior

This issue is related to https://github.com/appium/java-client/blob/master/docs/The-event_firing.md .

The proxy method is not able to intercept click action, only driver based methods are intercepted

Expected Behavior

Please provide a fix or documentation on how to intercept webelement method calls

Minimal Reproducible Example

MethodCallListener listener = new MethodCallListener() {
    @Override
    public void beforeCall(Object target, Method method, Object[] args) {
        if (method.getName().equals("execute")) {
            System.out.println(args[0].tostring());
        }
  

 
};

IOSDriver decoratedDriver = createProxy(
        IOSDriver.class,
        new Object[] {new URL("http://localhost:4723/"), capabilities},
        new Class[] {URL.class, Capabilities.class},
        listener
);

decoratedDriver.findElement(Appium.id("id")).click();

Environment

  • Operating system:
  • Appium server version (output of appium --version):
  • Appium driver(s) and their version(s) (appium driver list):
  • Appium plugin(s) and their version(s) (appium plugin list):
  • Node.js version (output of node --version):
  • npm version (output of npm --version):
  • Last component(s) version which did not exhibit the problem:
  • Platform and version under test:
  • Real device or emulator/simulator:

Link to Appium Logs

No response

Further Information

No response

Jayarams99 avatar Oct 04 '24 15:10 Jayarams99

It is only possible to wrap instances with proxies upon creation. And since WebElement instances creation is incapsulated in the driver the above isn't possible.

The only workaround would be to intercept the driver's execute method as eventually all WebElement's method calls are routed there.

mykola-mokhnach avatar Oct 05 '24 14:10 mykola-mokhnach

I tried to intercept execute call but it is not showing the click calls. Please find the code snippet for the same in initial bug details

Jayarams99 avatar Oct 14 '24 17:10 Jayarams99

I face the same issue, The execute method is a protected method. It will not be intercepted by proxy. You can create a custom driver that extends the origin one. Override the execute method and make it public.

ZhongGuan avatar Oct 28 '24 01:10 ZhongGuan

One option is to proxy the RemoteWebElement. It's not a convenient solution, but it works if you want to intercept selected calls.

package org.example;

import io.appium.java_client.AppiumBy;

import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;

import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.ios.options.XCUITestOptions;
import io.appium.java_client.proxy.MethodCallListener;
import io.appium.java_client.remote.AutomationName;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebElement;

import static io.appium.java_client.proxy.Helpers.createProxy;

public class IOSDecoratorTest {
    public static void main(String[] args) throws MalformedURLException, InterruptedException {
      
        XCUITestOptions options = new XCUITestOptions()
                .setPlatformName("iOS")
                .setDeviceName("iPhone 16 Pro")
                .setPlatformVersion("18.5")
                .setAutomationName(AutomationName.IOS_XCUI_TEST)
                .setApp("/path/to/TestApp-iphonesimulator.app");

        // Create listener to log method calls
        MethodCallListener listener = new MethodCallListener() {
            @Override
            public void beforeCall(Object target, Method method, Object[] args) {
                if (method.getName().contains("click")) {
                    System.out.println("→ " + method.getName() + "(" + (args.length > 0 ? args[0] : "") + ")");
                    }
            }
        };

    //    IOSDriver driver = new IOSDriver(new URL("http://localhost:4723/"), options);

        IOSDriver decoratedDriver = createProxy(
                IOSDriver.class,
                new Object[]{new URL("http://localhost:4723/"), options},
                new Class[] {URL.class, Capabilities.class},
                listener
        );

        System.out.println("App launched!");

        WebElement element = decoratedDriver.findElement(AppiumBy.accessibilityId("show alert"));
        
        RemoteWebElement proxied = createProxy(
                RemoteWebElement.class,
                new Object[]{},
                new Class[] {},
                listener
        );

        proxied.setParent(decoratedDriver);
        proxied.setId(((RemoteWebElement)element).getId());
        proxied.click();

        decoratedDriver.quit();

    }
}

pujagani avatar Jun 30 '25 14:06 pujagani

Can you please provid an example for the fix.

Thanks Jayaram

On Thu, Jul 3, 2025, 13:47 Valery Yatsynovich @.***> wrote:

Closed #2239 https://github.com/appium/java-client/issues/2239 as completed via #2311 https://github.com/appium/java-client/pull/2311.

— Reply to this email directly, view it on GitHub https://github.com/appium/java-client/issues/2239#event-18447652962, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHOELI2VPKEL2BN75UPOG2L3GTRLDAVCNFSM6AAAAACAOLQ3CCVHI2DSMVQWIX3LMV45UABCJFZXG5LFIV3GK3TUJZXXI2LGNFRWC5DJN5XDWMJYGQ2DONRVGI4TMMQ . You are receiving this because you authored the thread.Message ID: @.***>

Jayarams99 avatar Jul 03 '25 15:07 Jayarams99

Please refer to https://github.com/appium/java-client/blob/master/docs/The-event_firing.md#elementawarewebdriverlistener

pujagani avatar Jul 04 '25 04:07 pujagani