让所有的孩子指出在Appium父对象仅低一个级别(Android版) [英] Get all child notes only one level lower from parent object in Appium (Android)

查看:850
本文介绍了让所有的孩子指出在Appium父对象仅低一个级别(Android版)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个自动化测试软件,现在有一个问题。
在我的实际本机应用程序我有这样一个在这里的ListView:


  • 的ListView

    • 的LinearLayout

      • 的LinearLayout

        • 的LinearLayout


      • 的TextView


    • 的LinearLayout

      • 的LinearLayout


    • 的LinearLayout

      • 的LinearLayout


    • 的LinearLayout

    • ...


...等。

我需要什么,要的是让所有低一个级别的的LinearLayout (迭代的人,可见和不可见元素)从的ListView

ListView控件:{LinearLayout中,LinearLayout中,...}

使用了code我从ListView中所有孩子的,但其很多对我来说:

  listViewChildElements =[...] android.widget.ListView [1] // android.widget.LinearLayout
app.getDriver()findElements(By.xpath(listViewChildElements));

在另一篇文章中我看到计数的东西,但我不明白它。

感谢您的帮助,很高兴见到你。


:在右侧车道的第1步

通过@ har07的帮助下,我写一个函数,而不是最好的最优秀的或解决方案,并没有最终的,但

 公共静态列表<串GT; getAllViewElements(应用程序){
    INT I = 0;    清单<串GT; writtenElementsByText =新的ArrayList<串GT;();    而(真){
        如果(!writtenElementsByText.isEmpty()){
            串recentLastItem = writtenElementsByText.get(writtenElementsByText.size() - 1);
            如果(recentLastItem.equals(ALLE Themen))
                打破;
            其他
                。app.getDriver()scrollTo(recentLastItem);
        }        清单< WebElement> TMP = app.getDriver()findElements(By.xpath(listViewChildElements));
        如果(writtenElementsByText.isEmpty())
            对于(WebElement E:TMP){
                writtenElementsByText.add(getTextFromElement(e)条);
            }
        其他
            对于(WebElement E:TMP){
                字符串activualElemenByText = getTextFromElement(E);
                如果(!writtenElementsByText.contains(activualElemenByText))
                    writtenElementsByText.add(activualElemenByText);
            }
    }    的System.out.println(writtenElementsByText.size());
    对(串E:writtenElementsByText){
        的System.out.println(\\ T- \\ t+ E);
    }
    返回writtenElementsByText;
}公共静态字符串getTextFromElement(WebElement元素){
    返回element.findElement(By.id(listViewChildElementTextID))的getText()。
}


解决方案

如果我理解正确的问题,你想使用单斜杠( / )而不是双至从父元素得到直接子元素:

  listViewChildElements =[...] android.widget.ListView [1] /android.widget.LinearLayout
                                                        ^
                                                        请注意,只有一个斜线是
                                                        上面使用

通过这种变化,中的XPath现在应该返回的LinearLayout 这就是 ListView控件的直接子[1]

I write an automation testing software and have one problem now. In my actual native app I have a ListView like this here:

  • ListView
    • LinearLayout
      • LinearLayout
        • LinearLayout
      • TextView
    • LinearLayout
      • LinearLayout
    • LinearLayout
      • LinearLayout
    • LinearLayout
    • ...

... and more.

What I need and want is to get all one level lower LinearLayout (iterative ones, visible and non visible elements) from ListView:

ListView : {LinearLayout, LinearLayout, ...}

With that code I get all childs from ListView, but its to much for me:

listViewChildElements       = "[...]android.widget.ListView[1]//android.widget.LinearLayout";
app.getDriver().findElements(By.xpath(listViewChildElements));

In another post I see something with 'count', but I don't understand it.

Thanks for your help and nice to meet you.


With the help of @har07 I write one function, not best or finest solution and not finally, but the 1st step in the right lane:

    public static List<String> getAllViewElements(App app){
    int i = 0;

    List<String> writtenElementsByText = new ArrayList<String>();

    while(true){
        if(!writtenElementsByText.isEmpty()){
            String recentLastItem = writtenElementsByText.get(writtenElementsByText.size()-1);
            if(recentLastItem.equals("Alle Themen"))
                break;
            else
                app.getDriver().scrollTo(recentLastItem);
        }

        List<WebElement> tmp = app.getDriver().findElements(By.xpath(listViewChildElements));


        if(writtenElementsByText.isEmpty())
            for(WebElement e: tmp){
                writtenElementsByText.add(getTextFromElement(e));
            }
        else
            for(WebElement e: tmp){
                String activualElemenByText = getTextFromElement(e);
                if(!writtenElementsByText.contains(activualElemenByText))
                    writtenElementsByText.add(activualElemenByText);
            }
    }

    System.out.println(writtenElementsByText.size());
    for(String e: writtenElementsByText){
        System.out.println("\t-\t"+e);
    }
    return writtenElementsByText;
}

public static String getTextFromElement(WebElement element){
    return element.findElement(By.id(listViewChildElementTextID)).getText();
}

解决方案

If I understand the problem correctly, you want to use single slash (/) instead of double to get direct child element from a parent element :

listViewChildElements = "[...]android.widget.ListView[1]/android.widget.LinearLayout";
                                                        ^
                                                        notice that only one slash being 
                                                        used above

With that change, the XPath should now return LinearLayout that is direct child of the ListView[1].

这篇关于让所有的孩子指出在Appium父对象仅低一个级别(Android版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆