{"id":43,"date":"2021-02-12T15:30:00","date_gmt":"2021-02-12T15:30:00","guid":{"rendered":"https:\/\/rudnil.uber.space\/?p=43"},"modified":"2025-02-26T15:35:11","modified_gmt":"2025-02-26T15:35:11","slug":"swipe-gestures-with-selenium-on-android-devices","status":"publish","type":"post","link":"https:\/\/blog.nilsrudolph.de\/?p=43","title":{"rendered":"Swipe gestures with selenium on Android devices"},"content":{"rendered":"\n<p>Testing swipe gestures on android devices is not easy. I will show you how it can be done in this blog post.<\/p>\n\n\n\n<div class=\"wp-block-group is-vertical is-layout-flex wp-container-core-group-is-layout-ef346cd4 wp-block-group-is-layout-flex\" style=\"padding-top:0;padding-bottom:0\">\n<p>There are two things you need to know:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>You need to use the TouchAction Class from the Appium client<\/li>\n\n\n\n<li>You need the coordinated of the native context which differs from the web context<\/li>\n<\/ol>\n<\/div>\n\n\n\n<p>See the following example for swiping from left to right:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"java\" class=\"language-java\">import io.appium.java_client.android.AndroidDriver;\n\nimport static io.appium.java_client.touch.WaitOptions.waitOptions;\nimport static io.appium.java_client.touch.offset.PointOption.point;\nimport static java.time.Duration.ofMillis;\n\npublic class Test {\n\n    public void swipeLeftToRight(AndroidDriver&lt;?&gt; androidDriver) {\n\n        var currentContext = androidDriver.getContext();\n\n        \/\/ We need to switch to the native context to get the real windows size\n        androidDriver.context(\"NATIVE_APP\");\n        var windowsSize = androidDriver.manage().window().getSize();\n\n        \/\/ Avoid starting and ending directly on the edge of the screen\n        int border = 50;\n\n        int startX =  border;\n        int startY = (windowsSize.height \/ 2);\n\n        int endX = windowsSize.width - border;\n        int endY = startY;\n\n        new TouchAction(androidDriver)\n                .press(point(startX, startY))\n                .waitAction(waitOptions(ofMillis(2000)))\n                .moveTo(point(endX, endY))\n                .release()\n                .perform();\n\n        \/\/ Switch back to the web context\n        androidDriver.context(currentContext);\n    }\n}<\/code><\/pre>\n\n\n\n<p>This example just swipes horizontal from left to right over the whole screen. If you need to swipe over a more precise postition (e.g. over a web element), you need to convert the position of the web element to the native screen size. That depends on you kind of screen and on the positiotn and size of the web view.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Testing swipe gestures on android devices is not easy. I will show you how it can be done in this blog post. There are two things you need to know: See the following example for swiping from left to right: This example just swipes horizontal from left to right over the whole screen. If you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-43","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.nilsrudolph.de\/index.php?rest_route=\/wp\/v2\/posts\/43","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.nilsrudolph.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.nilsrudolph.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.nilsrudolph.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.nilsrudolph.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=43"}],"version-history":[{"count":5,"href":"https:\/\/blog.nilsrudolph.de\/index.php?rest_route=\/wp\/v2\/posts\/43\/revisions"}],"predecessor-version":[{"id":48,"href":"https:\/\/blog.nilsrudolph.de\/index.php?rest_route=\/wp\/v2\/posts\/43\/revisions\/48"}],"wp:attachment":[{"href":"https:\/\/blog.nilsrudolph.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.nilsrudolph.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.nilsrudolph.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}