gpt4 book ai didi

java - 如何在 Selendroid 中执行滑动操作?

转载 作者:搜寻专家 更新时间:2023-10-30 21:34:25 27 4
gpt4 key购买 nike

  1. 我已经尝试使用以下代码进行滑动。
  2. 在运行测试用例时,滑动操作没有发生,我也没有收到任何错误消息。
  3. 如何在两侧从左向右滑动,反之亦然。

有以下两种方法:-

方法 1(使用 TouchActions):-

    1. //Swipe Right to Left side of the Media Viewer First Page
WebElement firstPages = driver.findElement(By.id("media-list"));
TouchActions flick = new TouchActions(driver).flick(firstPages,-100,0,0);
flick.perform();

2. //perform swipe gesture
TouchActions swipe = new TouchActions(driver).flick(0, -20);
swipe.perform();

方法 2(使用 javascript):-

public static void swipe(WebDriver driver) {

JavascriptExecutor js = (JavascriptExecutor) driver;
HashMap<String, Double> swipeObject = new java.util.HashMap<String, Double>();
swipeObject.put("startX", 0.95);
swipeObject.put("startY", 0.5);
swipeObject.put("endX", 0.05);
swipeObject.put("endY", 0.5);
swipeObject.put("duration", 1.8);
js.executeScript("mobile: swipe", swipeObject);
}

最佳答案

尝试以下实现,其中包括标准 FlickAction.SPEED_NORMAL 参数以及 flick 的 Action 生成器:

import org.openqa.selenium.interactions.touch.FlickAction;

private Action getBuilder(WebDriver driver) {
return new Action(driver);
}

WebElement toFlick = driver().findElement(By.id("media-list"));
Action flick = getBuilder(driver()).flick(toFlick, -500, 0, FlickAction.SPEED_NORMAL).build();
flick.perform();

可以通过改变 X 轴坐标来执行从左向右滑动和反之亦然:

  • 向左滑动:

Action flick = getBuilder(driver()).flick(toFlick, -500, 0, FlickAction.SPEED_NORMAL).build();

  • 向右滑动:

Action flick = getBuilder(driver()).flick(toFlick, 500, 0, FlickAction.SPEED_NORMAL).build();

  • 滑动到顶部:

Action flick = getBuilder(driver()).flick(toFlick, 0, 500, FlickAction.SPEED_NORMAL).build();

  • 滑动到底部:

Action flick = getBuilder(driver()).flick(toFlick, 0, -500, FlickAction.SPEED_NORMAL).build();

关于java - 如何在 Selendroid 中执行滑动操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29210824/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com