0

我想使用 appium 打印或阅读完整的联系人列表(假设我的 .apk 是一个联系人应用程序,它显示完整的 A 到 Z 联系人,通过滚动我可以查看所有联系人)。

我能够计算/打印第一个屏幕中显示的联系人(即显示一些联系人的屏幕默认情况下说 10 个联系人,更多我必须滚动)。我已经解决了上述问题并且它工作了一段时间但是之后它抛出一个错误,请帮助如何纠正这个问题 java.lang.IndexOutOfBoundsException: Index: 9, Size: 9

我的代码是`

AppiumDriver 驱动程序 = null; ArrayList 值 = 新 ArrayList();

    DesiredCapabilities cap = new DesiredCapabilities();
    cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Galaxy S4");
    cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
    cap.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4.4");
    cap.setCapability(MobileCapabilityType.APP_PACKAGE, "com.brainworks.contacts");
    cap.setCapability(MobileCapabilityType.APP_ACTIVITY, "com.brainworks.contacts.ui.Main");

    driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"),cap);
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    List<WebElement> allContactsOnfirstScreen = driver.findElements(By.xpath("//*[@resource-id='com.brainworks.contacts:id/txt_name']"));
    driver.context("NATIVE_APP");
    Dimension size = driver.manage().window().getSize();
    int StartY = (int)(size.height * 0.70);
    int EndY = (int)(size.height * 0.55);
    int StartX = size.width/2;

    for(int i =0;i<200;i++){
        System.out.println("Contacts are = " + allContactsOnfirstScreen.get(i).getAttribute("text"));
        String Values = allContactsOnfirstScreen.get(i).getAttribute("text");
        values.add(Values);
        System.out.println("Value is = " + values);
        String ext = Values;
        String [] a = ext.split(",");
        String [] b = a[a.length-1].split("//]");
        System.out.println("val = " + b[0]);
        driver.swipe(StartX, StartY, StartX, EndY , 1000);
    }`

我尝试过的第二种方法

            AppiumDriver driver = null;

    // desired cap as above in the first try

    // same as above -but now what i am doing is i am printing the list in for loop
            // then do a swipe form last element to first element then again print the new list 
            // and this cycle keeps on running until i reach the end of the list or the parameter
            // in the if loop.

    int XOfFirstElement = allContactsOnfirstScreen.get(0).getLocation().getX();
    int YOfFirstElement =  allContactsOnfirstScreen.get(0).getLocation().getY();

    System.out.println("X for XOfFirstElement = " + XOfFirstElement);
    System.out.println("Y for YOfFirstElement = " + YOfFirstElement);

    int XOfLastElement = allContactsOnfirstScreen.get(allContactsOnfirstScreen.size()-1).getLocation().getX();
    int YOfLastElement =  allContactsOnfirstScreen.get(allContactsOnfirstScreen.size()-1).getLocation().getY();

    System.out.println("X for XOfLastElement = " + XOfLastElement);
    System.out.println("Y for YOfLastElement = " + YOfLastElement);

    int XOfSecondLastElement = allContactsOnfirstScreen.get(allContactsOnfirstScreen.size()-2).getLocation().getX();
    int YOfSecondLastElement =  allContactsOnfirstScreen.get(allContactsOnfirstScreen.size()-2).getLocation().getY();

    System.out.println("X for XOfSecondLastElement = " + XOfSecondLastElement);
    System.out.println("Y for YOfSecondLastElement = " + YOfSecondLastElement);

    while(true){
        for(int i = 0 ;i<allContactsOnfirstScreen.size();i++){
            System.out.println("Value is for " + allContactsOnfirstScreen.get(i).getAttribute("text")); 
        }
        driver.swipe(XOfLastElement, YOfLastElement, XOfFirstElement, YOfFirstElement, 1000);
        Thread.sleep(1000L);
        String LastName = allContactsOnfirstScreen.get(allContactsOnfirstScreen.size()-1).getAttribute("text");
        if(LastName.equals("MyLastContactText"))
            break;
    }

现在的问题是 1.> 一切都按预期工作,但有时会打印重复值(联系人)(可能是由于该联系人的坐标在滑动后没有改变) 2. 我无法每次打印最后一个联系人姓名,因为我在 while 循环中的 if 语句将我带出并且脚本停止。

如果有人可以提供帮助,那么如果有任何新方法,那就太好了,请让我知道提前谢谢

4

1 回答 1

0

我想,按照代码片段,问题String [] b = a[a.length-1].split("//]");在行。您可能会收到 0 a.length,这属于异常。您能否a在此行之前添加打印变量?

另外,如果这不是问题,您能否在帖子中添加完整的异常堆栈?

于 2015-06-09T08:28:03.143 回答