A DRY Trick for variations of the same Action
Recall that I split my Page Objects in Elements and Actions but I also try to take things a little further hand have my actions not take any arguments whenever possible. This means I might have, in addition to a register_random_user action I will also have a number of variations like register_standard_user, register_advanced_user and register_with_coupon. The simplest way to do this would be to copy-and-paste the registration steps n number of times, but of course that is very anti-DRY (and an absolute pain to try and maintain).
Instead, the pattern I have stumbled across is to actually wrap the generic registration action with very specific ones that set the bits then need before calling the actual action. So far I have only done this with Python, but in theory it could be done with any language. In Python I am using a dictionary as the config-holding object and then it is accessed not through the direct key but by the get method which allows for provision of a default value if a key doesn’t exist.
<pre lang="python">def register_random_user(self, tailorings = {}):
self.se.select("plan_select", tailorings.get("plan", random.choice(["Standard", "Advanced"])))
self.se.type("coupon_box", tailorings.get("coupon", ""))
self.se.click("submit_button")
self.se.wait_for_page_to_load("30000")
def register_random_standard_user(self):
options = {"plan": "Standard"}
self.register_random_user(options)
def register_random_advanced_user(self):
options = {"plan": "Advanced"}
self.register_random_user(options)
def register_with_coupon(self):
options = {"coupon": "100Dollars"}
self.register_random_user(options)
Now I can have descriptively named actions that are highly tailored to what I am trying to achieve at this specific moment without having to do a bunch of code duplication.