使用新的Appium-java TouchActions的ClassCastException

时间:2018-01-26 09:35:56

标签: appium

我在新的TouchActions课程中遇到错误。

  • JDK版本:1.8
  • Appium:1.7.2
  • appium.java-client.version:6.0.0-BETA2
  • selenium.java.version:3.8.1

TouchActions actions = new TouchActions(appiumDriver);

运行时错误:

  

java.lang.ClassCastException:io.appium.java_client.ios.IOSDriver无法强制转换为org.openqa.selenium.interactions.HasTouchScreen

然而,旧的以下工作都很好:

TouchAction touchAction = new TouchAction(appiumDriver);

4 个答案:

答案 0 :(得分:2)

显然,这仍然是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;
    }

但是,如果您使用的是具有webview的应用,则可以更好地使用JavaScript进行滚动。向下滚动的代码是:

((JavascriptExecutor)driver).executeScript("window.scrollBy(0,500)", "");

或者向上滚动:

((JavascriptExecutor)driver).executeScript("window.scrollBy(0,-500)", "");

或滚动到某个元素:

((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);

在使用之前一定要切换到webview上下文。

答案 1 :(得分:1)

解决方案是使用Appium undefined代替硒TouchAction

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();
}

答案 2 :(得分:1)

使用io.appium.java_client.TouchAction类。

第1步

TouchAction action = new TouchAction(driver);

这里driverAppiumDriver的实例。

第2步

WebElement ele = driver.findElement(By.id("locator"));

action.tap(new TapOptions().withElement(new ElementOption().withElement(ele))).perform();

使用TouchAction的新实现,您不能直接传递weblement。

使用依赖项

<dependency>
        <groupId>io.appium</groupId>
        <artifactId>java-client</artifactId>
        <version>7.0.0</version>
    </dependency>

答案 3 :(得分:0)

使用W3C Actions 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

可通过以下网址获得文档:https://appiumpro.com/editions/29