如何在 Java 中使用操作或在应用程序中滚动?

How to use actions or do scroll in app in Java?

TouchAction 已弃用。 任何关于垂直滚动的新用法的指示? 下面的代码使用。但不工作,因为 TouchAction 已被弃用。

Dimension dimension =driver.manage().window().getSize();
int start_x=(int) (dimension.width*0.5); 
int start_y=(int) (dimension.height*0.9); 
int end_x=(int) (dimension.width*0.2); 
int end_y=(int) (dimension.height*0.1); 
TouchAction touch =new TouchAction(driver); 
touch.press(PointOption.point(start_x,start_y))
   .waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1)))
   .moveTo(PointOption.point(end_x, end_y))
   .release()
   .perform(); 
Thread.sleep(3000); 
dimension =driver.manage().window().getSize(); 
start_x=(int) (dimension.width*0.2); 
start_y=(int) (dimension.height*0.2); 
end_x=(int) (dimension.width*0.5); 
end_y=(int) (dimension.height*0.8); 
touch =new TouchAction(driver); 
touch.press(PointOption.point(start_x,start_y))
    .waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1)))
    .moveTo(PointOption.point(end_x, end_y))
    .release()
    .perform();

在 Selenium 4/Appium 8 中 TouchActions 替换为 W3C 操作。

http://appium.io/docs/en/commands/interactions/actions/

以你的例子为例:

W3cActions 实用程序 Class

import org.openqa.selenium.Point
import org.openqa.selenium.interactions.PointerInput
import org.openqa.selenium.interactions.Sequence
import static java.time.Duration.ofMillis
import static org.openqa.selenium.interactions.PointerInput.Kind.TOUCH
import static org.openqa.selenium.interactions.PointerInput.MouseButton.LEFT
import static org.openqa.selenium.interactions.PointerInput.Origin.viewport

public class W3cActions {

    private final static PointerInput FINGER = new PointerInput(TOUCH, 'finger');

    public static void doSwipe(AppiumDriver driver, Point start, Point end, int duration) {
        Sequence swipe = new Sequence(FINGER, 1)
                .addAction(FINGER.createPointerMove(ofMillis(0), viewport(), start.getX(), start.getY()))
                .addAction(FINGER.createPointerDown(LEFT.asArg()))
                .addAction(FINGER.createPointerMove(ofMillis(duration), viewport(), end.getX(), end.getY()))
                .addAction(FINGER.createPointerUp(LEFT.asArg()));
        driver.perform(Arrays.asList(swipe));
    }

    public static void doTap(AppiumDriver driver, Point point, int duration) {
        Sequence tap = new Sequence(FINGER, 1)
                .addAction(FINGER.createPointerMove(ofMillis(0), viewport(), point.getX(), point.getY()))
                .addAction(FINGER.createPointerDown(LEFT.asArg()))
                .addAction(new Pause(FINGER, ofMillis(duration)))
                .addAction(FINGER.createPointerUp(LEFT.asArg()));
        driver.perform(Arrays.asList(tap));
    }

}

滑动

Dimension dimension = driver.manage().window().getSize();
Point start = new Point((int)(dimension.width*0.5), (int)(dimension.height*0.9));
Point end = new Point((int)(dimension.width*0.2), (int)(dimension.height*0.1));
W3cActions.doSwipe(driver, start, end, 1000);  //with duration 1s

Thread.sleep(3000); 

start = new Point((int)(dimension.width*0.2), (int)(dimension.height*0.2));
end = new Point((int)(dimension.width*0.5), (int)(dimension.height*0.8)); 
W3cActions.doSwipe(driver, start, end, 1000); //with duration 1s

点击

Dimension dimension = driver.manage().window().getSize();
Point forTap = new Point((int)(dimension.width*0.5), (int)(dimension.height*0.9));
W3cActions.doTap(driver, forTap, 200); //with duration 200ms

隐含问题的绝佳解决方案,但是您如何点击此处的特定元素?因为在这种情况下看起来像是在屏幕上的某个点上执行了点击,我正在尝试应用它来点击移动屏幕上的一个小部件元素