我试图在模拟器上运行一个滚动屏幕的命令。我尝试过action类中的一组操作命令,但它们不一致。我最终偶然发现了下面的代码。
是否需要导入或包含某些内容才能使这些操作正常工作?*我的另一个困惑是,此代码在使用Espresso驱动程序时可以工作,但在UiAutomator2驱动程序下运行时会产生此错误消息。*我尝试导入操作类,但这不能解决问题。*再说一次,是否需要导入或专门用于UiAutomator2驱动程序上的这些命令?
此代码在使用Espresso驱动程序时工作,但在UiAutomator2驱动程序下运行时会产生此错误消息。我尝试导入操作类,但这不能解决问题。
WebElement element1 = wait.until(ExpectedConditions.visibilityOfElementLocated(originLocator));
WebElement element2 = wait.until(ExpectedConditions.visibilityOfElementLocated(destinationLocator));
int startY = element1.getLocation().getY() + (element1.getSize().getHeight() / 2);
int startX = element1.getLocation().getX() + (element1.getSize().getWidth() / 2);
int endX = element2.getLocation().getX() + (element2.getSize().getWidth() / 2);
int endY = element2.getLocation().getY() + (element2.getSize().getHeight() / 2);
PointerInput input = new PointerInput(PointerInput.Kind.TOUCH, "input");
Sequence swipeTo = new Sequence(input, 0);
swipeTo.addAction(input.createPointerMove(Duration.ZERO, PointerInput.Origin.viewport(), startX, startY));
swipeTo.addAction(input.createPointerDown(PointerInput.MouseButton.LEFT.asArg()));
swipeTo.addAction(input.createPointerMove(Duration.ofMillis(1000), PointerInput.Origin.viewport(), endX, endY));
swipeTo.addAction(input.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
driver.perform(Arrays.asList(swipeTo));这应该会在模拟器上滚动移动应用程序的可见页面,但目前它在使用UiAutomator2驱动程序运行时会导致以下错误(org.openqa.selenium.UnsupportedCommandException: actions)。
编辑:在应用下面评论中的代码后,我有以下代码,它与UiAutomator2驱动程序一致,但不适用于Espresso驱动程序(它的目标是屏幕上错误的空间,似乎是点击一个元素,而不是触摸和按住)。
WebElement element1 = wait.until(ExpectedConditions.visibilityOfElementLocated(originLocator));
WebElement element2 = wait.until(ExpectedConditions.visibilityOfElementLocated(destinationLocator));
int startY = element1.getLocation().getY() + (element1.getSize().getHeight() / 2);
int startX = element1.getLocation().getX() + (element1.getSize().getWidth() / 2);
int endX = element2.getLocation().getX() + (element2.getSize().getWidth() / 2);
int endY = element2.getLocation().getY() + (element2.getSize().getHeight() / 2);
Dimension dim=driver.manage().window().getSize();
int height=(int) dim.getHeight();
int width=(int) dim.getWidth();
int x= width/2;
new TouchAction(driver)
.press(PointOption.point(x,startY))
.waitAction(WaitOptions.waitOptions(Duration.ofMillis(2000)))
.moveTo(PointOption.point(x,endY))
.release()
.perform();因此,我为每个驱动程序都有一个可用的版本。有没有人知道为什么它们会根据使用的驱动程序而有不同的行为?
发布于 2019-02-01 21:32:31
如果你想在安卓应用上滚动,你可以使用touch action class。下面的代码将向下滚动。如果要向上滚动,请相应地更改startY和startX。希望能有所帮助。
public void scrollDown() {
Dimension dim=new Dimension();
int height=(int) dim.getHeight();
int width=(int) dim.getWidth();
int x= width/2;
int startY=(int) (height*0.80);
int endY=(int) (height*0.20);
new TouchAction(driver).press(x, startY).waitAction(Duration.ofMillis(2000)).moveTo(x, endY).release().perform();
}发布于 2019-02-01 13:45:33
你可以使用AndroidDriver驱动,并且在AppiumDriver类中有一个支持滑动的方法,所以你不需要使用Action类来滑动页面。
我已经创建了一个通用的参数化方法来上下垂直滑动,它根据卷帘类型进行卷帘。
代码如下:
public enum SwipeType {
VerticallyUp, VerticallyDown
}
public void swipeVertically(int swipeValue, SwipeType Move) {
Double swipeHeight = 0.0;
swipeHeight = (double) swipeValue;
Dimension screenSize = driver.manage().window().getSize();
Double screenWidth = Double.valueOf(String.valueOf(screenSize.getWidth())) / 2;
Double screenHeight = Double.valueOf(String.valueOf(screenSize.getHeight())) / 2;
if (screenHeight + swipeValue > screenHeight * 2 || swipeValue == 0) {
swipeHeight = screenHeight / 2;
}
switch (Move) {
case VerticallyUp:
driver.swipe(screenWidth.intValue(), screenHeight.intValue() + swipeHeight.intValue(), screenWidth.intValue(), screenHeight.intValue(), 2000);
break;
case VerticallyDown:
driver.swipe(screenWidth.intValue(), screenHeight.intValue(), screenWidth.intValue(), screenHeight.intValue() + swipeHeight.intValue(), 2000);
break;
}
}我使用了swipeValue as 300,因为它在我的应用程序上工作得很好。你可以根据你的应用来使用它。
我希望这对你有帮助!
https://stackoverflow.com/questions/54470121
复制相似问题