Scrolling Gesture – Appium


You already know how to perform longpress gesture using Appium. Now let’s see how to perform a Scroll. I hope you know what is a ‘scrolling‘. We do it often in day to day life. As a quick reference swiping your finger upwards will perform a scroll down and swiping down your finger will perform a scroll up. Just like ‘TouchAction‘ class we also a ‘scrollTo’ function in-built. Lets quickly take a look at the syntax  &  Example ‘driver.scrollTo(“sampleScroll”); ‘ 

package com.training;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class Scroll {
AndroidDriver driver;
@BeforeClass
public void setUp() throws MalformedURLException{
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, “4.4.2”);
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, “Android”);
caps.setCapability(MobileCapabilityType.DEVICE_NAME, “YourDevice”);
caps.setCapability(MobileCapabilityType.APP_PACKAGE,”Your Package”);
caps.setCapability(MobileCapabilityType.APP_ACTIVITY,”App Activity”);
driver = new AndroidDriver (new URL(“http://127.0.0.1:4723/wd/hub”),caps);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
}
@Test
public void scrollSample(){
//scroll function
driver.scrollTo(“sampleScroll”);
}
@AfterClass
public void tearDown(){
driver.closeApp();
}
}

By using the above function, it will scroll to the text ‘sampleScroll’ We also have another function called ‘scrollToExact’ which is different from above function. This function will scroll to exact given text. This is generally useful in spinners. By scrolling using text, your screen will moved to that element having that particular text. This would be one way to scroll. Lets see another way using javascript executor.

Example:

@Test
public void scrollSample() {
JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap<String, Double> scrollElement = new HashMap<String, Double>();
scrollElement .put(“startX”, 0.50);
scrollElement .put(“startY”, 0.95);
scrollElement .put(“endX”, 0.50);
scrollElement .put(“endY”, 0.01);
scrollElement .put(“duration”, 3.0);
js.executeScript(“mobile: swipe”, scrollElement );
}

Using JsonWP we can use this function to scroll. Prefix “mobile:” allows us to perform gestures. There are few other methods which can be utilized ‘mobile: tap’ & ‘mobile: shake’. So, these are the ways to scroll to an element, directions. Don’t worry about Hashmap and ‘startX’ ‘startY’ coordinates, we will see it  in next article. If you’re not familiar with Desired capabilities then click here

Advertisements

3 thoughts on “Scrolling Gesture – Appium

  1. Anonymous April 4, 2017 / 4:22 pm

    Hi Charan,
    scrollTo method has been deprecated now, please update the blog accordingly.

    Like

    • Hari Charan April 4, 2017 / 9:38 pm

      Yea, thanks for the information. I am planning to republish few articles. So I will do it altogether

      Like

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s