簡體   English   中英

如何使用appium在android應用程序中滾動表單

[英]How to scroll form in android app using appium

我正在測試 android 應用程序

  1. 我可以將表格填寫到設備上可見的某些字段
  2. 但現在我想滾動表單並想填寫其他字段並最后點擊提交按鈕。

我被困在這里請幫忙。

我的例子來自 python,但它也適用於 Java 以及只使用 java 語法來查找元素,如

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))')

或者使用java語法

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

只需將所有內容包裝在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 只能包含一個項目(只有一個布局作為子項)......所以如果你有這樣的東西:

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

您必須將其更改為:

<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>

作為參考,你可以raed..

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

或簡單的 ScrollView 示例...

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

在使用 appium 測試您的應用程序時,請使用以下代碼垂直滑動:

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); 

這將垂直向下滑動您的屏幕。

您也可以嘗試使用此行滾動:

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

當您面對您的 android 驅動程序時,請如下聲明:

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

不再

在 java 客戶端 4.0 版本中,不推薦使用 scrollTo 和 scrollToExact。 你必須使用driver.swipe()

// 創建一個方法名稱 scrollTo 或您選擇的任何名稱,使用下面提到的代碼,它將采用我們想要滾動的文本參數:

public void scrollTo(String text){                
      driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\""+text+"\").instance(0))");
   }

我寫了一個代碼來用方向滑動表單,它對我有用,這對你有幫助。

下面是上下滑動的代碼

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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM