【问题标题】:How to use swipe down/scroll down in appium with java-client 7.0.0?如何在带有 java-client 7.0.0 的 appium 中使用向下滑动/向下滚动?
【发布时间】:2019-03-22 20:08:19
【问题描述】:

这是我的代码,代码不起作用,控制台发送: java.lang.ClassCastException:无法将 com.sun.proxy.$Proxy18 转换为 org.openqa.selenium.remote.RemoteWebElement

我需要滑动/向下滚动。

TouchAction swipe = new TouchAction(ApplicationLauncherAndroid.driver)
.tap(element(lblImagen))// first initialElement
.waitAction(waitOptions(Duration.ofMillis(2000)))
.moveTo(element(elementoFinal)) final Element
.release();
swipe.perform();

【问题讨论】:

标签: java selenium appium


【解决方案1】:

在appium中使用以下方法刷卡。确保您已从正确的库中导入。

import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.MobileElement;
import org.openqa.selenium.Dimension;
import static io.appium.java_client.touch.WaitOptions;
import static io.appium.java_client.touch.offset.PointOption;
import static java.time.Duration.ofMillis;

//Vertical Swipe by percentages
    public void verticalSwipeByPercentages(double startPercentage, double endPercentage, double anchorPercentage) {
        Dimension size = driver.manage().window().getSize();
        int anchor = (int) (size.width * anchorPercentage);
        int startPoint = (int) (size.height * startPercentage);
        int endPoint = (int) (size.height * endPercentage);

        new TouchAction(driver)
                .press(PointOption.point(anchor, startPoint))
                .waitAction(WaitOptions.waitOptions(ofMillis(1000)))
                .moveTo(PointOption.point(anchor, endPoint))
                .release().perform();
    }

    //Swipe by elements
    public void swipeByElements (MobileElement startElement, MobileElement endElement) {
        int startX = startElement.getLocation().getX() + (startElement.getSize().getWidth() / 2);
        int startY = startElement.getLocation().getY() + (startElement.getSize().getHeight() / 2);

        int endX = endElement.getLocation().getX() + (endElement.getSize().getWidth() / 2);
        int endY = endElement.getLocation().getY() + (endElement.getSize().getHeight() / 2);

        new TouchAction(driver)
                .press(PointOption.point(startX,startY))
                .waitAction(WaitOptions.waitOptions(ofMillis(1000)))
                .moveTo(PointOption.point(endX, endY))
                .release().perform();
    }

您似乎导入了错误的 TouchAction。从 io.appium.java_client.TouchAction

导入触摸动作

【讨论】:

  • 我试试这段代码,但我有一个错误:java.lang.IllegalArgumentException: Can not set io.appium.java_client.android.AndroidElement field qa.automated.web.bci.Pages.pageMercadoLibre .precio 到 com.sun.proxy.$Proxy18
  • 你能告诉我 qa.automated.web.bci.Pages.pageMercadoLibre.precio 包中的代码吗?您可以尝试 MobileElement,而不是 AndroidElement
  • github.com/piter721/pruebaScroll ,在分支中:“pruebaScrollTheApp”,src->text->generic->scrollElement |或 src->text->definition->scrollApp
  • 问题在于您的页面对象实现。使用页面对象模型,如this
【解决方案2】:

@Pedro Perez Aballay,嗨。 我建议使用以下替代方法:

(仅适用于检测应用程序 - 和 ListViews) - 使用应用程序的内部 API,并且类似于 scrollIntoView 在 Web 上的工作方式,尽管它需要检测不适用于所有类型的滚动视图 \screens

  • 选项 B: TouchAction - 例如

    TouchAction touchAction = new TouchAction(driver); WebElement phone = driver.findElement(in.Repo.obj("apps.Phone")); WebElement contact = driver.findElement(in.Repo.obj("apps.Contacts")); touchAction.press(phone) .waitAction(200) .moveTo(contact) .release() .perform();

  • 选项 C:client - SwipeWhileNotFound

  • 选项 D:执行脚本并指定您要执行 swipe action

语法:

driver.executeScript("client:client.swipeWhileNotFound(\"direction\", offset, time);

从屏幕边缘向上滑动 500 毫秒的示例:

driver.executeScript("experitest:client.swipe(\"Up\", 0, 500)");

参数:

方向:滑动的方向(上、下、左、右) 偏移量:屏幕上滑动开始的位置 时间:滑动的持续时间(较低 = 更快的滑动)

请注意

这些值(时间\偏移量)通常是根据设备屏幕尺寸作为参数计算的,例如 -

int offset = driver.manage().window().getSize().getHeight() / 2; // start from mid screen
int time = driver.manage().window().getSize().getHeight() * 0.3; // just an example

希望对您有所帮助,

最好的问候,

尤金

【讨论】:

    猜你喜欢
    • 2016-05-16
    • 2019-12-24
    • 2018-01-01
    • 2020-11-04
    • 2018-02-02
    • 1970-01-01
    • 2017-03-23
    • 2018-05-06
    • 1970-01-01
    相关资源
    最近更新 更多