When we create automated tests, we usually use an object-oriented programming language. This means that it’s a good idea to understand the basic OOP principles and apply them to our code when this seems fit 🔎
This article is the first one in a 4-part series that covers these principles, explained in a Page Object Model context, and exemplified with C# code and NUnit test framework. We’ll start with encapsulation and how it fits in test automation. In the following articles, we will cover the rest of these principles.
Table of Contents
- You’re here → Chapter 1 – Encapsulation
- Chapter 2 – Inheritance
- Chapter 3 – Polymorphism
- Chapter 4 – Abstraction
What is encapsulation in OOP?
Before we jump into the principle of encapsulation, we have to understand access modifiers:
Access modifiers in OOP
Access modifiers are keywords that define the access level of a member or a type. So, what does that mean? Let’s take a look at the available access modifiers in C#:
- Private: the access is limited to the containing class. For example, a variable or a method declared as private can only be accessed by other members inside the same class.
- Protected: the access is limited to the containing class and any types derived from this class.
- Internal: the access is limited to the current assembly.
- Public: there are no restrictions, the member or type can be accessed both from inside and outside the assembly.
Encapsulation definition
Encapsulation is a fundamental notion in OOP, and it refers to hiding the internal state and functions of an object, as well as binding the code and the data it manipulates under the same unit.
It is used to restrict the direct access to some of the object’s components. This prevents data from being accessed from outside the object or being modified by accident. It can be useful, for example, if we want to make the fields of a class read-only or write-only.
One way to achieve encapsulation is by declaring the variables as private and using properties to get and set the variable values.
Encapsulation example
Here is a quick and easy example of how encapsulation looks:
class Dog { private string name; public String Name { get => name; set => name = value; } }
We have a Dog class, with one private variable, name. We can access it only through the public property Name:
Dog myDog = new Dog { Name = "Rudie" }; Console.WriteLine(myDog.Name);
The console will display “Rudie” which is the name I set for myDog. Of course, in real-life projects, the use of encapsulation is much more complex, but you get the gist 😁
How does encapsulation fit in test automation?
Onto what we’re most interested in – how do we use encapsulation in our test automation framework / in POM? You’re probably already using it, but perhaps you didn’t know it’s one of the most important OOP principles.
Encapsulation example in test automation
Usually, our page object classes have all web (or mobile) elements defined as private variables. Then, we have the methods that interact with the application. The tests themselves only call these methods, but they do not have direct access to the web elements.
This is the main definition of encapsulation – we encapsulate all the elements and their usage under the same class, but only expose outside the class the functions.
Let’s use the OrangeHRM demo website for our examples:
To implement the login on the login page, we need to enter a username, a password, and click the login button. We can do all this in one method, that takes 2 string parameters: 1 for the username, and 1 for the password.
The web elements identified on the page will be the Username and Password fields, and the login button. We don’t need to access these buttons from outside our page class, so they will be private.
The elements definitions should look like this:
private IWebElement NameInput => driver.FindElement(By.Id("txtUsername")); private IWebElement PasswordInput => driver.FindElement(By.Id("txtPassword")); private IWebElement LoginButton => driver.FindElement(By.Id("btnLogin"));
The login method is:
public BasePage Login(string username, string password) { NameInput.SendKeys(username); PasswordInput.SendKeys(password); LoginButton.Click(); return new BasePage(driver); }
And the test will simply call this method:
LoginPage loginPage = new LoginPage(driver); loginPage.Login("admin", "admin123");
Conclusion
Encapsulation in UI test automation brings together all web elements and the actions performed on them under the same page object class.
At the same time, access to these elements is restricted from classes outside the page object (for example the test classes), and can only be obtained by using the public methods provided to interact with them.
Hope this tutorial was helpful! See you at the next chapter 📕