0

I have the following menu structure:

Main Menu1 > Sub menu1, submenu2
Main Menu2 > sub menu 1

etc.

And sub menus change based on different user. I need to have common method to handle this. Please reply.

  • What programming language are you using? – Alexey R. Apr 3 '18 at 12:14
  • I am using Java – Shilpa Kuri Apr 5 '18 at 7:07
1

If your menu changes based on the user's role, and you have to have a single common method, then your method needs to have a parameter (user role), or find the user role if it is displayed on the screen (or as a hidden field)), and do different things for different roles.

Complexity is like a balloon: when you squeeze it in one place, it will appear in another.

0

Here is the example approach I'd use in your case:

1st. Describe the interface that would define how you deliver the mapping of user and roles and roles and menues.

import java.util.List;
import java.util.Map;

public interface MappingProvider {

    Map<String, String> getUserToRoleMapping();
    Map<String, List<String>> getRoleToSubmenuMapping();

}

2nd. Describe the utility class that would address your particular issue. The interface above is required to abstract from the particular implementation of how exactly you're going to store the mappings.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MenuTestTools {

    Map<String, String> userToRoleMapping = new HashMap<String, String>();
    Map<String, List<String>> roleToSubmenuMapping = new HashMap<String, List<String>>();

    public MenuTestTools(MappingProvider provider){
        userToRoleMapping = provider.getUserToRoleMapping();
        roleToSubmenuMapping = provider.getRoleToSubmenuMapping();
    }

    public List<WebElement> getSubmenuForUser(WebDriver driver, String user){
        List<WebElement> result = new ArrayList<WebElement>();
        List<String> subMenuPaths = roleToSubmenuMapping.get(userToRoleMapping.get(user));
        for(String subMenuPath: subMenuPaths){
            result.add(driver.findElement(By.xpath(subMenuPath)));
        }
        return result;
    }

}

3rd. Use your utility in the test code.

@Test
public void testSubMenues() {

    /**
     *  Some code here...
     */

    MappingProvider sampleProvider = new MappingProvider() {
        @Override
        public Map<String, String> getUserToRoleMapping() {
            Map<String, String> result = new HashMap<String, String>();
            result.put("user1", "Role1");
            result.put("user2", "Role2");
            return result;
        }

        @Override
        public Map<String, List<String>> getRoleToSubmenuMapping() {
            Map<String, List<String>> result = new HashMap<String, List<String>>();
            result.put("Role1", Arrays.asList(new String[]{"//XPATH11", "//XPATH12"}));
            result.put("Role2", Arrays.asList(new String[]{"//XPATH21", "//XPATH22"}));
            return result;
        }
    };

    MenuTestTools menuTestTools = new MenuTestTools(sampleProvider);
    List<WebElement> subMenues = menuTestTools.getSubmenuForUser(driver, "user1");

    /**
     *  Some code here...
     */
}

Some notes about implementation.

  1. You would probably want to implement your own provider since there is nobody knowing how to store your mappings better than yourself.
  2. This sample implementation took approximately 2-5 minutes, hence it is missing some important moments like exception handling. For example it will fail with NullPointerException if you call the utility method with the user that does not exist in the mapping. You have to consider all such possible cases and rework the example for your needs.
  3. This sample implementation implies you have one-to-one user-to-role mapping.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.