-3

I am automating my app. App is a commentary app. I am selecting 3 players and clicking on their respective positions. Here I am able to select the first player and its position, but unable to select the second player with the help of Selenium.

Here is My Code:

//Away team lineups
                            List<String> total = new ArrayList<String>();
                            WebElement awayTeam=AppSession.findElementByAccessibilityId("DgViewAwayTeam");
                            List<WebElement> itemNam =awayTeam.findElements(By.tagName("./*[contains(@LocalizedControlType, 'table')]"));
                            System.out.println(itemNam.size());//1

                            for(int b=0;b<itemNam.size();b++) {

                                 List<WebElement> date = itemNam.get(b).findElements(By.tagName("./*[contains(@LocalizedControlType, 'item')]"));
                                System.out.println(date.size()); //30
                                 for(int z=1;z<date.size();z+=5) {

                                String value=date.get(z).getAttribute("Value.Value");
                            System.out.println(value);
                                total.add(value);
                            }
                            }
    for(int currentPlayerIndexFromUI = 0; currentPlayerIndexFromUI < total.size(); currentPlayerIndexFromUI++)
                            {

                                String game = total.get(currentPlayerIndexFromUI); //  Josh Childress



                                for(int currentPlayerCellIndexInExcel = 1; currentPlayerCellIndexInExcel < 6; currentPlayerCellIndexInExcel += 2)
                                {

                                    String nameFomExcel = event.getRow(i).getCell(currentPlayerCellIndexInExcel).getStringCellValue();

                                    String positionInExcel = event.getRow(i).getCell(currentPlayerCellIndexInExcel + 1).getStringCellValue();


                                    WebElement away = AppSession.findElementByAccessibilityId("DgViewAwayTeam");
                                    List<WebElement> date = away.findElements(By.tagName("./*[contains(@LocalizedControlType, 'item')]"));

                                    if(game.equals(nameFomExcel)){  //initially game= Josh Childress  , nameFomExcel =Andre Owens

                                        System.out.println(nameFomExcel);

                                        for(int z=0;z<date.size();z++) //30
                                        {

                                            String value=date.get(z).getAttribute("Name").substring(0, 1);
                                            System.out.println(value);
                                            if (value.equals(positionInExcel))
                                            {

                                                date.get(z).click();

                                                break;
                                            }

                                        }

                                       break;   
                                    }

                                }
                            }

After selecting first player and clicking on F,C,G..

This is my console showing.

    Josh Childress
U
N
F
C
Corsley Edwards
U
N
F
C
G
Andre Owens
U
N
F

enter image description here

this is my player list .I'm keep it in array list and looping

Josh Childress
Corsley Edwards
Andre Owens
Brian Scalabrine
DeShawn Stevenson
Jermaine Taylor

enter image description here

I am using winapp driver tool, here localisedcontrol type only support tagname.. I am taking tagname from inspect.exe, it is only taking first row, how could i take each row by row ...Here looping is working fine as u can see the console, but click is performing on the first row only, it is not move to click on second player position

Now the problem is date means taking position value from UI, and here date size is 0 to 30, whenever the click happens z loop exit, and then it move to excel data loop and again after exit from that loop .then it will move to first loop , then again comes to z loop . does z loop starts from 0 to 30 ?, thats why the click on the second row is not working...instead of xpath , how do i select the each row ?

  • Are you saying that the first player is selected each time you go through the loop? – Kate Paulk Jul 24 '18 at 11:43
  • @KatePaulk yes, here after click on first player, then it will not go to h loop, it still on z loop and click on first player different position like F,C,G – user24314 Jul 24 '18 at 11:45
  • I don't know why peoples are trying to close this question. In my perspective this is good question. this may resolves by changing some loop conditions. – Bharat Mane Jul 24 '18 at 13:00
  • 3
    @BharatMane I believe because the author did not put enough of effort to formulating the question. Anthony, could you please explain what is h, what is k, what is j and what is z? Probably by showing screenshot of your UI with marking corresponding areas on it. Also please explain where did you take values U and N. What do they mean? They are absent on the screen-shot provided. – Alexey R. Jul 24 '18 at 13:18
  • 2
    I haven't voted to close this (yet), but may do shortly as it's starting to look like a debugging request which is off-topic here. @Anthony, can you please format your question to steer it away from being a "Can you please debug my code for me?" type query. – trashpanda Jul 25 '18 at 10:02
0

Check my review of your code. It looks quire error prone. Please see the inline comments of what could be the problem. Read it in the order they are enumerated.

    for (int h = 0; h < total.size(); h++) {
        String game = total.get(h);

        // 1. Here you set game = "Josh Childress" (for example)

        while (k < 7) {
            outLoop:
            // 6. If we get here after breaking loop we will start from j=1 again
            for (int j = 1; j < 6; j += 2) {
                // 2. Here you start taking values from excel and on the FIRST step you have
                // 3. event.getRow(i).getCell(j=1).getStringCellValue() = "Josh Childress"
                // 7. If we will get here after breaking the loop then our condition "game.equals(names)" will stop being true so that we won't get to the if section that is shown below
                String names = event.getRow(i).getCell(j).getStringCellValue();
                if (game.equals(names)) {   // 4. This condition will be true on the FIRST step, hence the code inside if will execute
                                            // 5. Now assume we're continuing the loop. Lets get back/

                }

            }

        }
    }

Here is the approach I can suggest.

    for(int currentPlayerIndexFromUI = 0; currentPlayerIndexFromUI < total.size(); currentPlayerIndexFromUI++){
        // Here we have items collected on UI and start iterating one by one
        // Take the current value from list
        String game = total.get(currentPlayerIndexFromUI);
        // Now we start iterating cells in excel line
        // We know that first player is placed in the cell with index 1
        for(int currentPlayerCellIndexInExcel = 1; currentPlayerCellIndexInExcel < 6; currentPlayerCellIndexInExcel += 2){
            // Take the next player from excel
            String nameFomExcel = event.getRow(i).getCell(currentPlayerCellIndexInExcel).getStringCellValue();
            // We also know that the position is alsways next to the player in excel, so we do not need "k". We just shiift player cell +1 to get position
            String positionInExcel = event.getRow(i).getCell(currentPlayerCellIndexInExcel + 1).getStringCellValue();

            // Now we check the current player from excel equals to the current player from UI list
            // If no, just move to the next player from excel (move 2 cells to the right in our "i" line)
            if(game.equals(nameFomExcel)){
                // If current player in UI is equal to current player from excel, then we take list of date elements for current player in UI(whatever date elements mean)
                WebElement away = AppSession.findElementByAccessibilityId("DgViewAwayTeam");
                List<WebElement> date = away.findElements(By.tagName("./*[contains(@LocalizedControlType, 'item')]"));
                // start iterating by the list of date list
                for(int z=0;z<date.size();z++) //30
                {
                    // Extract position from current date item
                    String value=date.get(z).getAttribute("Name").substring(0, 1);
                    System.out.println(value);
                    if (value.equals(positionInExcel))
                    {
                        // if the position matches to the player position from excel then click item on UI
                        date.get(z).click();
                        // Our logic assumes there won't be more clicks so we can break current loop
                        break;
                    }

                }
                // After finishing the loop above we are sure that we successfully handled player/position pair and we may break the loop which we have got into after breaking loop above
                // Hence after breaking the current loop we'll move for processing neew player item from UI .
                break;
            }
        }
    }

This approach does not use k and gotos which is always a bad practice.

  • I understand, suppose if i remove the break statement. but after click on position, then I need to check the second player, that means to go loop h.. what should i do ? – user24314 Jul 24 '18 at 15:37
  • @Anthony check the update – Alexey R. Jul 24 '18 at 16:18
  • can you check my UI screenshot. still I'm not able to select the second player. it is selecting first player F, C, G..I update the console – user24314 Jul 25 '18 at 4:34
  • @ Alexey R. this code am put into my code, but still not breaking...not go to second player and select C.. please help me – user24314 Jul 25 '18 at 6:18
  • Did you do debugging? I believe the problem is outside "for" and not related to "break". Probably in how you compose "total" list or in your "event" whatever that object is. Put some effort to make a debugging and let me know what is wrong with the solution I have provided. – Alexey R. Jul 25 '18 at 9:44

Your Answer

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