简体   繁体   中英

How to scroll form in android app using appium

I am testing android application

  1. I am able to fill form up to certain fields which are visible on device
  2. but now i want to scroll form and want to fill other fields and tap on submit button in last.

I am stuck here please help.

My example is from python but it will work for Java as well just use java syntax to find element like

driver.find_element_by_android_uiautomator('new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains("**/Put some text of scroll screen/**").instance(0))')

Or using java syntax

driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\"**/Put some text of scroll screen/**\").instance(0))")

Just wrap all that inside a ScrollView :

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <!-- Here you put the rest of your current view-->
</ScrollView>

ScrollView can contain just one item (only one layout as child)... so if you had something like this:

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <!-- bla bla bla-->
</LinearLayout>

You must change it to:

<ScrollView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
    <LinearLayout
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
        <!-- bla bla bla-->
    </LinearLayout>
</ScrollView>

For reference you can raed..

https://developer.android.com/reference/android/widget/ScrollView.html

or Simple ScrollView example...

http://stacktips.com/tutorials/android/android-scrollview-example

while testing your app using appium, use the below code to swipe vertically:

Dimension size = driver.manage().window().getSize(); 
int starty = (int) (size.height * 0.80); 
//Find endy point which is at top side of screen. 
int endy = (int) (size.height * 0.20);
//int endy = (int) (size.height * 0.10);
//Find horizontal point where you wants to swipe. It is in middle of screen width. 
int startx = size.width / 2; 

//Swipe from Bottom to Top. 
driver.swipe(startx, starty, startx, endy, 3000); 

this will vertically swipe down your screen.

you can also try this line to scroll:

// Scroll till element which contains Tabs text.
driver.scrollTo("Tabs");

as you are facing with your android driver, declare it like below:

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName","fevicename");
capabilities.setCapability("platformName","Android");
//add more according to your requirement
public AppiumDriver<WebElement> driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.scrollTo("Tabs");

is no more

In java client 4.0 version, scrollTo and scrollToExact are deprecated. You have to use driver.swipe()

// 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))");
   }

I wrote a code to swipe form with direction, it's working for me home this helps you.

The below is the code for swipe up and down

if (Direction.equalsIgnoreCase("Up")) {
            startX = size.width / 2;
            startY = ((int) (size.height * 0.80) - Offset);
            endX   = startX;
            endY   = (int) (size.height * 0.20);
        }
        //down
        else if(Direction.equalsIgnoreCase("down")) {
            startX = size.width / 2;
            startY = ((int) (size.height * 0.20) + Offset);
            endX   = startX;
            endY   = (int) (size.height * 0.80);
        }

pointOption = new PointOption();

new TouchAction<>(getDriver()).press(pointOption.point(startX, startY)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(1000)))
                .moveTo(pointOption.point(endX, endY)).release().perform();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM