How to automate scrolling feature in IOS/Android using Appium tool | Mobile Testing
How to automate scrolling feature in Mobiles( IOS/Android) using Appium tool for mobile testing?
Initially, Appium tool developers came up with below two methods to scroll down to the particular element in Mobiles
driver.scrollTo(String text); and driver.scrollToExact(String text);
But however, they have deprecated this feature from Version.
So with this depreciation, automating scrolling functionality with appium turns to be tedious task,
Below is the simple code that helps to scroll using Appium in both android and iOS mobile application testing- It scrolls until an element with XPath is found.
Note: Keep in mind for iOS is that the XPath should also contain visible property along.
Use swipe method Keep start x and end x value constant and change start y and end y values to scroll up or down
swipe Method Syntax –
swipe(intstartx, intstarty, intendx, intendy, int duration) – Convenience method for swiping across the screen.
Below is the code to scroll till element present in the particular page
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
import java.net.MalformedURLException; import java.net.URL; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.Dimension; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebElement; import org.openqa.selenium.remote.DesiredCapabilities; import io.appium.java_client.android.AndroidDriver; import io.appium.java_client.remote.MobileCapabilityType; public class SwipeDemo1 { public static void main(String[] args) throws MalformedURLException, InterruptedException { DesiredCapabilities caps = new DesiredCapabilities(); caps.setCapability("appPath", “Path_to_ApiDemos.apk"); caps.setCapability("appPackage", "io.appium.android.apis"); caps.setCapability("appActivity", ".ApiDemos"); caps.setCapability("deviceName", "Emulator"); caps.setCapability("platformName", "Android"); caps.setCapability(MobileCapabilityType.VERSION, "6.0"); AndroidDriver<WebElement> driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), caps); driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); driver.findElement(By .xpath("//android.view.ViewGroup[1]/android.widget.FrameLayout[2]/android.widget.ListView[1]/android.widget.TextView[12]")) .click(); // This is the working code - works for both iOS and android. For iOS we // have to just add the visible property in the XPath. do { try { driver.findElement(By.xpath("//*[@content-desc='TextClock']")).click(); break; } catch (NoSuchElementExceptionnse) { Dimension dimensions = driver.manage().window().getSize(); System.out.println(dimensions); Double screenHeightStart = dimensions.getHeight() * 0.5; intscrollStart = screenHeightStart.intValue(); Double screenHeightEnd = dimensions.getHeight() * 0.2; intscrollEnd = screenHeightEnd.intValue(); driver.swipe(0, scrollStart, 0, scrollEnd, 1000); } } while (true); } } |
You need to observe the swipes carefully. It may be bit slow but work for sure.
Want to see more practical examples of Mobile automation features??
Please do enroll in my Mobile testing with Appium– online training and get Mastered in Mobile automation Technology
Very useful, Thank you