[英]How to scroll form in android app using appium
我正在测试 android 应用程序
我被困在这里请帮忙。
我的例子来自 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.