Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Scroll down to click a particular element using Appium as scrollTo is not working

I'm trying to scroll down and click on some element in setting, but as scrollTo is not working I'm not able to proceed

package Test1;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import net.sourceforge.htmlunit.corejs.javascript.ScriptableObject;

import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.By;

import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.File;
import java.net.URL;
public class MobileFindJavaTest {
private AppiumDriver<WebElement> driver;

@BeforeMethod
 @Test
public void setUp() throws Exception {
File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "/Apps/slide/");
File app = new File(appDir, "ApiDemos-debug.apk");

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName","BECUPJTWGA7HAQQK");
capabilities.setCapability("platformVersion", "5");

capabilities.setCapability("appPackage", "com.android.settings");
capabilities.setCapability("appActivity", ".Settings");
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), c         Capabilities);


    //driver.scrollTo("Views");
    TouchAction touchAction4 = new TouchAction(driver);
    touchAction4.press(30,40).moveTo(89,51).release();
    driver.performTouchAction(touchAction4);
    System.out.println("gaurav");
    driver.findElementByName("Feature").click();

}

}

driver.scrollTo is showing not supported , now how to scroll down and click because the element which i want to click is not there on the screen , we need to scroll for it

MobileElement radioGroup = (MobileElement) wd

            .findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()"

            + ".resourceId(\"com.android.settings:id/title\")).scrollIntoView("

            + "new UiSelector().text(\"Feature\"));");
            radioGroup.click();

i tried in this way also but it is not scrolling...the setting opens and that's it

like image 490
gaurav pandey Avatar asked Oct 27 '16 18:10

gaurav pandey


2 Answers

You can scroll using below code snippet:

public void scroll() {
    Dimension dimensions = driver.manage().window().getSize();
    int Startpoint = (int) (dimensions.getHeight() * 0.5);
    int scrollEnd = (int) (dimensions.getHeight() * 0.5);
    driver.swipe(200, Startpoint,200,scrollEnd,2000); 
}

You can have some condition upto which this method can be called and once your condition is met, scrolling will stop and you can perform desired action. Something like:

while(condition){
    scroll();
}

P.S.: You can modify value of 0.5 as required, I used the value as required in my case.

like image 154
Abhinav Avatar answered Oct 18 '22 03:10

Abhinav


// create a method name scrollTo or any name of your choice , with below mentioned code it will take parameter of text using which we want to scroll :

public void scrollTo(String text)
{                
driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""+text+"\").instance(0))");
}
like image 37
shiv Avatar answered Oct 18 '22 04:10

shiv