如何在Java客户端7.0.0中使用appium向下滑动/向下滚动?

时间:2019-03-22 20:08:20

标签: java selenium appium

这是我拥有的代码,该代码不起作用,控制台发送此代码: 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();

2 个答案:

答案 0 :(得分:1)

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

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

导入触摸操作

答案 1 :(得分:0)

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

(仅适用于已检测的应用程序-以及在ListViews上使用)-使用应用程序的内部API,类似于scrollIntoView在网络上的工作方式,尽管它要求检测功能不适用于所有类型的滚动视图\屏幕< / p>

  • 选项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:executeScript并指定您要执行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

希望获得帮助,

最诚挚的问候,

Eugene