# How to use TouchAction in Kotlin

**URL:** <https://discuss.appium.io/t/how-to-use-touchaction-in-kotlin/22567>\
**Category:** Support\
**Created:** [June 26, 2018, 10:32am UTC](https://discuss.appium.io/t/how-to-use-touchaction-in-kotlin/22567 "2018-06-26T10:32:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fpilz82](https://discuss.appium.io/letter_avatar_proxy/v4/letter/f/439d5e/32.png) [@fpilz82](https://discuss.appium.io/u/fpilz82)\
**Post date:** [June 26, 2018, 10:32am UTC](https://discuss.appium.io/t/how-to-use-touchaction-in-kotlin/22567/1 "2018-06-26T10:32:21Z")

</div>

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.

 ![Appium_Kotlin](https://discuss.appium.io/uploads/default/original/2X/6/6eaaa9a71ba24749203e7d689667c0c270dd25b7.png)

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

Thanks,  
Flo

---

<div class="post-metadata">

**Author:** ![fpilz82](https://discuss.appium.io/letter_avatar_proxy/v4/letter/f/439d5e/32.png) [@fpilz82](https://discuss.appium.io/u/fpilz82)\
**Post date:** [June 28, 2018, 8:26am UTC](https://discuss.appium.io/t/how-to-use-touchaction-in-kotlin/22567/2 "2018-06-28T08:26:12Z")

</div>

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()
```

---

<div class="post-metadata">

**Author:** ![VikramVI](https://discuss.appium.io/user_avatar/discuss.appium.io/vikramvi/32/116_2.png) [@VikramVI](https://discuss.appium.io/u/VikramVI)\
**Post date:** [June 28, 2018, 10:15am UTC](https://discuss.appium.io/t/how-to-use-touchaction-in-kotlin/22567/3 "2018-06-28T10:15:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fpilz82](https://discuss.appium.io/letter_avatar_proxy/v4/letter/f/439d5e/32.png) [@fpilz82](https://discuss.appium.io/u/fpilz82)\
**Post date:** [June 28, 2018, 12:03pm UTC](https://discuss.appium.io/t/how-to-use-touchaction-in-kotlin/22567/4 "2018-06-28T12:03:09Z")

</div>

Hi Vikram,

in our team the Android developers switched from Java to Kotlin last year. So I was curious 🙂  
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

---

<div class="post-metadata">

**Author:** ![VikramVI](https://discuss.appium.io/user_avatar/discuss.appium.io/vikramvi/32/116_2.png) [@VikramVI](https://discuss.appium.io/u/VikramVI)\
**Post date:** [June 28, 2018, 12:46pm UTC](https://discuss.appium.io/t/how-to-use-touchaction-in-kotlin/22567/5 "2018-06-28T12:46:47Z")

</div>

Hey Flo,

Thanks for detailed insights; it is quite interesting learnings.

Regards,  
Vikram

---

<div class="post-metadata">

**Author:** ![jpita](https://discuss.appium.io/user_avatar/discuss.appium.io/jpita/32/4385_2.png) [@jpita](https://discuss.appium.io/u/jpita)\
**Post date:** [July 30, 2018, 3:48pm UTC](https://discuss.appium.io/t/how-to-use-touchaction-in-kotlin/22567/6 "2018-07-30T15:48:39Z")

</div>

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 ☹

---

<div class="post-metadata">

**Author:** ![fpilz82](https://discuss.appium.io/letter_avatar_proxy/v4/letter/f/439d5e/32.png) [@fpilz82](https://discuss.appium.io/u/fpilz82)\
**Post date:** [July 30, 2018, 8:35pm UTC](https://discuss.appium.io/t/how-to-use-touchaction-in-kotlin/22567/7 "2018-07-30T20:35:17Z")

</div>

Hi,

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

 ![14](https://discuss.appium.io/uploads/default/original/2X/0/087f451e4a38b27eb03be1467483f3ce678b4a9c.png)  
And here how I use it in the code:  
 ![26](https://discuss.appium.io/uploads/default/original/2X/d/d2470762086f7e72a8ab885aef2b437ab0225502.png)  
Maybe this helps a litte bit?

---

<div class="post-metadata">

**Author:** ![jpita](https://discuss.appium.io/user_avatar/discuss.appium.io/jpita/32/4385_2.png) [@jpita](https://discuss.appium.io/u/jpita)\
**Post date:** [July 31, 2018, 12:37pm UTC](https://discuss.appium.io/t/how-to-use-touchaction-in-kotlin/22567/8 "2018-07-31T12:37:56Z")

</div>

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

Thanks!
