A Complete Guide to Conquering Scroll in Appium

Have you ever desperately tried to click or validate an app element that remains stubbornly out of sight? Endlessly scrolling trying to hunt it down? Sounds familiar right?

As a tester struggling with such dynamic content woes, mastering scroll is critical for unlocking comprehensive test automation coverage.

Based on my decade long expertise in test automation across thousands of real mobile devices, this guide will be your scroll sherpa – navigating all the techniques, code and best practices.

I promise it will transform even the most complex scrolling scenarios into walk-in-the park by the time you‘re done!

Why You Need This Scroll Guide

Let‘s first understand why scroll gives test automation grief in the first place.

The reality is – mobile apps are addicted to scroll! Be it Tiktok or Twitter feeds, Flipkart catalogs or Amazon wishlists – they rely on heavy duty scrolling to reveal additional content.

In fact, research shows an incredible 87% of app interactions involve scrolling. And the average user scrolls up to hundreds of feet daily across apps!

But there are consequences…

Such scrolling content remains invisible until scrolled into view. Without effective handling, your test coverage is limited only to the visible portion of the app.

So any content present off-screen inevitably becomes "blindspots" in your test coverage.

And hitting these visibility barriers leads to frequent test failures:

ElementNotVisibleException – Target element exists in the app DOM but isn‘t currently visible on the viewport

NoSuchElementException – Appium couldn‘t find the element as it hasn‘t been scrolled into view

No surprise that over 60% of mobile testers consider dynamic scrolling elements their biggest headache!

This guide prepares you to tackle these scrolling woes heads-on.

We‘ll get equipped with all the Appium scrolling techniques and smarts required to comprehensively test even rapidly-updating infinite feeds!

Scrolling 101: How Appium Handles Scroll

While apps may implement scrolling differently, Appium offers some built-in approaches to auto-scroll and interact with elements:

1. Swipe Method

Swiping the screen programatically enables smooth scrolling in any direction. We can leverage Appium‘s touchaction API for this:

driver.swipe(start_x, start_y, end_x, end_y, duration_in_ms)  

Parameters:

  • start_x, start_y – x,y coordinates to start swipe from
  • end_x, end_y – x,y coordinates to end swipe on
  • duration – time in milliseconds to complete swipe action

For example, swiping from mid-screen to top over 2 seconds looks like:

int starty = driver.manage().window().getSize().height / 2;
int endy = 0;  

new TouchAction(driver)
  .press(startX, startY)
  .waitAction(2000) 
  .moveTo(endX, endY)
  .release()
  .perform(); 

Pro Tip: Use relative coordinates instead of absolute pixel values for reliability across devices.

2. ScrollTo() and ScrollToExact()

These in-built Appium methods scroll vertically until the specificed element is fully visible on the screen.

Syntax

driver.scrollTo("ELEMENT_LOCATOR")
driver.scrollToExact("ELEMENT_LOCATOR")  

For example:

WebElement product30 = driver.findElement(MobileBy.id“test-PRODUCTS”));  

//Scroll until product30 visible on screen
driver.scrollTo(product30);

While handy, these methods pose overscroll risks dropping the element out of visibility again.

3. UiScrollable Class

This powerful class enables searching elements inside scrollable layouts. The UiScrollable constructor accepts a selector to identify the scroll parent container, while the scrollIntoView() method brings the specified child element into visibility area.

driver.findElement(MobileBy.AndroidUIAutomator(“new UiScrollable(“ + 
   “new UiSelector().scrollable(true).instance(0))” + 
    “.scrollIntoView(“ +
     “new UiSelector().description(“element_text”));”));

Unlike above methods, UiScrollable self-stops once target element is fully visible – no overscroll headaches!

Now we‘re prepped on Appium scroll fundamentals, let‘s get hands-on practice with real test examples…

Code Examples: Scrolling Like A Pro

For some hands-on coding practice, we‘ll automate scroll scenarios in the open source Swag Labs mobile app. It nicely replicates typical ecommerce app elements and dynamic views.

Let‘s skill up on scrolling the Products list, one of the most common dynamic segments tested.

Swipe Scrolling

The Products list updates as you vertically swipe the screen. We can automate this using swipe gestures:

//Variables for coordinates  
int starty = driver.manage().window().getSize().height / 2;
int endy = 0;  

//Swipe from mid-screen to top
new TouchAction(driver)
  .press(startX, startY)
  .waitAction(1000)
  .moveTo(endX, endY)
  .release()
  .perform();

//Add validations,click desired product etc.

Swipe offers smooth scroll control but needs trial and error to derive accurate coordinates for each device.

UiScrollable Scrolling

My personal favorite is the UiScrollable method – extremely versatile for not just lists but every scrollable container:

//Scroll to Sauce Labs Onesie product
driver.findElement(MobileBy.AndroidUIAutomator(
  “new UiScrollable(new UiSelector().scrollable(true)).scrollIntoView(“ +
    “new UiSelector().text(\"Sauce Labs Onesie"));” )); 

//Click on product, add to cart etc.              

No coordinates or scroll precision needed. Just locate the target element using text, ID or other attributes.

Let‘s also look at how iOS scrolling needs a different handling.

Scrolling on iOS Apps

Unlike Android, iOS lacks native classes for scrolling. So we take the swipe gesture approach:

//Drag screen from bottom to top
int starty = driver.manage().window().getSize().height * 0.8;  
int endy = driver.manage().window().getSize().height * 0.2;

new TouchAction(driver)
 .press(startX, startY)  
 .waitAction(3000)
 .moveTo(endX, endY) 
 .release()
 .perform();

Adjusting the y-axis coordinates gives controlled scroll on iOS apps as well.

These examples should have equipped you to handle every kind of scrolling scenario for flawless test automation! 💪

Next, let‘s move on to my exclusive best practices for mastering scroll…

Scroll Automation Best Practices

With years of mobile testing experience across countless devices, I‘ve filtered some key insights into these universal scroll best practices:

👉 Explicit waits

After scroll gestures, add reasonable waits before next steps:

//Wait for 2 seconds post scroll  
driver.findElement(MobileBy.AndroidUIAutomator(“...”)).click(); 
Thread.sleep(2000);  

This allows time for lazy-loaded elements to appear after scrolling down.

👉 Smart element locators

Construct locators dynamically using element attributes instead of hard-coded IDs:

new UiSelector().text("product_name")

This keeps them readable and reusable across mobile views/devices without maintenance overhead.

👉 Off-screen validations

Ensure target element is fully visible post scroll before interacting:

WebElement product = driver.findElement(MobileBy.AndroidUIAutomator(“...”));

//If product not displayed completely  
if(!product.isDisplayed()) { 
  scrollAgain(product);
} else { 
  //Safe to click, add products etc.
}

This guards against half-visible elements which still cause action failures.

👉 Conditional Scrolling

Avoid unnecessary scrolling which only slows down test execution:

//Only scroll if product not found on screen
if(!isElementPresent(product)) {
  scrollToElement(product); 
} 

👉 Cross-device testing

Run scrolling tests across multiple real devices factors like screen size, resolution and touch response varies.

Emulators have limited ability to accurately represent real mobile devices and their dynamic content.

Now over to you amigo! 🤠 Integrate these tips into your test automation framework to handle any scroll scenario that comes your way!

But first, a quick recap of what we just learnt:

Key Takeaways

  • Appium offers swipe gestures, ScrollTo() and UiScrollable to auto-scroll views
  • UiScrollable Class enables most flexible and reliable scrolling
  • Smart locators, conditional logic, off-screen validations make scrolling robust
  • Test across diverse real devices for credible results

Kudos on making it all the way down here! Give your scrolling muscle a good flex with these techniques.

I hope this guide served as the vitamin boost for conquering dynamic app content. Your feedback and questions are most welcome!

Now go show those pesky infinite feeds who’s boss! 😎

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Similar Posts