Webinar: Clean Coding Practices for Test Automation: Part 2 [Voices Of Community]
Sparsh Kesari
Posted On: March 28, 2023
60595 Views
7 Min Read
Design patterns are widely used in software development to create reusable solutions to common design problems. These patterns provide proven solutions to recurring problems and can save developers significant time and effort by providing a framework for designing efficient and maintainable software systems.
In software testing, design patterns are applied to improve the effectiveness, efficiency, and maintainability of testing processes. Design patterns in testing can help testers to create more robust and scalable test suites, reduce duplication of test cases, and increase the ease with which new tests can be added and maintained over time. In this way, design patterns can be an essential tool for software testing professionals looking to optimize their testing processes and deliver high-quality software products.
We’re sure you would have many questions about how you can write clean and effective code for test automation.
But worry not! In this new episode of Voices of Community, our special guests, Sai Krishna, Lead Consultant, ThoughtWorks, and Srinivasan Sekar, Lead Consultant, ThoughtWorks, have teamed up with Manoj Kumar, VP-Developer Relations & OSPO at LambdaTest to share their tips and to answer your questions on how to build a clean and effective test automation framework! They showcased the implementation of various design patterns and clean coding practices with real-world scenarios.
If you are tired of slogging through the pages, go ahead and hit Play on the below video, and get ready to be wowed! 🙂
If you have missed Part 1 of this exciting webinar, worry not, for we have you covered. Here’s a quick recap of the major highlights you missed out on:
About the Webinar:
The webinar starts with Manoj recapping the learnings Sai and Srinivasan covered in the previous webinar, where we discussed implementing clean code principles like SOLID and DRY and seeing how test automation can improve. We also learned about Single Responsibility Principle, Open Closed Principle, and Flow Manager.
In addition, Manoj quotes Martin Fowler’s famous quote: “Any fool can write code that a computer can understand. Good programmers can write code that humans can understand.”
Further, Sai explains that when it comes to writing clean code, it’s important to note that design patterns are often considered. It’s a misconception to believe that design patterns are a magic solution to all problems. Implementing a design pattern doesn’t make your code better. Attempting to use a design pattern to solve a problem could complicate the issue.
Using a real-world scenario, Sai and Srinivasan demonstrate how to build a clean and effective test automation framework.
Factory Design Pattern
Srinivasan explains with a scenario about Factory Design Pattern, a type of creational design pattern, which is a way of organizing your code to make it easier to create objects. Think of it like a factory that produces different types of products based on your needs. With the factory Design Pattern, we create an object without exposing the creation logic to the client and refer to a newly created object using a common interface.
He further explains that in this pattern, we create a central “Factory” that creates objects. We tell the factory what kind of object we want, and it creates it for us behind the scenes. This is useful because it allows us to abstract away the details of how objects are created. Instead of worrying about the nitty-gritty details of object creation, we can just tell the factory what you need, and it takes care of the rest.
Strategy Design Pattern
Sai and Srinivasan further explain Strategy Design Patterns, a type of Behavioral Design Pattern. The Strategy Design Pattern organizes your code to make it easier to switch between different algorithms or behaviors at runtime. Think of it like having different strategies or plans for achieving a goal. Each strategy may have different steps or approaches, but they all aim to achieve the same result.
In the Strategy Design Pattern, we start by defining an interface or a base class representing the behavior we want to vary. Then, we create concrete classes that implement this interface or extend the base class with different algorithms or approaches.
At runtime, we can switch between different implementations of this behavior by setting the appropriate concrete class. This makes it easy to switch between different algorithms or approaches without changing the overall structure of your code.
Lombok
Lombok is an external 3rd party Java dependency that helps to create immutable objects and reduce boilerplate code for model/data objects. E.g., it can generate getters and setters for those objects automatically.
We can also use Records which was introduced in Java 14.
Decorator Design Pattern
Additionally, they explained the Decorator Design Pattern, a type of Functional Design Pattern. It is a way of extending the functionality of an object at runtime without modifying its underlying structure. It allows you to add new behavior or modify the behavior of an object by wrapping it with one or more decorator objects.
In the Decorator Design Pattern, you start with a base object with some basic functionality. Then, you create one or more decorator classes that wrap the base object and add new functionality or modify its existing behavior.
Each decorator class has the same interface as the base object, which allows you to stack multiple decorators on top of each other in any order you want. A result is an object that has all of the functionality of the base object plus the additional functionality provided by the decorators.
Q&A Session:
Q: Is it possible to implement this design pattern in conjunction with a BDD framework? Additionally, what steps or guidelines should be followed to implement it successfully?
A: No, you can apply design patterns and clean code practices regardless of the programming language or whether you’re using a BDD framework. Behavior Driven Development (BDD) is just using the Gherkin language to add steps on top of functions, so the underlying implementation remains the same. So, it’s possible to use design patterns with BDD.
Q: Is an excessive reliance on the “static” keyword generally considered an abuse, given its frequent usage?
A: We added the “static” keyword to prevent accidental object creation in the future. We also created a static function, and we don’t need to create an object for it. By making it public, we want to ensure everything is clear and correct when a new member joins the team. The aim is to provide clean functionality and prevent any unnecessary complexity.
Q: Is it possible to use abstract classes instead of interfaces in implementing the strategy design pattern?
A: The definition of the interface has evolved, as seen in Java’s default method feature. However, this should be distinct from the strategy design pattern’s main concept, which is to identify an algorithm and alter its behavior at runtime. The implementation of the strategy design pattern may vary depending on the programming language and version used. The key process is identifying the algorithm and changing its behavior during runtime.
Q: Can consumers and supplier concepts in Java’s inheritance be applied to JavaScript frameworks like TestCafe and Cypress? As design patterns are language-independent, how would this work with different object structures in these frameworks?
A: Java has consumers and suppliers, but without them, we could still make the decorator pattern by writing more code. The same applies to JavaScript, where we may have different concepts, but the idea of design patterns is to solve problems across different programming languages. If we want to implement design patterns in JavaScript, the idea will be the same, but the implementation may differ due to language differences. Cypress and TestCafe are just runners and have nothing to do with design patterns.
Hope You Enjoyed The Webinar!
We hope you liked the webinar. In case you missed it, please find the webinar recording above. Share this webinar with anyone who wants to learn more about clean coding practices. You can also subscribe to our newsletter Coding Jag to stay on top of everything testing and more! Stay tuned for more exciting LambdaTest Webinars.
That’s all for now. Happy testing!
Got Questions? Drop them on LambdaTest Community. Visit now