appium / appium-uiautomator2-server

Appium UiAutomator/UiObject2-based server for Android UI automation. This module is used by appium-uiautomator2-driver component
Apache License 2.0
319 stars 227 forks source link

Problem parsing multi-line UiAutomator code. InvalidSelectorException: Could not parse selector expression: UiScrollable has no suitable constructor with arguments #628

Closed coryffaeus closed 3 months ago

coryffaeus commented 3 months ago

Appium server: 2.5.1 Automation driver: uiautomator2@3.0.1 (automationName 'UiAutomator2') Appium client: io.appium:java-client:9.2.2 Env: MacOS Sonoma 14.4.1, Android emulator Pixel 3a API34 extension level 7 arm64-v8a

The code is written in Kotlin like the following, with the intent of using multiline input for better readability:

driver.findElement(
        AppiumBy.androidUIAutomator(
                    """
                    new UiScrollable(
                        new UiSelector()
                            .scrollable(true)
                            .instance(0)
                    ).scrollIntoView(
                        new UiSelector()
                            .text("Frequently asked questions")
                            .instance(0)
                    );
                """.trimIndent()
        )
)

which produces the following exception:

Exception in thread "main" org.openqa.selenium.InvalidSelectorException: Could not parse selector expression `new UiScrollable(
    new UiSelector()
        .scrollable(true)
        .instance(0)
).scrollIntoView(
    new UiSelector().text("Frequently asked questions").instance(0)
)`: UiScrollable has no suitable constructor with arguments [new UiSelector()
        .scrollable(true)
        .instance(0)]

If the code is written in a single line, e.g.:

new UiScrollable(new UiSelector().scrollable(true).instance(0))

the exception is gone.

There was a similar issue already, so I thought I'd add another one. Thanks in advance!

jlipps commented 3 months ago

This may be expected. Any reason you don't want to just strip newlines on your end?

coryffaeus commented 3 months ago

If that's the expected behaviour, then no particular reason not to, of course. Thank you!