java – How to test menu and submenu selenium webdriver

I am making a test script, during which all menu items must be clicked. If items have nested submenus, they also must be clicked.
I understand that I need to do something through the “if”. If there is a submenu, then run a function that will go through them, if there are none, then repeat the current one, but I don’t know java very well and don’t understand how to implement it. Now my functions are simply driven in a circle through one menu item and its submenus. Thanks for the help!
Link: demo.litecart.net/admin
Leftside menu

Code:

List<WebElement> elementList = driver.findElements(By.cssSelector("#box-apps-menu li a"));
       int numberOfMenuElements = elementList.size();
       for (int i = 0; i < numberOfMenuElements; i++) {
           elementList = driver.findElements(By.cssSelector("#box-apps-menu li a"));
           elementList.get(i).click();
           wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("h1")));

           List<WebElement> subMenuElementsList = driver.findElements(By.cssSelector(".docs>li"));
           int numberOfsubMenuElements = subMenuElementsList.size();
           for (int j = 0; j < numberOfsubMenuElements; j++) {
               subMenuElementsList = driver.findElements(By.cssSelector(".docs>li"));
               subMenuElementsList.get(j).click();
               wait.until(ExpectedConditions.visibilityOfElementLocated(By.tagName("h1")));
           }

       }

Read more here: Source link