With the modernization of the business landscape, mobile applications have become an integral part of our lives. From handling our day-to-day tasks, getting groceries, and communicating with others, to handling our finances, mobile applications are used everywhere. This increases the need for the technology teams to ensure that the mobile applications work seamlessly. Testing teams use Appium, which is an open-source test automation framework to automate mobile app testing across different platforms like iOS, Android, and Windows. One very common task in Appium test automation is to scroll the mobile application to interact with a specific web element. For instance, an element might be present in the lower part of the screen, hence scrolling would become an essential part to interact with that element.
In this article, we will see why scrolling is required to access elements and the different ways using which we can perform the scrolling action using Appium.
Why Scrolling is Necessary in Mobile Apps?
Unlike the traditional web apps accessible via desktop browsers, mobile applications have different screen sizes and dimensions. All the data and information displayed on these apps is adjusted to be accessed using a vertical scroll. Let us look at some points to see why scrolling is important in mobile app testing:
- To access hidden elements – The elements that can not be seen in the current view can be accessed only after scrolling. For example, a button to submit a form might be at the end of a registration form. Scrolling down is required to access the button. Similarly, certain form elements might not be in the current view, to access them for filling, scrolling is required.
- To validate the loading of dynamic content – Scrolling might be required to load dynamic tables or lists that use pagination that triggers the loading of more content. Additionally, accessing infinite scrolling feeds, or lazy loaded media also requires scrolling to update or refresh the page’s content.
- Mimic real-time gestures – Scrolling is integral to using mobile devices and it becomes important to validate the behavior within the application under test.
Explore Appium Inspector: A GUI tool for inspecting & automating mobile apps.
Scrolling Using Appium
Appium allows you to perform the scroll action in different ways-
- Touch Action – Appium provides TouchAction API that allows you to perform a series of actions like tap, touch, press, long press, scroll, and pinch. You can perform complex multi-touch actions one after the other using this API.
- UiScrollable – This is a UI Automator API provided by Appium to automate scrolling gestures. You can use it to scroll to an element under the parent object without the need to specify the direction of the scroll.
- JavaScript Executor – Appium can execute JS scripts to imitate scroll gestures and interact with the desired element.
Let us now discuss these ways to perform scrolling in mobile applications, but before that let us look at the use case that we would be automating.
We will be using the Settings App, which is an inbuilt system app for Android devices. After launching the app we will-
- Scroll down to the Display tab.
- Tap on the Display menu.
Master appium locators for efficient mobile testing.
Appium Touch Action
Touch action helps to emulate various touch-based interactions like tap, swipe, long press, etc, that are performed by mobile device users. Using touch action, you can evaluate the functionality as well as the responsiveness of mobile applications across different configurations. The code below demonstrates scrolling through a mobile application using the touch action class.
package appium;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.touch.offset.PointOption;
public class Scroll {
@Test
public void scrollToElement() throws MalformedURLException, InterruptedException {
DesiredCapabilities dc = new DesiredCapabilities();
//Setting desired capabilities for the android device with details like device name, version, etc
dc.setCapability("platformName", "android");
dc.setCapability("platformVersion","14");
dc.setCapability("deviceName", "Pixel6_TestGrid");
dc.setCapability("automationName", "UiAutomator2");
//setting capability for application we want to test
dc.setCapability("appPackage", "com.android.settings");
dc.setCapability("appActivity", "com.android.settings.Settings");
//Instantiating Android Driver and using Appium server host and port
AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), dc);
//Sleep to wait for the app to load
Thread.sleep(10000);
//Creating an object of the touch action class
TouchAction action = new TouchAction(driver);
//Using PointOption class to specify the co-ordinates for touch action
PointOption pointStart = PointOption.point(0,700);
PointOption pointEnd = PointOption.point(0,100);
//Performing touch action, press at the start point and moving it till end point
action.press(pointStart).moveTo(pointEnd).release().perform();
//Click on the Display Tab
driver.findElement(AppiumBy.androidUIAutomator("new UiSelector().text(\"Display\")")).click();
//Closing the driver instance
driver.quit();
}
}
Upon executing the above code, you will see that after the application launches, scrolling happens and the Display tab is tapped. You may wish to add desired loggers or print statements for your ease. Successful execution logs will be printed post the run finishes.
UiScrollable
UI Automator provides UiScrollable, which can be used to automate scrolling gestures in Appium. It enables us to scroll views like grids, lists, expandable collections, etc. It provides an easy way to search child elements under a scrollable parent through scrolling. It is known to handle nested scrolls efficiently without the need to specify directions and positions. It imports UiSCrollable and UiSelector to first identify the scrollable view and then locate the child element. The below code can be used to scroll through the Settings app and click on Display using UiScrollable.
package appium;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.AndroidDriver;
public class Scrollable {
@Test
public void scrollToElement() throws MalformedURLException, InterruptedException {
DesiredCapabilities dc = new DesiredCapabilities();
//Setting desired capabilities for the android device with details like device name, version, etc
dc.setCapability("platformName", "android");
dc.setCapability("platformVersion","14");
dc.setCapability("deviceName", "Pixel6_TestGrid");
dc.setCapability("automationName", "UiAutomator2");
//setting capability for application we want to test
dc.setCapability("appPackage", "com.android.settings");
dc.setCapability("appActivity", "com.android.settings.Settings");
//Instantiating Android Driver and using Appium server host and port
AndroidDriver driver = (AndroidDriver)new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), dc);
//Sleep to wait for the app to load
Thread.sleep(10000);
// Use the 'driver.findElement' method to find a single element within the current context
// The element will be located using an Android UI Automator command
// This command scrolls until it finds an element with the text "Display" and clicks on it
driver.findElemen(AppiumBy.androidUIAutomator("new UiScrollable(new UiSelector().scrollable(true)).scrollIntoView(new UiSelector().text(\"Display\"))")).click();
//Closing the driver instance
driver.quit();
}
}
The above works exactly as we saw in the first execution, with a difference in the function we used in the execution. The results too would be of a similar type.
JavaScript Executor
Another way to handle scrolling in Appium is to leverage Javascript. It handles the web view and hybrid apps in a much more efficient way. One can easily pinpoint elements using the selector. Additionally, you can pass custom offsets in place of direction. We will see how the “mobile: scroll” method can be employed to scroll through the Settings app in our use case.
package appium;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.AndroidDriver;
public class ScrollScript {
@Test
public void scrollToElement() throws MalformedURLException, InterruptedException {
DesiredCapabilities dc = new DesiredCapabilities();
//Setting desired capabilities for the android device with details like device name, version, etc
dc.setCapability("platformName", "android");
dc.setCapability("platformVersion","14");
dc.setCapability("deviceName", "Pixel6_TestGrid");
dc.setCapability("automationName", "UiAutomator2");
//setting capability for application we want to test
dc.setCapability("appPackage", "com.android.settings");
dc.setCapability("appActivity", "com.android.settings.Settings");
//Instantiating Android Driver and using Appium server host and port
AndroidDriver driver = (AndroidDriver)new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), dc);
//Sleep to wait for the app to load
Thread.sleep(10000);
//Instantiate a JavascriptExecutor to execute JavaScript code within the context of the AndroidDriver
JavascriptExecutor js = (JavascriptExecutor) driver;
//Define a string representing the element selector for the element with the text "Display"
String eleSelector ="new UiSelector().text(\"Display\")";
//Create a HashMap to store parameters for scrolling
HashMap<String, Object> scrollObj = new HashMap<String, Object>();
//Specify the strategy for scrolling, which is "-android uiautomator" indicating the use of Android UI Automator
scrollObj.put("strategy", "-android uiautomator");
//Specify the selector for the element to be scrolled to, using the previously defined 'eleSelector'
scrollObj.put("selector", eleSelector);
//Specify the direction of scrolling, in this case, "down"
scrollObj.put("direction", "down");
//Execute the JavaScript code to perform the scrolling action, using the 'mobile: scroll' command and passing the scroll parameters
js.executeScript("mobile: scroll", scrollObj);
//Use the 'driver.findElement' method to find a single element within the current context
//The element will be located using an Android UI Automator command
//This command locates the element with the text "Display" and clicks on it
driver.findElement(AppiumBy.androidUIAutomator(eleSelector)).click();
//Closing the driver instance
driver.quit();
}
}
Similar to the above two runs, the one with JS Executor will also perform the same actions and yield the same results.
After discussing the different approaches to automate scrolling in Appium, we can say that we can emulate the scroll action through the different parameters, be it the element locator, coordinates, or simply by using the strategy of the scroll in JavaScript executor.
Best Practices for Scrolling to Elements
Listed below are a few tips to ensure reliable scrolling when an element you want to interact with is not in view.
- The UI should be carefully analyzed to find scrollable parents.
- Try to scroll in the least nested scroll around your target element.
- Prefer scroll methods over JS scrolling.
- Waits after scrolls can help to avoid issues due to content loading.
- Fixing the scroll counts in the automation scripts should be avoided, rather the scrolls should be linked to the visibility of elements.
- Ensure proper exception handling in case the element is not visible upon scrolling.
- When using coordinates, try to scroll fractionally more to avoid flakiness.
Conclusion
Scrolling through an application is an important aspect of user interaction. Hence it becomes extremely critical to have robust scrolling interactions to achieve stable test automation of applications. Based on the build and complexity of the application under test, you can use one of the different techniques discussed to handle scroll. Appium’s scrolling methods when used with the best practices can help access and test complex scroll interactions.