At one time or another, an automation engineer has written the following ruby code below to select a menu option above.
Although this code works, maintenance can soon become a time consuming effort.
Thanks to metaprogramming, a more elegant and maintainable solution can now be written:
["Users", "Apps", "Activity", "Settings"].each do |menu|
Metaprogramming is the act of writing code that operates on code rather than on data. The define_method allows one to create methods programmatically using a method. The 'def' method is not needed in this case.
In the newly rewritten code, ruby will now create 4 methods:
This addresses the two maintenance issues pointed out earlier.
Additionally, if a menu is not defined, then there would not be an existing method. This eliminates the need to have the else statement to handle the case of entering a menu option that does not exist.
Checkout the Page Objects created for OneLogin.