简体   繁体   English

Android / Java / Appium - 向下滑动(滚动)不起作用[移动应用]

[英]Android / Java / Appium - Swipe (Scroll) Down does not work [Mobile App]

Hi community: I'm fighting with a mobile application in Android.嗨社区:我正在与 Android 中的移动应用程序作斗争。

I try to do a swipe (scroll) down to the final of the app, but it's impossible.我尝试向下滑动(滚动)到应用程序的最后,但这是不可能的。 I have red a lot about similar situations in the forum, but nothing could help me.我在论坛中对类似情况有很多了解,但没有什么可以帮助我。

This is my tech stack below.这是我下面的技术堆栈。

Appium version: 1.17.1
Java: 1.8

This is my POM file.这是我的 POM 文件。

    <properties>
        <java.version>1.8</java.version>
        <junit.version>4.12</junit.version>
        <cucumber.version>1.2.4</cucumber.version>
        <maven.compiler.version>3.3</maven.compiler.version>
    </properties>
<dependencies>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>2.53.0</version>
        </dependency>
        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>commons-validator</groupId>
            <artifactId>commons-validator</artifactId>
            <version>1.5.1</version>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>${cucumber.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>${cucumber.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.10</version>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-testng</artifactId>
            <version>1.2.0</version>
        </dependency>
        <dependency>
        <groupId>org.aeonbits.owner</groupId>
        <artifactId>owner-java8</artifactId>
        <version>1.0.6</version>
    </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.51.0</version>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java8</artifactId>
            <version>1.2.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-picocontainer</artifactId>
            <version>1.2.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <type>maven-plugin</type>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-firefox-driver</artifactId>
            <version>2.51.0</version>
        </dependency>
        <dependency>
            <groupId>net.masterthought</groupId>
            <artifactId>cucumber-reporting</artifactId>
            <version>3.4.0</version>
        </dependency>
    </dependencies>

The methods that I'm trying to use for this:我试图为此使用的方法:

public void scrollToBottom(){

        JavascriptExecutor js = (JavascriptExecutor) driver;
        HashMap<String, String> scrollObject = new HashMap<>();
        scrollObject.put("direction", "down");
        js.executeScript("mobile: scroll", scrollObject);
    }

    public void swipeVertical(AppiumDriver driver, double startPercentage, double finalPercentage, double anchorPercentage, int duration) throws Exception {
        Dimension size = driver.manage().window().getSize();
        int anchor = (int) (size.width * anchorPercentage);
        int startPoint = (int) (size.height * startPercentage);
        int endPoint = (int) (size.height * finalPercentage);
        new TouchAction(driver).press(anchor, startPoint).waitAction((3000)).moveTo(anchor, endPoint).release().perform();
    }

Time after time I receive the next error:我一次又一次收到下一个错误:

java.lang.NullPointerException

Can anybody help me?有谁能够帮我?

You need to add the element as well in the hashmap to swipe the page up or down.您还需要在 hashmap 中添加元素以向上或向下滑动页面。 Appium will hold that element and swipe up/down. Appium 将持有该元素并向上/向下滑动。
You can do it like:你可以这样做:

 JavascriptExecutor js = (JavascriptExecutor) driver;
 HashMap<String, String> scrollObject = new HashMap<>();
 scrollObject.put("direction", "up");
 scrollObject.put("element", ((RemoteWebElement) driver.findElementByAccessibilityId("id")).getId());
 js.executeScript("mobile: swipe", scrollObject);

The only useful method for the dependencies above is the next:对上述依赖项唯一有用的方法是下一个:

TouchAction action = new TouchAction(driver());
        AndroidDriver<WebElement> driver = (AndroidDriver<WebElement>) driver();
        int x = driver.manage().window().getSize().getWidth()/2;
        int y = driver.manage().window().getSize().getHeight()/2;
        action.longPress(x, y).moveTo(x, y-(y)).release().perform();

The bad thing is the swipe is slow.坏处是滑动很慢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM