Search code examples
javaandroidappiumui-automationmobile-application

strange behavior of indexing of list of LinearLayout element in Appium test writen with JAVA


I am testing a local Android mobile application with Appium, tests are writen in Java.

In the following code I am trying to search for a certain item in a menu list in the app and click. The menu has altogether 4 items, but the listSize is read as 35, and the reading varies from execution to execution. Also, if I do a click on for example menuList.get(0).click(), in the execution it is the second item got the click instead of the first item.

Print out result listed below.

Appium version 1.4.13.1, JDK version 1.8.0_66. Not sure any other information could be relevant, please ask if you wonder.

List<WebElement> menuList = driver.findElementsByClassName("android.widget.LinearLayout");
    int listSize = menuList.size();
    int index = 0;
    Boolean menuFound = false;
    while(!menuFound && index < listSize)
    {
        String label = menuList.get(index).findElement(By.id("no.ruter.reise.qa:id/label")).getText();
        System.out.printf("%d of %d, %s\n", index, listSize, label);
        if (label.equals(menuItem)){
            menuList.get(index).click();
            System.out.printf("\t%s %s\n", menuItem, "click");
            menuFound = true;
        }
        index++;
    }

Print out result:

Test: avgangMinpositionTilStavNaa
0 of 35, Avgangstider
1 of 35, Avgangstider
2 of 35, Avgangstider
3 of 35, Avgangstider
4 of 35, Avgangstider
5 of 35, Avgangstider
6 of 35, Avgangstider
7 of 35, Avgangstider
8 of 35, Avgangstider
9 of 35, Avgangstider
10 of 35, Avgangstider
11 of 35, Avgangstider
12 of 35, Avgangstider
13 of 35, Avgangstider
14 of 35, Avgangstider
15 of 35, Avgangstider
16 of 35, Avgangstider
17 of 35, Avgangstider
18 of 35, Avgangstider
19 of 35, Avgangstider
20 of 35, Avgangstider
21 of 35, Avgangstider
22 of 35, Avgangstider
23 of 35, Avgangstider
24 of 35, Avgangstider
25 of 35, Avgangstider
26 of 35, Avgangstider
27 of 35, Avgangstider
28 of 35, Avgangstider
29 of 35, Avgangstider
30 of 35, Avgangstider
31 of 35, Avgangstider
32 of 35, Favoritter
33 of 35, Kart
34 of 35, Finn reise
    Finn reise click
    Finn reise

Solution

  • You are using "android.widget.LinearLayout" class which is common and your application has 35 layout which class name "android.widget.LinearLayout".

    These layout may be in nested format. You need to create locator for menus, if your all menu locator has id "no.ruter.reise.qa:id/label" you can use below code:

    List<WebElement> menuList = driver.findElementsByClassName("no.ruter.reise.qa:id/label");
    int listSize = menuList.size();
    int index = 0;
    Boolean menuFound = false;
    for(WebElement menu : menuList)
    {
        String label = menu.getText();
        System.out.printf("%d of, %s\n", index,label);
        if (label.equals(menuItem)){
            menu.click();
            System.out.printf("\t%s %s\n", menuItem, "click");
            menuFound = true;
        }
        index++;
    }