Appium Touch Screen Action in Kotlin
As Kotlin gains more prominence as the de facto language to develop Android apps, so does the Appium based tests developed for these apps, prefer it as the language.
Having been developing test frameworks in Java & PERL for a while, then shifting to Python for a few years; when I finally came back to Java/ Android world at my new organization, I too got into the Kotlin train and it’s been wonderful developing test automation code in it. I feel I got the best of both Java and Python in a way.
But despite this, here & there I did notice some misinformation on Kotlin way of doing things, since the web is so Java full, especially when it comes to the Appium. This one, by the way, I came across while doing research to do screen touch.
I wanted to update the original thread at Appium for this https://discuss.appium.io/t/how-to-use-touchaction-in-kotlin/22567 But there appears to be some issue with their signups, due to all outgoing emails being blocked. I tried to signup, but never received a confirmation email.
So, without any more build up [THE TLDR], you do not need to create a custom touch class as suggested in the thread, instead use Kotlin Appium’s own io.appium.java_client.android.AndroidTouchAction
Sample Code:
import org.openqa.selenium.Dimension
import io.appium.java_client.TouchAction
import io.appium.java_client.android.AndroidTouchAction
import io.appium.java_client.PerformsTouchActions
import io.appium.java_client.touch.offset.PointOption
...
fun clickOnScreen(){
val windowDimension: Dimension = driver.manage().window().getSize()
val width: Int = windowDimension.getWidth()
val height: Int = windowDimension.getHeight()
val touchAction:AndroidTouchAction = AndroidTouchAction(driver as PerformsTouchActions)
touchAction.press(PointOption.point(width/2, height/10)).release().perform()
logger.debug{"Clicked on Top of Screen"}
}
Hope this helps! Thanks.