简体   繁体   中英

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

Hi community: I'm fighting with a mobile application in 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.

    <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. Appium will hold that element and swipe up/down.
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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