[英]How to perform swipe action in Selendroid?
There are two methods which are as follows:- 有两种方法如下: -
Method 1(using TouchActions):- 方法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();
Method 2 (using javascript):- 方法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);
}
Try following implementation which inlcudes standard FlickAction.SPEED_NORMAL argument and also action builder for flick : 尝试以下实现,其中包括标准的FlickAction.SPEED_NORMAL参数以及用于轻弹的动作构建器:
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();
Swiping from left to right and vice verse can be performed by varying X-asis coordinates: 可以通过改变X-asis坐标来执行从左到右的滑动和反之亦然:
Action flick = getBuilder(driver()).flick(toFlick, -500 , 0, FlickAction.SPEED_NORMAL).build(); 动作片= getBuilder(驱动程序())轻弹(toFlick,-500,0,FlickAction.SPEED_NORMAL).build();
Action flick = getBuilder(driver()).flick(toFlick, 500 , 0, FlickAction.SPEED_NORMAL).build(); 动作片= getBuilder(驱动程序())轻弹(toFlick,500,0,FlickAction.SPEED_NORMAL).build();
Action flick = getBuilder(driver()).flick(toFlick, 0, 500 , FlickAction.SPEED_NORMAL).build(); 动作片= getBuilder(驱动程序())轻弹(toFlick,0,500,FlickAction.SPEED_NORMAL).build();
Action flick = getBuilder(driver()).flick(toFlick, 0, -500 , FlickAction.SPEED_NORMAL).build(); 动作flick = getBuilder(driver())。flick(toFlick,0, -500 ,FlickAction.SPEED_NORMAL).build();
A common reason for actions just completely failing to work when functionally testing JavaScript heavy web sites is that the actions are taken before the site has finished initialising. 在功能测试JavaScript重型网站时,操作完全无法工作的一个常见原因是在网站完成初始化之前采取了操作。 The simplest way to test this is the case is to add a short sleep before performing the action. 测试这个的最简单方法是在执行操作之前添加一个短暂的睡眠。 Say 2 seconds. 说2秒。
If this solves the problem then you know you have a race condition between the page initialising and your test code running. 如果这解决了问题,那么您就知道在页面初始化和运行的测试代码之间存在竞争条件。
At that point you can rewrite your code to wait for the action to be possible . 此时,您可以重写代码以等待操作成为可能 。
I know this is an old question, but I encountered a number of similar problems with this. 我知道这是一个老问题,但我遇到了一些类似的问题。
As others have pointed out, it's generally a timing thing. 正如其他人所指出的那样,这通常是时间问题。 So I found that if I waited for an element to be present (ie the driver has returned control and the page has rendered), and then did a brief sleep both before and after the flick action, it works, certainly on every page/app I've tested it with. 所以我发现如果我等待一个元素存在(即驱动程序已经返回控件并且页面已经渲染),然后在轻弹动作之前和之后进行了短暂的睡眠,它确实可以在每个页面/应用程序上运行我用它测试过。 So our code looks a bit like this: 所以我们的代码看起来有点像这样:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.