2/18/19

Handling Horizontal & Vertical Scrolling in Appium

Horizontal Scrolling
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.MobileDriver;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.touch.offset.ElementOption;

public class Example6HorizontalScrolling {

    public static void main(String[] args) throws MalformedURLException {
   
        DesiredCapabilities dc= new DesiredCapabilities();
       
        dc.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");
       
        dc.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
       
        dc.setCapability(MobileCapabilityType.PLATFORM_VERSION, "5.1");//Version is number here
       
        dc.setCapability(MobileCapabilityType.DEVICE_NAME, "Android");

        dc.setCapability(MobileCapabilityType.APP, "C:\\apkfiles\\AndroidUI.apk");
       
        URL url =new URL("http://127.0.0.1:4723/wd/hub");

        AndroidDriver driver= new AndroidDriver(url,dc);
       
        //ProgressBar/Horizontal scrolling
       
        WebElement obj=driver.findElementById("com.android.androidui:id/seekBar1");
       
        String val1=driver.findElementById("com.android.androidui:id/textProgress").getText();
        System.out.println(val1);
       
        TouchAction action=new TouchAction((MobileDriver) driver);
       
        action.longPress(ElementOption.element(obj)).moveTo(ElementOption.element(obj,250,250)).release().perform();
   
        String val2=driver.findElementById("com.android.androidui:id/textProgress").getText();
        System.out.println(val2);
   
    }

}
Vertical Scrolling

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.MobileBy;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.remote.MobileCapabilityType;

public class Example7VericalScrolling {

    public static void main(String[] args) throws MalformedURLException {
DesiredCapabilities dc= new DesiredCapabilities();
       
        dc.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Appium");
       
        dc.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
       
        dc.setCapability(MobileCapabilityType.PLATFORM_VERSION, "5.1");//Version is number here
       
        dc.setCapability(MobileCapabilityType.DEVICE_NAME, "Android");
       
        dc.setCapability(MobileCapabilityType.APP, "C:\\apkfiles\\ApiDemos.apk");
       
        URL url =new URL("http://127.0.0.1:4723/wd/hub");

        AndroidDriver driver= new AndroidDriver(url,dc);
       
       
        //vertical scrolling
        driver.findElementsById("android:id/text1").get(10).click();//identify views and click on it
       
       
        //capturing all teh list items
        AndroidElement list=(AndroidElement)driver.findElementById("android:id/text1");
       
        //scrolling down till we get the element
        MobileElement listitem = (MobileElement) driver.findElement(
                MobileBy.AndroidUIAutomator(
                        "new UiScrollable(new UiSelector()).scrollIntoView("
                                + "new UiSelector().description(\"Switches\"));"));
       

        System.out.println(listitem.getLocation());
        listitem.click();
    }

}

0 comments:

Post a Comment