What are good coding practices for automation?

What are good coding practices for automation?


Writing clean code is not straightforward, you need to keep a lot of good practices in mind. Below are the following practices for writing better automation code.

1. Naming Convention

Following proper naming conventions helps in an easier understanding of code and maintenance. This naming convention implies variables, methods, classes, and packages. For example, your method name should be specific to what it is intended for. A ‘Register_User()’ method portrays the method displaying user registration within that method. Clearly defined method names add to the easy maintenance and readability of the script.

2. The Three R’s-Reduce, Reuse and Recycle

Do not overcomplex your methods with multiple functionalities covered in a single method. Reducing the complexity of the methods leads to the easy maintainability of the code. Also, reuse your methods wherever required, do not copy-paste the same code in different methods. This leads to unnecessary duplication and redundancy in the code. Recycling is also another useful tip for writing better automation code. Always update the existing methods whenever the flow changes, though it has its own challenges, where the new user may not be aware of the dependencies the method may have.

3. Structure Your Tests Well

Structuring your tests with the help of the framework adds value to your work and reduces the maintenance effort in the long run. You can control the flow of your application via the use of annotations provided by frameworks like JUnit and TestNG.

4. Thorough Validation Of Your Tests

Every user action should be validated as are your test case steps, whether it is validating the visibility of an element, or keeping in mind the typography tips, textual representation, redirections to a page, or any kind of visual validation or even if it’s about evaluating the results from the database.

5. Sleep Does Not Improve Stability

The major problem with this type of static sleep is, we are not aware of the load of the machine on which tests are run and hence these may lead to timeouts. Therefore thread.sleep should be avoided for maintaining better automation code. A better approach of using wait to your scripts is through condition binding, wherein the script can wait like a human till a certain condition is met. For example, waiting till a certain element is visible or not.

Source: 8 Actionable Insights To Better Automation Code

1 Like