appium / appium-uiautomator2-driver

Appium driver for Android UIAutomator2
Apache License 2.0
607 stars 177 forks source link

`mobile: stopService` throws WebDriverException #792

Closed nichiyoshi closed 5 months ago

nichiyoshi commented 5 months ago

Issue

I am trying to stop the service of io.appium.settings/.LocationService using the executeScript("mobile: stopService"), but it throws WebDriverException as follows.

Code

val params = mapOf(
        "intent" to "io.appium.settings/.LocationService"
    )

runCatching { appiumDriver.executeScript("mobile: stopService", params) as String }
            .fold(
                onSuccess = { result -> CommandResult.Success(result) },
                onFailure = { exception -> CommandResult.Exception(exception) }
            )

Exception log

org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. Original error: Error executing adbExec. Original error: 'Command 'path-to-adb -P 5037 shell am stop-service io.appium.settings/.LocationService' exited with code 255'; Command output: Service stopped

This also happens when I execute the same with executeScript("mobile: shell")

Code

val params =
          mapOf(
              "command" to "am stopservice io.appium.settings/.LocationService",
          )

return runCatching { appiumDriver.executeScript("mobile: shell", params) as String }
  .fold(
      onSuccess = { result -> CommandResult.Success(result) },
      onFailure = { exception -> CommandResult.Exception(exception) }
  )

Exception log

org.openqa.selenium.WebDriverException: An unknown server-side error occurred while processing the command. Original error: Cannot execute the 'am stopservice io.appium.settings/.LocationService' shell command. Original error: Command 'path-to-adb -P 5037 shell 'am stopservice io.appium.settings/.LocationService'' exited with code 255. StdOut: Stopping service: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=io.appium.settings/.LocationService }
. StdErr: Service stopped

If I just run adb command from terminal, it succeeds. -> It turned out it was returning 255, and the message was output to inputstream of the Process, not the errorStream

% adb shell am stopservice io.appium.settings/.LocationService 
Stopping service: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=io.appium.settings/.LocationService }
Service stopped
KazuCocoa commented 5 months ago

What was the exit code in the adb shell?

It looks like https://github.com/appium/appium-android-driver/blob/7532ad4d1680b1dd661b55bd3958b6205c47c7f3/lib/commands/intent.js#L155-L166 should handle if the command succeeds by checking the output

mykola-mokhnach commented 5 months ago

I assume it's an issue with the adb itself. Ideally it should return the exit code 0 in case of a successful response. Let me add a a workaround for now :(

nichiyoshi commented 5 months ago

I assume it's an issue with the adb itself. Ideally it should return the exit code 0 in case of a successful response. Let me add a a workaround for now :(

Thank you for the quick workaround to fix this! Yes, it seems the issue is of the adb itself. According to logs, I could confirm that it started to happen around the 7th June.

KazuCocoa commented 5 months ago

UIA2 driver 3.5.4 has the fix