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

ListensToLogcatMessages.class function startLogcatBroadcast improvement

Open hwulfmeyer opened this issue 5 years ago • 1 comments

The problem

I tried using the 'addLogcatMessagesListener' for the appium Android Driver but it failed with an exception that it can not connect. Only after I looked into the issue more deeply I noticed, that the default port is hardcoded into the startLogcatBroadcast function. I think this is really confusing and unnecessary.

    default void startLogcatBroadcast() {
        this.startLogcatBroadcast("localhost", 4723);
    }

    default void startLogcatBroadcast(String host) {
        this.startLogcatBroadcast(host, 4723);
    }

However, it is possible to use this to not use the default host and port:

    default void startLogcatBroadcast(String host, int port) {
        this.execute("executeScript", ImmutableMap.of("script", "mobile: startLogsBroadcast", "args", Collections.emptyList()));

        URI endpointUri;
        try {
            endpointUri = new URI(String.format("ws://%s:%s/ws/session/%s/appium/device/logcat", host, port, ((RemoteWebDriver)this).getSessionId()));
        } catch (URISyntaxException var5) {
            throw new IllegalArgumentException(var5);
        }

        this.getLogcatClient().connect(endpointUri);
    }

What I do not get is why ListensToLogcatMessages.class doesn't simply use the host and port by the driver calling this class/function? Is there even a point in doing logcatListening to another appium service besides the one used by the driver? Is there a reason why this should not be automated? Imho what it should do is this:

    default void startLogcatBroadcast() {
        URL remoteAdress = this.getRemoteAddress();
        this.startLogcatBroadcast(remoteAdress.getHost(), remoteAdress.getPort());
    }

Environment

  • Appium version (or git revision) that exhibits the issue: 1.17.1
  • Desktop OS/version used to run Appium: Manjaro
  • Mobile platform/version under test: Android
  • Real device or emulator/simulator: emulator
  • Appium CLI or Appium.app|exe: CLI

Details

Code To Reproduce Issue [ Good To Have ]

For testing the addLogcatMessagesListener I took this code: https://github.com/appium/java-client/blob/master/src/test/java/io/appium/java_client/android/AndroidLogcatListenerTest.java

For creating the appium service I used this code (which will always uses any free port it finds):

    public static URL createAppiumService() {
        AppiumServiceBuilder builder = new AppiumServiceBuilder();
        builder.usingAnyFreePort();
        builder.withArgument(GeneralServerFlag.LOG_LEVEL, LogLevel);
        builder.withArgument(GeneralServerFlag.LOG_TIMESTAMP);
        builder.withArgument(GeneralServerFlag.RELAXED_SECURITY);
        appium = AppiumDriverLocalService.buildService(builder);
        appium.start();
        Assert.assertTrue(appium.isRunning(), "Der Appium Server läuft nicht!");
        return appium.getUrl();
    }

The driver can then be initialized like so:

AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(createAppiumService(), _YourDesiredCapsHere_);

hwulfmeyer avatar May 26 '20 12:05 hwulfmeyer

Thanks for the proposal.

Feel free to create a PR

mykola-mokhnach avatar May 26 '20 12:05 mykola-mokhnach