Small Customisation for Capybara and Calabash
One of the biggest challenge in test automation is updating existing codes. Because of the update in the development, it is very prone to have some changes for the attributes of ui elements and less likely to have some change in behaviour in actions. The second option is most of the time called as bug, otherwise if the business is changed, it could be called as a new feature. For any reasons that cause the automation code gives failures you need to update something.
For easy maintenance, one of the good approach keep your codes modular. It means that each module should responsible for one job and a update problem should be solved by applying just one change in your system. Using Capybara plus Cucumber gives you this features but if you are using them professionally. For this framework, business rules are kept on Cucumber so you can update business rules from your .feature files but related update should reflect to the code.
In this post I want to share a small but efficient customisation for accessing UI element. This change may be argued as against to Capybara soul but we are just playing with Ruby to enhanced Capybara's functions. Actually this idea is like Selenium Page-Object pattern but still BDD.
If we summarise the test automation in three words, it should be as follows: accessing element, performing actions and asserting result. These are the fundamentals of test automation. Accessing element can also be used for asserting result so I just write two new functions for finding element and asserting result as following:
The functions taking three parameters as accessing method can be by id, by css selector or xpath; second parameter is identifier for UI element; third parameter is optional and explains the behaviour of find function if there are more than one element with the given locator information.
We can call assert and find_by function as follow. With this way if there any update for accessing UI element we just need to update the ids.rb module.
One of the question may arise as 'we are just using id for each element and we will not need this' but even you are using ids for every element you may sometimes need to access some dynamic element with css or xpath. Even there is no need to access dynamic UI element it is a good practice to store ids in a different module for keeping them updated. This approach we will efficient if you are automating regression test cases for completed project.