2

I am writing test automation for a web app whose navigation is behind a 'hamburger menu'. That is, an icon must be clicked to display the navigation options.

I am considering two different designs:

1) The code to click a menu item only clicks the menu item. In this case, a test requiring navigation would look something like:

hamburgerMenu.click() // opens menu
menuItem.click() // clicks item

2) The code to click a menu item contains the call to open the menu. In this case, a test requiring navigation would look like:

menuItem.click() // internally calls hamburgerMenu.click()

The code for the second option would be slightly more compact, since the 'click on hamburgerMenu' call would only exist once, in the definition of menuItem.click(), rather than needing to be repeated each time a menu item needs to be clicked.

On the other hand, someone unfamiliar with this code might find it less intuitive, and expect it to not work, since they won't see the call to open the menu, and think the test code is trying to click on items that are not visible yet.

The question is:

1) Are there other considerations that make one or the other of these designs better, which I might not have thought of?

2) Is one of these generally considered to be much better than the other, or does it not really matter?

2

A touchstone for a good test script is whether someone reading the script can perform the actions manually. So the script can either contain high-level actions (like login, select_profile_menu) or just clicks and keystrokes. Your second option looks like a low-level action though it is actually made up of two low-level actions. I will stick to option(1).

1

In this case, it is more or less a personal preference issue.

From a maintenance point of view, it is easier to read the code within a single method, therefore the first approach it preferred.

There is only one line of additional code, and it is such a natural logic flow.

0

I would argue the second option is better. Although I agree it can be down to personal preference, having your test script concise looks better if you have good naming conventions. You say its only one line but if it is in the main header then surely this will get exercised fairly often, and instead you should try to follow DRY principles.

With testing, having verbose method names is actually beneficial as it allows the script to be read more easily. Rather than having two calls you could instead have one named well:

# Using POM style: Separate page for methods

def select_navigation_menu_option(self, option):
    self.driver.hamburgerMenu.click()
    self.driver.menuItem(option).click() // Need stuff behind scenes to locate item

# In main script

select_navigation_menu_option("Contact page")

This allows you to hide all the complexity of automation while keeping the actual test script neat and to the point. Thus someone new coming in can now just read the test script and understand what the script does. Look into POM style framework for examples on this. Once you have, remember to stick to whatever methodology you use throughout your project :)

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.