Python for test automation: object-oriented programming
Object-Oriented Programming (OOP) refers to the encapsulation of data and behavior into self-contained objects. Understanding OOP is crucial for building maintainable and scalable test automation frameworks. We can use the class keyword in Python to create a blueprint for a custom object. For example, we can create a Dog
class:
class Dog:
def bark(self):
print("woof")
dog = Dog()
dog.bark() # prints out 'woof'
We can initialize any amount of these objects, each of which will have the data and functionality of the Dog-class. To pass in some initial data to an object, we must use the init dunder-method (a predefined object initialization method starting and ending with 2 underscores) of a class:
class Dog:
def __init__(self, name: str):
self.name = name
self.age = 5
dog = Dog("John")
print(dog.name, dog.age) # prints out 'John 5'
With this init method, we can define any initial data an object of type Dog
should have. Here, we simply defined a name that needs to be passed as an argument to the class to initialize the value, and an age that will always have an initial value of 5. We can also use other functions inside the init method to enable more complex initialization of an object.
Inheritance
Inheritance in object-oriented programming refers to a mechanism by which a new class can inherit the data and behavior of another class. This is used to promote code reusability by allowing new classes to use existing functionality without always redefining that functionality within the new class. For example, our Dog
class could inherit from the Animal
class:
class Animal:
def __init__(self, name: str):
self.name = name
self.age = 5
class Dog(Animal):
def __init__(self, name: str):
super().__init__(name) # initialize the superclass 'Animal'
def bark(self):
print("woof")
dog = Dog("John")
dog.bark() # prints out 'woof'
print(dog.name, dog.age) # prints out 'John 5'
Now we do not need to define the age of a dog within the dog class, since that is inherited from the Animal
class, also called superclass. In order to inherit the data of the superclass, we must call the init method of the superclass by utilizing the built-in function super()
to refer to the superclass during the initialization of Dog
, which is also referred to as the subclass of Animal
. We can easily create other animal classes which inherit the behavior and data of the Animal
class:
class Python(Animal):
def __init__(self, name:str, length: int):
super().__init__(name)
self.length = length
python = Python("Monty", 42)
print(python.name, python.age, python.length) # prints out 'Monty 5 42'
Test automation use case
OOP comes in handy for test automation in cases such as the Page Object Model (POM) where a single page of a website is defined as an object that encapsulates its data and behavior. During a test run, we may, for example, want to log some data from the page into our test log. Now, this kind of functionality would need to be common among all pages of the website, so naturally OOP comes to mind. We can define a base class that defines these behaviors, and inherit those behaviors in classes defining the specific functionality of individual pages of the website under test.
class Base:
def log(self, message: str, level: str = "INFO"):
# library or framework specific implementation
pass
class LandingPage(Base):
pass