【问题标题】:How to perform scroll in appium 1.6.4 and java client 5.0.0 beta7如何在 appium 1.6.4 和 java 客户端 5.0.0 beta7 中执行滚动
【发布时间】:2017-09-13 20:34:12
【问题描述】:

请为我提供可能的替代方法,以在不使用滑动方法和触摸动作类的情况下上下滚动 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>

【问题讨论】:

    标签: java android selenium testing appium


    【解决方案1】:

    使用下面的代码向下滚动到一个元素

    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);
            }
        }
    

    【讨论】:

    • 感谢您提供这样的选择
    • 如果它回答了您的查询,则接受它作为“答案”
    猜你喜欢
    • 1970-01-01
    • 2019-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-24
    • 2017-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多