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

falq053o  于 2023-06-20  发布在  Android
关注(0)|答案(3)|浏览(140)

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

kxe2p93d1#

在Selenium 4/Appium 8中,TouchActions被替换为W3C操作。
http://appium.io/docs/en/commands/interactions/actions/
例如:

    • W3cActions Util类**
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
mbjcgjjk

mbjcgjjk2#

对于隐含的问题,这是一个很好的解决方案,但是你如何在这里利用一个特定的元素呢?因为在这种情况下看起来像是在屏幕上的某个点上执行点击,我试图将其应用到移动的屏幕上的一个小部件元素上

nimxete2

nimxete23#

Dimension size = getDriver().manage().window().getSize();
    int startY = (int) (size.height * 0.70);
    int endY = (int) (size.height * -0.2);
    int centerX = size.width / 2;
    //Type of Pointer Input
    PointerInput finger = new PointerInput (PointerInput.Kind. TOUCH, "finger");
    //Creating Sequence object to add actions
    Sequence swipe = new Sequence(finger, 1);
    //Move finger into starting position
    swipe.addAction(finger.createPointerMove(Duration.ofSeconds(0),PointerInput.Origin.viewport(), centerX, (int)startY));
    //Finger comes down into contact with screen
    swipe.addAction(finger.createPointerDown(0));
    //Finger moves to end position
    swipe.addAction(finger.createPointerMove (Duration.ofMillis(1000),PointerInput.Origin.viewport(),centerX, (int)endY));
    //Get up Finger from Srceen
    swipe.addAction(finger.createPointerUp(0));
    //Perform the actions
    getDriver().perform(Arrays.asList(swipe));

相关问题