What is the better way to write the code for my app?

By | July 12, 2018

enter image description here

I am automating my app. I want to know better way to automate my app. My application is a reporter software. I am selecting 11 players and the players names , I put into excel file and I am calling through excel and selecting the player. I have two team like Belgium and portugual. Here home team is belgium and away team is portugual. By default the radio button is selecting for belgium team.

enter image description here

public class SoccerTest {

    int maximum2;
    int maximum;
    int max1;
    String player2;
    String player1;
    public static void main(String[] args) throws InterruptedException, Exception {
        // TODO Auto-generated method stub

        DesktopOptions opt=new DesktopOptions();
        opt.setApplicationPath("E:\\setup.exe");

        WiniumDriver driver = new WiniumDriver(new URL("http://localhost:9999"),opt);
        File fr = new File("new.xlsx");
        FileInputStream st = new FileInputStream(fr);
        XSSFWorkbook br = new XSSFWorkbook(st);
        XSSFSheet sheet1 = br.getSheetAt(1);
        String username = sheet1.getRow(0).getCell(0).getStringCellValue();
        String pass = sheet1.getRow(0).getCell(1).getStringCellValue();
        Thread.sleep(10000);
        driver.findElementById("txtUsername").sendKeys(username);
        driver.findElementById("txtPassword").sendKeys(pass);
        driver.findElementById("btnLogin").click();


         driver.findElementByName("Yes").click();
           Thread.sleep(15000);
           String game =  sheet1.getRow(0).getCell(2).getStringCellValue();
           driver.findElement(By.name(game)).click();
           driver.findElement(By.id("btnLogin")).click();

           Thread.sleep(30000);
           driver.findElementByName("Setup").click();
           Thread.sleep(10000);
           driver.findElementByName("Team Setup").click();



           for(int e=1; e<=2;e++)
           {
               System.out.println(e);

           WebElement team = driver.findElement(By.id("lblTeam")); 
           String visible_team = team.getAttribute("Name");
           System.out.println(visible_team);//Belgium

           WebElement teams = driver.findElementById("pnlHeader");
          WebElement team1 = teams.findElement(By.id("radHomeTeam"));    
           System.out.println(team1);
           String homeTeam = team1.getAttribute("Name");
              System.out.println(homeTeam);
           WebElement team2 = teams.findElement(By.id("radAwayTeam"));
           System.out.println(team2);
           String awayTeam = team2.getAttribute("Name");
           System.out.println(awayTeam);

           //belgium--home team, port-awayteam


           if(visible_team.equals(homeTeam) || visible_team.equals(awayTeam) )
           {
    WebElement e2= driver.findElement(By.id("dgvStartingLineup"));

              List<WebElement> bench = e2.findElements(By.xpath("//*[contains(@Name,'Player Row')]"));
              System.out.println(bench.get(0));
              if(e==2)
              {
           //      soc.maximum2 = sheet1.getRow(4).getLastCellNum();
               //  System.out.println(soc.maximum2);
              }
              else
              {
             //    soc.max1 = sheet1.getRow(2).getLastCellNum();
             //System.out.println(soc.max1);
              }


             int ben = bench.size();
             int a=0;


           //  while(a<ben & a<soc.max1 ||a<soc.maximum2)
             {


                 System.out.println(a);
                String s1 = bench.get(a).getAttribute("Name");
                  System.out.println(s1);
                  e2.findElement(By.name(s1)).click();

                  //Thread.sleep(5000);
                 e2.findElement(By.id("DropDown")).click();



                 int c=0;
                while(c<=a)
                 {
                    if(e==2)
                    {
                       /*String []player2 = {sheet1.getRow(4).getCell(c).getStringCellValue()};*/
                 //       soc.player2 = sheet1.getRow(4).getCell(c).getStringCellValue();
                    }
                    else
                    {
                       /*String []player1 = {sheet1.getRow(2).getCell(c).getStringCellValue()};*/
                   //    soc.player1 = sheet1.getRow(2).getCell(c).getStringCellValue();
                    }
                     //System.out.println(player1.length);
                     /*int player_list = player1.length;*/
                     for(int b=0; b< 1/*player_list*/; b++)
                     {
                         //System.out.println(player1[b]);

                           WebElement roster = e2.findElement(By.id("ListBox"));
                           WebElement r1 = roster.findElement(By.name("Editing Control"));
                           WebElement r2 = r1.findElement(By.id("ListBox"));
                           List<WebElement> roster1 = r2.findElements(By.xpath("//*[@LocalizedControlType='list item']"));

                           for(int d=0; d<roster1.size(); d++)
                           {
                               String s2 = roster1.get(d).getAttribute("Name");

                              // if(s2.equals(soc.player1  /*player1[b]*/   ) ||  s2.equals(soc.player2))
                               {

                                   //System.out.println(s1);
                                   roster1.get(d).click();
                                   Thread.sleep(5000);

                               }
                           }

                     }
                     c++;

                 }
                a++;
             }

    }
           driver.findElement(By.id("radAwayTeam")).click();
            Thread.sleep(10000);
       }


       }
   }