如何在appium 1.6.4和java客户端5.0.0 beta7中执行滚动

时间:2017-04-18 06:08:52

标签: java android selenium testing appium

Plz为我提供了可选的数量,可以在不使用滑动方法和触摸动作类的情况下向上和向下滚动Android屏幕。 我无法在Android应用程序版本21中执行滚动功能。 以下是我的代码。

package com.raaga.demo;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;

import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;

public class MainTest {
    private AndroidDriver<WebElement> driver;

    @Test
    public void setUp() {
        File apkFile = new File("./Raaga.apk");
        DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability("platform", "Android");
        cap.setCapability("deviceName", "Andriod Device");
        cap.setCapability("app", apkFile.getAbsolutePath());
        cap.setCapability("commandTimeOut", "2*60");
        cap.setCapability("appPackage", "com.raaga.android");
        cap.setCapability("appActivity", "com.raaga.android.SplashScreen");

        try {
            driver = new AndroidDriver<WebElement>(new URL("http://0.0.0.0:4723/wd/hub"), cap);
        } catch (MalformedURLException e) {

            e.printStackTrace();
        }
        try {
            driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
            driver.findElement(By.id("com.raaga.android:id/skip_text")).click();
            driver.findElementById("com.raaga.android:id/landing_skip_to_raaga").click();
            List<WebElement> ele = driver.findElements(By.className("android.widget.TextView"));
            System.out.println(ele.size());
            System.out.println(ele.get(1).getText());
            ele.get(1).click();
            driver.findElementById("com.raaga.android:id/toolbar_logo").click();
            System.out.println(driver.findElementById("com.raaga.android:id/menu_music_btn").getSize());
            driver.findElementById("com.raaga.android:id/menu_music_btn").click();
            System.out.println(driver.findElementById("com.raaga.android:id/slidingmenumain").getSize());
            System.out.println(driver.getCapabilities());
            Thread.sleep(30000);
         //   driver.findElement(MobileBy.AndroidUIAutomator("new UiSelector().text()"));
            TouchAction ac=new TouchAction(driver);
            ac.moveTo(driver.findElement(By.id("com.raaga.android:id/artists_radio_lay1"))).release().perform();



//          Boolean result = driver.findElement(By.xpath("//android.widget.TextView[@index='0']")).isDisplayed();
//          while (result) {
//              driver.swipe(0, 853, 0, 122, 2000);
//              if (result==true) {
//                  
//                  break;
//              }
//
//          }


        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

}

下面是我在项目pom.xml文件中使用的依赖项

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.demo</groupId>
    <artifactId>RagaAppTesting</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.3.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.json/json -->
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20160810</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>20.0</version>
        </dependency>

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

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.10</version>
            <scope>test</scope>

        </dependency>

    </dependencies>

</project>

1 个答案:

答案 0 :(得分:0)

使用以下代码向下滚动到元素

public void scrollDownToAnElement(String object) throws InterruptedException{
        try{
            driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
            boolean loop=true;
            int count=0;
            while(loop){
                if(count==5)
                    break;
                if(getDriver().findElements(By.id(object)).size()>0){
                    loop=false;
                }
                else{
                    Dimension size = getDriver().manage().window().getSize();
                    System.out.println(size);
                    //Find swipe start and end point from screen's with and height.
                    //Find starty point which is at bottom side of screen.
                    int starty = (int) (size.height * 0.80);
                    //Find endy point which is at top side of screen.
                    int endy = (int) (size.height * 0.30);
                    //Find horizontal point where you wants to swipe. It is in middle of screen width.
                    int startx = size.width / 2;
                    System.out.println("starty = " + starty + " ,endy = " + endy + " , startx = " + startx);
                    //Swipe from Bottom to Top.
                    driver.swipe(startx, starty, startx, endy, 3000);
                    Thread.sleep(1000);
                    count++;
                }
                //Swipe from Top to Bottom.
                //        ((AndroidDriver) driver).swipe(startx, endy, startx, starty, 3000);
            }
        }catch(Exception e){
            throw e;
        }
        finally{
            driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        }
    }