1

I am doing automation for my app. Here I am selecting a particular game.. here list array contain 9 data .. like I am fetch gui data and put it into array... how to keep the index size as 3 for outer list from inner list contain 9 data....fetch data from oracle db code :

game_date=result.getString(1);
home_team_name=result.getString(2);
away_team_name=result.getString(3);




WebElement listView = AppSession.findElementByAccessibilityId("ListView1");
List<WebElement> row = listView.findElements(By.tagName("./*[contains(@LocalizedControlType, 'item')]"));
ArrayList<ArrayList<String>> outerList = new ArrayList<ArrayList<String>>();
ArrayList<String> innerList = new ArrayList<String>();
for (int a = 0; a < row.size(); a++) {
    List<WebElement> column = row.get(a).findElements(By.tagName("./*[contains(@LocalizedControlType, 'text')]"));
    for (int j = 0; j < column.size(); j++) //take first column
    {
        innerList.add(column.get(j).getAttribute("Name"));
    }
}
outerList.add(innerList);
System.out.println(innerList);
System.out.println(innerList.size());//9
System.out.println(outerList);
System.out.println(outerList.size()); //1
for (int i = 0; i < outerList.size(); i++) {
    if ((outerList.get(i).contains(game_date)) && (outerList.get(i).contains(home_team_name)) && (outerList.get(i).contains(away_team_name))) {
        WebElement line_up1 = AppSession.findElementByName(game_date);
        line_up1.click();
    }
}

this is how my gui app looks like for game selection...

 Game date                   home team             away team
    06/29/2018 07:00:00 PM     Ball Hogs Ball Hogs    Tri-State Tri-State
    06/29/2018 08:00:00 PM     Power                  Ghost BALLERS
    06/29/2018 07:00:00 PM     Killer 3's             3's Company

if i can use inner array list. it is better way to write ..

My expected output of outer list should be like this ::

[[06/29/2018 07:00:00 PM ,Ball Hogs Ball Hogs ,Tri-State Tri-State],[06/29/2018 08:00:00 PM, Power, Ghost BALLERS],[06/29/2018 07:00:00 PM, Killer 3's, 3's Company]]

I am getting outerlist loop like this ::

[06/29/2018 07:00:00 PM ,Ball Hogs Ball Hogs ,Tri-State Tri-State,06/29/2018 08:00:00 PM, Power, Ghost BALLERS,06/29/2018 07:00:00 PM, Killer 3's, 3's Company]

I want to take the entire data in the first row of GUI as index 1 and so i can take 3 index and loop that index and check the game and click...

  • store db data in three dimensional array list and do same for gui data , then compare in for loop – sameer joshi Sep 4 '18 at 4:59
  • You should rework your question. It seems to me that you mix a lot of things here. You should keep the only things which are relevant to your question. The title of your question is very broad and do not reflect the body. Also explain why is using ArrayList a requirement? Try to formulate your question using 10-15 words. Here we have too much details which confuse everyone and don't let us give you a proper answer. – Alexey R. Sep 4 '18 at 10:24
  • @AlexeyR. please check my question.. – user32519 Sep 4 '18 at 11:27
  • @AlexeyR. please check my question.. I am not getting.. – user32519 Sep 4 '18 at 13:34
  • In your title you asking about ArrayList. You also mention that your senior told you to use multi-dimensional array. Arrays and ArrayLists is not the same thing. So What-Is-Your-Question again. What do you want to achieve? The only thing I've got so far is that you have UI with game date, home team and away team. All other parts of your question look odd to me. You start from that you describe when your script would fail (why do we need to know that) and end with that you want click elements in the loop (why do we need to know that)? Which parts are relevant to your issue and which are not? – Alexey R. Sep 4 '18 at 16:45
1

Looks like I got your point. So, if your issue is that you see

[06/29/2018 07:00:00 PM ,Ball Hogs Ball Hogs ,Tri-State Tri-State,06/29/2018 08:00:00 PM, Power, Ghost BALLERS,06/29/2018 07:00:00 PM, Killer 3's, 3's Company]

but you expect to see

[[06/29/2018 07:00:00 PM ,Ball Hogs Ball Hogs ,Tri-State Tri-State],[06/29/2018 08:00:00 PM, Power, Ghost BALLERS],[06/29/2018 07:00:00 PM, Killer 3's, 3's Company]]

Then the solution is simple..

Here is your problem:

enter image description here

First of all 1 - rename the variable. What you call column here is actually a row. You have a row list in your row variable and iterate row by row taking row.get(a). This is not logical issue, but it doesn't let one read your code effectively.

Then 2 - you populate your outer list outside the loop. This is why it has the only one item. You should move ArrayList<String> innerList = new ArrayList<String>(); to the first line of outer loop so that new instance is created for each row. You also should move outerList.add(innerList); to the last line of outer loop so that it adds new inner list at the end of a cycle.

So, instead of:

    ArrayList<ArrayList<String>> outerList = new ArrayList<ArrayList<String>>();
    ArrayList<String> innerList = new ArrayList<String>();
    for (int a = 0; a < row.size(); a++) {
        List<WebElement> column = row.get(a).findElements(By.tagName("./*[contains(@LocalizedControlType, 'text')]"));
        for (int j = 0; j < column.size(); j++) //take first column
        {
            innerList.add(column.get(j).getAttribute("Name"));
        }
    }
    outerList.add(innerList);
    System.out.println(innerList);
    System.out.println(innerList.size());//9
    System.out.println(outerList);

You should have:

    ArrayList<ArrayList<String>> outerList = new ArrayList<ArrayList<String>>();
    for (int a = 0; a < row.size(); a++) {
        ArrayList<String> innerList = new ArrayList<String>();
        List<WebElement> column = row.get(a).findElements(By.tagName("./*[contains(@LocalizedControlType, 'text')]"));
        if(!column.isEmpty()){
            for (int j = 0; j < column.size(); j++)
            {
                innerList.add(column.get(j).getAttribute("Name"));
            }
            outerList.add(innerList);
        }
    }
    System.out.println(outerList);
  • please check my question – user32519 Sep 4 '18 at 12:58
  • @Anthony updated – Alexey R. Sep 5 '18 at 7:59
  • [[], [], [],[06/29/2018 07:00:00 PM ,Ball Hogs Ball Hogs ,Tri-State Tri-State],[06/29/2018 08:00:00 PM, Power, Ghost BALLERS],[06/29/2018 07:00:00 PM, Killer 3's, 3's Company]] it is printing like this.. how to remove the blank.. – user32519 Sep 5 '18 at 13:34
  • @Anthony obviously you fetch empty values on first three cycles of your loop. Fix the way how you obtain your row's. This line row.get(a).findElements(By.tagName("./*[contains(@LocalizedControlType, 'text')]")); returns empty list for first three iterations – Alexey R. Sep 5 '18 at 13:44
  • @Anthony check updated code – Alexey R. Sep 5 '18 at 13:46
2

I would suggest model your GUI to a simple java class like below:

  • override the equals and hashcode method
  • use getters and setters to extract values at runtime.

    public class MyGame {
    
    Date gameDate;
    String homeTeam;
    String awayTeam;
    
    public MyGame(Date gameDate, String homeTeam, String awayTeam) {
        this.gameDate = gameDate;
        this.homeTeam = homeTeam;
        this.awayTeam = awayTeam;
    }
    
    public Date getGameDate() {
        return gameDate;
    }
    
    public void setGameDate(Date gameDate) {
        this.gameDate = gameDate;
    }
    
    public String getHomeTeam() {
        return homeTeam;
    }
    
    public void setHomeTeam(String homeTeam) {
        this.homeTeam = homeTeam;
    }
    
    public String getAwayTeam() {
        return awayTeam;
    }
    
    public void setAwayTeam(String awayTeam) {
        this.awayTeam = awayTeam;
    }
    
    @Override
    public boolean equals(Object o) {
        // Your own implementation
        return false;
    }
    
    @Override
    public int hashCode() {
        return Objects.hash(gameDate, homeTeam, awayTeam);
    }}
    

After this hopefully you can perform any operation you want.

  • Here code is very lengthy...I want to just check the one row of data conatin date, hometeam, and awayteam from db....how to use mulitdimensional array list – user32519 Sep 3 '18 at 17:36
  • @Anthony It's lengthy but self-explanatory. It's easier to read and fix the code that is self-explanatory, than more cryptic structures like ArrayList<ArrayList<String>> outerList. It's easier to read and fix the code that does not mix levels of abstractions as in innerList.add(column.get(j).getAttribute("Name"));. – dzieciou Sep 5 '18 at 8:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy