有两种方法如下: -
方法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);
}
答案 0 :(得分:2)
尝试以下实现,包括标准 FlickAction.SPEED_NORMAL 参数以及 flick 的操作构建器:
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-asis坐标来执行:
动作flick = getBuilder(driver())。轻弹(toFlick, -500 ,0, FlickAction.SPEED_NORMAL).build();
动作flick = getBuilder(driver())。轻弹(toFlick, 500 ,0,FlickAction.SPEED_NORMAL).build();
动作flick = getBuilder(driver())。轻弹(toFlick,0, 500 ,FlickAction.SPEED_NORMAL).build();
动作flick = getBuilder(driver())。轻弹(toFlick,0, -500 ,FlickAction.SPEED_NORMAL).build();
答案 1 :(得分:0)
在功能测试JavaScript重型网站时,操作完全无法工作的一个常见原因是在网站完成初始化之前采取了操作。测试这个的最简单方法是在执行操作之前添加一个短暂的睡眠。说2秒。
如果这样可以解决问题,那么您就知道在页面初始化和测试代码运行之间存在竞争条件。
此时你可以rewrite your code to wait for the action to be possible。
答案 2 :(得分:0)
我知道这是一个老问题,但我遇到了一些类似的问题。
正如其他人所指出的那样,这通常是时间问题。所以我发现如果我等待一个元素存在(即驱动程序已经返回控制并且页面已经渲染),然后在轻弹动作之前和之后进行了短暂的睡眠,它的工作原理,当然在我测试过的每个页面/应用程序上。所以我们的代码看起来有点像这样:
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("some-element-id)));
flickIt(driver, element -100, 0);
private void flickIt(WebDriver driver, WebElement el, int x, int y) {
SyntheticsUtility.sleep(500); // put 0.5s sleeps around it to make sure, need to be stable
TouchActions touch = new TouchActions(driver);
touch.flick(el, x, y, FlickAction.SPEED_NORMAL);
touch.perform();
SyntheticsUtility.sleep(500);
}