0

]I'm automating baseball sports reporter application. My application is a desktop application. I'm using winium tool with java language. In my application table contain player names in alphabetical order. I want to select 8 players from the list in same order as you can see above order. And also I want to select the matching positions.

These are the players that i need to select::

  #players names  ::  positions
 Happ            = CF
 Bryant          = 3B
 Rizzo           = 1B
 Contreras       = C
 Schwarber       = LF
 Russell         = SS
 Heyward         = RF
 Baez            = 2B

now i need to select the particular players that I mentioned above list order. My application manual working process is first double click a player and mean time a window came to select the position by single click. example firstly , I need to select the player Happ and his position CF(center field) then select the player Bryant and his position 3B(third base) etc...

     public void awayTeamHitters() {


            try
            {
            String[] players = new String[]
                    {
                    "Happ, Ian#",
                    "Bryant, Kris",
                    "Rizzo, Anthony*",
                    "Contreras, Willson",
                    "Schwarber, Kyle*",
                    "Russell, Addison",
                    "Heyward, Jason*",
                    "Baez, Javier"
                    };
            String[] positions=new String[]
                    {
                            "CF",
                            "3B",
                            "1B",
                            "C",
                            "LF",
                            "SS",
                            "RF",
                            "2B"
                    };


                List<String> playersInList = Arrays.asList(players);
                List<String> positionsInlist=Arrays.asList(positions);
//selecting players   of 2 table
                driver.findElement(By.id("lblAwayTeamHittersAll")).click();  // click on 2 tables all players
                WebElement table1 = driver.findElement(By.id("lsvAwayTeamHitters1"));
                WebElement table2 = driver.findElement(By.id("lsvAwayTeamHitters2"));
                //taking row size
                List<WebElement> rows1 = table1.findElements(By.xpath("./*[contains(@LocalizedControlType, 'item')]"));
                List<WebElement> rows2 = table2.findElements(By.xpath("./*[contains(@LocalizedControlType, 'item')]"));
                //create string array add players to player_table1
                List<String> player_table1=new ArrayList<String>();
                for(int i=0;i<rows1.size();i++) {
                    List<WebElement> cols1 = rows1.get(i).findElements(By.xpath("./*[contains(@LocalizedControlType, 'text')]"));
                    for(int j=3;j<cols1.size();j++) {
                        String celtext1 = cols1.get(j).getAttribute("Name");
                        player_table1.add(celtext1);
                    }      
                }
                //create string array and add players to player_table2
                List<String> player_table2=new ArrayList<String>();
                for(int i=0;i<rows2.size();i++) {
                    List<WebElement> cols2 = rows2.get(i).findElements(By.xpath("./*[contains(@LocalizedControlType, 'text')]"));
                    for(int j=3;j<cols2.size();j++) {
                        String celtext2 = cols2.get(j).getAttribute("Name");
                        player_table2.add(celtext2);
                    }                     
                }


        //combine player table 1 and 2 
                player_table2.addAll(player_table1);  
                List<String> player_table3=new ArrayList<String>();
                player_table3.addAll(player_table2);
                System.out.println(player_table3);  //it will print the 25 player names
                int p=player_table3.size();  
                System.out.println(p);     //it will print the size 25
                for(int i=0;i<p;i++) {
                 if(playersInList.contains(player_table3)) {

                    Actions act = new Actions(driver);

                        act.doubleClick(playersInList).build().perform();
                 }

                   WebElement pos= driver.findElementById("PlayerPositions");
                           List<WebElement> col=pos.findElements(By.xpath("./*[contains(@LocalizedControlType, 'button')]"));

                           if(positionsInlist.contains(col)) {


                           pos.click();

                   }
                }      


        }
        catch (Exception e) {
                System.out.println(e);
                                }
    }
}

I have 2 tables in my application and I add it into one list.. Here double click action is not performing and after printing 25 player names and size .it will got stop.

10
  • Is this the same problem as this one? sqa.stackexchange.com/questions/33483/…
    – Pieter A
    May 21, 2018 at 5:50
  • @PieterA no.. it is different . now i need to automate only selected particular player. i have just take the name from xpath and print it it console and then need to store the name in location then select the players ..this is my scenario ..
    – user32519
    May 21, 2018 at 5:56
  • So what is the issue? You wonder how do you store the list of players? Use String array for example. Or List of Strings. Or clarify what your issue is.
    – Alexey R.
    May 21, 2018 at 7:42
  • @AlexeyR. my issue is that I want to select only 9 players from the list. how to perform this ? i want to select 9 players and their position. i have confuse to go through the next step...whether what should i do ?
    – user32519
    May 21, 2018 at 11:07
  • So your issue is selecting in a proper order or only from a predefined set of players or what? there is just a code snippet with few comment lines. Point us out where exactly in your code you're starting to experience difficulties.
    – Alexey R.
    May 22, 2018 at 11:16

1 Answer 1

0

To select only particular player names you need to:

  1. Store the required names somewhere (e.g. in String array)
  2. When get the next player name check if it matches any of your preserved player from the list.

This might look like:

{
    String[] players = new String[]{
            "Player 1",
            "Player 2",
            "Player 3",
            // ...
            "Player N"
    };

    try {
        List<String> playersInList = Arrays.asList(players);
        WebElement table = driver.findElement(By.xpath(".//*[@AutomationId='lsvAwayTeamHitters1']"));
        List<WebElement> rows = table.findElements(By.xpath("./*[contains(@LocalizedControlType, 'item')]"));
        for (WebElement row : rows) {
            List<WebElement> cols = row.findElements(By.xpath("./*[contains(@LocalizedControlType, 'text')]"));
            for (WebElement col : cols) {
                String celtext = col.getAttribute("Name");
                if (playersInList.contains(celtext)) {
                    // process your player here
                }

            }
        }
        ArrayList<String> inputList = new ArrayList<String>();
        // ... Remaining code
    }
}

I reworked you code slightly since it does not make sense to loop through the items indices. Using iterator is more appropriate here (the logic does not break).

There is also potentially problematic code in your example:

for(int j=3;j<=col.size();j++) {
    String celtext = col.get(j).getAttribute("Name");
    System.out.println(celtext);
}

which I reworked to utilize iterator mechanism, however the logical problems remain in place. Namely you are iterating through all the cells in a row, but there is likely one single row that contains a player name so looping through all the cells might introduce logical errors to the process.

1
  • Please check my question.
    – user32519
    May 26, 2018 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.