ClassCastException with new Appium-java TouchActions
This is still an issue in appium, apparently. Right now, the only way to do it in native Android is by an adb command:
adb shell input touchscreen swipe <x> <y> <x> <y> <durationMs>
In Java you can implement this using the following code:
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;
}
However, if you are using an app that has a webview you can better use JavaScript to scroll. The code to scroll down is:
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,500)", "");
Or to scroll up:
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,-500)", "");
Or to scroll to a certain element:
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
Be sure to switch to the webview context before using this.