对于新的TouchActions类,我得到了以下错误。
TouchActions actions = new TouchActions(appiumDriver);运行时错误:
org.openqa.selenium.interactions.HasTouchScreen :不能将java.lang.ClassCastException转换为io.appium.java_client.ios.IOSDriver
然而,旧的,以下所有工作都很好:
TouchAction touchAction = new TouchAction(appiumDriver);发布于 2018-12-22 15:22:37
使用W3C动作API执行手势。
public void horizontalSwipingTest() throws Exception {
login();
driver.findElementByAccessibilityId("slider1").click();
wait.until(ExpectedConditions.presenceOfElementLocated(MobileBy.AccessibilityId("slider")));
MobileElement slider = driver.findElementByAccessibilityId("slider");
Point source = slider.getLocation();
PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");
Sequence dragNDrop = new Sequence(finger, 1);
dragNDrop.addAction(finger.createPointerMove(Duration.ofMillis(0),
PointerInput.Origin.viewport(), source.x, source.y));
dragNDrop.addAction(finger.createPointerDown(PointerInput.MouseButton.MIDDLE.asArg()));
dragNDrop.addAction(new Pause(finger, Duration.ofMillis(600)));
dragNDrop.addAction(finger.createPointerMove(Duration.ofMillis(600),
PointerInput.Origin.viewport(),
source.x + 400, source.y));
dragNDrop.addAction(finger.createPointerUp(PointerInput.MouseButton.MIDDLE.asArg()));
driver.perform(Arrays.asList(dragNDrop));
}
public void verticalSwipeTest() throws InterruptedException {
login();
wait.until(ExpectedConditions.presenceOfElementLocated(MobileBy.AccessibilityId("verticalSwipe")));
driver.findElementByAccessibilityId("verticalSwipe").click();
wait.until(ExpectedConditions.presenceOfElementLocated(MobileBy.AccessibilityId("listview")));
verticalSwipe("listview");
}
private void verticalSwipe(String locator) throws InterruptedException {
Thread.sleep(3000);
MobileElement slider = driver.findElementByAccessibilityId(locator);
Point source = slider.getCenter();
PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");
Sequence dragNDrop = new Sequence(finger, 1);
dragNDrop.addAction(finger.createPointerMove(Duration.ofMillis(0),
PointerInput.Origin.viewport(),
source.x / 2, source.y + 400));
dragNDrop.addAction(finger.createPointerDown(PointerInput.MouseButton.MIDDLE.asArg()));
dragNDrop.addAction(finger.createPointerMove(Duration.ofMillis(600),
PointerInput.Origin.viewport(), source.getX() / 2, source.y / 2));
dragNDrop.addAction(finger.createPointerUp(PointerInput.MouseButton.MIDDLE.asArg()));
driver.perform(Arrays.asList(dragNDrop));
}在这里可以找到其他手势的例子:https://github.com/saikrishna321/VodQaAdvancedAppium/blob/master/src/test/java/com/appium/gesture/GestureTest.java
发布于 2018-04-21 15:09:52
显然,这仍然是appium的一个问题。现在,在本地Android中实现这一目的的唯一方法是通过adb命令:
adb shell input touchscreen swipe <x> <y> <x> <y> <durationMs>
在Java中,您可以使用以下代码实现这一点:
public static String swipe(int startx, int starty, int endx, int endy, int duration) {
return executeAsString("adb shell input touchscreen swipe "+startx+" "+starty+" "+endx+" "+endy+" "+duration);
}
private static String executeAsString(String command) {
try {
Process pr = execute(command);
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = input.readLine()) != null) {
if (!line.isEmpty()) {
sb.append(line);
}
}
input.close();
pr.destroy();
return sb.toString();
} catch (Exception e) {
throw new RuntimeException("Execution error while executing command" + command, e);
}
}
private static Process execute(String command) throws IOException, InterruptedException {
List<String> commandP = new ArrayList<>();
String[] com = command.split(" ");
for (int i = 0; i < com.length; i++) {
commandP.add(com[i]);
}
ProcessBuilder prb = new ProcessBuilder(commandP);
Process pr = prb.start();
pr.waitFor(10, TimeUnit.SECONDS);
return pr;
}但是,如果您使用的应用程序有一个use视图,您可以更好地使用JavaScript滚动。向下滚动的代码是:
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,500)", "");或者向上滚动:
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,-500)", "");或者滚动到某个元素:
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);在使用之前,一定要切换到webview上下文。
发布于 2018-08-27 13:50:04
解决方案是使用Appium TouchAction而不是Selenium TouchActions。
import io.appium.java_client.TouchAction;
public AndroidDriver<MobileElement> driver = new TouchAction(driver);
public void tap(MobileElement element) {
getTouchAction()
.tap(
new TapOptions().withElement(
ElementOption.element(
element)))
.perform();
}调用方法():
tap(myMobileElement);https://stackoverflow.com/questions/48458726
复制相似问题