Knowing the basics of object-oriented programming is necessary not only for programmers, but also, of course, for testers who interact with program code, study it, or write it. Insight into programming fundamentals enables QA experts to better understand the program behavior, give effective recommendations on how to improve the structure of program code, and, more efficiently create autotest code.
Concept of Encapsulation
Encapsulation is a set of practices for hiding parts of the implementation, or the internal state of an object and selectively allowing access to internal mechanics through certain public methods. It is often used to directly prevent users from accessing an object and editing these properties, for web security purposes or to prevent deliberate changes to information.
An Example of Using Encapsulation in Program Code
To visually examine the use of encapsulation and its properties, let’s say that we have some TEST class which is a bank account with 2 features: type of account and account balance.
So, the simplest display of such a class can look like this:
It is very formulaic and raises a lot of questions. The most relevant one is that all the users of this class can update the values of the bank balance properties as they want when they perform bank withdrawals or deposit actions.
And this is not a good idea. No one can guarantee that the business rules are followed correctly because such operations cannot impose “correct system behavior”.
It is much better to use the basics of encapsulation. To do this we can make the properties hidden by restricting direct access to them with the TEST class. Then we give access to the property through public methods, adding some business rules along the way:
When encapsulation is used and some kind of business rules are imposed, it becomes unrealistic to withdraw a negative amount of money or to put a minus value of funds into a user account. The real balance is still available to everyone, but access is now limited to read parameters.
The Role of Encapsulation in Automation
The most common use of the basics of encapsulation in the field of test automation is Page Object, which, in turn, is widely used because of its user-friendly interface based on tools such as Selenium, Playwright, and Cypress.
As we know, Page Object are special classes that encapsulate the details of page creation, locators used to verify elements on a web page, and used in test checks.
An Example of Page Object Encapsulation
Let’s take a small example of a LoginPage object that hides implementation basics in private parameters, and gives access to them through simple test manipulations that are defined in public methods.
The “user” of such a class can operate on elements that are defined in the class exclusively through a public method without direct access to the details of page implementation – e.g., the HTML structure. All this leads to a clean API for the page object class and efficient separation of interests.
Leave A Comment