How to use TouchAction in Kotlin

Hi everyone,

if have a question: currently I’m switching from Java to Kotlin which works quite well (also thanks to IntelliJs’ conversion). The only thing that does not work are the TouchActions. And I don’t know how to solve it.
My setup is Appium 6.1.0
Java 1.8
Kotlin 1.2.50

Here is some sample code for Java
@Test
public void testInJava() throws MalformedURLException {

        DesiredCapabilities capabilities = new DesiredCapabilities();
        URL url = new URL("http://localhost:4723/wd/hub");
        AppiumDriver driver = new AppiumDriver(url, capabilities);

        TouchAction touchAction = new TouchAction((PerformsTouchActions) driver);
        touchAction.press(PointOption.point(200, 200))
                .waitAction(WaitOptions.waitOptions(Duration.ofMillis(100)))
                .release()
        }

And the new code in Kotlin:
fun testInKotlin() {

        val capabilities = DesiredCapabilities()
        val url = URL("http://localhost:4723/wd/hub")
        val driver = AppiumDriver<MobileElement>(url, capabilities)

        TouchAction(driver as PerformsTouchActions)
                .press(PointOption.point(200, 200))
                .waitAction(WaitOptions.waitOptions(Duration.ofMillis(100)))
                .release()
                .perform()
    }

My problem is that the type inference of Kotlin fails. Something like TouchAction(driver as PerformsTouchActions)… is needed but all attempts for different types failed and are not accepted by IntelliJ.

Do you have a solution or clue what is needed/ missing?

Thanks,
Flo

Okay, we found a solution. We created a class that just contains the following code:

class PlatformTouchAction(performsTouchActions: PerformsTouchActions) : TouchAction<PlatformTouchAction>(performsTouchActions)

Then we use this class instead of the original TouchAction class. An example would be:

PlatformTouchAction(driver as AppiumDriver)
.press(PointOption.point(100, 100))
.waitAction(WaitOptions.waitOptions(Duration.ofMillis(1000)))
.release()
.perform()
1 Like

Hi Fio,

I’m curious to know why you decide to move to Kotlin from Java ?

i know lot of Android developer are shifting to Kotlin but would love to hear reasons from your side as SDET requirements.

Regards,
Vikram

Hi Vikram,

in our team the Android developers switched from Java to Kotlin last year. So I was curious :slight_smile:
One reasonfor me to switch to Kotlin was that we use the factory pattern in many places. This makes it possible for us to use one code base and the same tests for Android and iOS and all countries we support and test. So we only need to set the os in the beginning and don’t need to care about the os in the later steps. The problem is that (i.m.o.) the factory pattern is very complicated in Java (you need the classes per os/ country, the parent class and the factory class) so I searched for an alternative. In Kotlin the factory pattern is pretty simple to create (you only need the os/country classes and one interface which contains the structure and a companion object which enables the factory pattern). This makes the test code more readable and maintainable. Also other code parts are (i.m.o.) much better readable in Kotlin e.g. enums, the inheritance in the page objects, …
That were the main reasons for me to switch to Kotlin. And it’s pretty easy to switch with IntelliJ. The conversion of 90% of the whole code base needed a few days.

Regards
Flo

1 Like

Hey Flo,

Thanks for detailed insights; it is quite interesting learnings.

Regards,
Vikram

I found this issue today and still can’t make it work, when I use your technique it never imports the class no matter how many times I try to import it.

my (dirty) solution was to implement a util class in java :frowning:

Hi,

here are two screenshots of how I implemented it. First the PlatformTouchAction:


And here how I use it in the code:

Maybe this helps a litte bit?

1 Like

OK, lesson learned here, package names matter :slight_smile:
Just needed to move the file around and use the IDE’s helper to create the class and got the package right.

Thanks!