10-Minute Guide to Object Oriented Programming
Object Oriented (OO) simplified by examples
The articles in the 10-minute guide series:
- 10-Minute Guide to Git Version Control for Testers
- 10-Minute Guide to Set up Test Automation using Selenium WebDriver with Ruby
- 10-Minute Guide to Object Oriented Programming
Most modern programming languages support object-oriented programming (OOP). Mastering OOP is a must for software engineers and test automation engineers nowadays. This quick guide is intended for non-programmers to get a quick understanding of OO, in particular, to become automated testers.
OOP sounds like a complicated concept, at least for me when I was 13 years old. It turned out to be quite simple after my father illustrated with this example:
Car is a class, a type of something, it has the following two functions (plus many more…):
My car (the one in my garage) is an object of Car, it can ‘brake’ and ‘accelerate’. I can physically drive it.
Now have a think about the statements below:
my_car = Car.new
your_camry = Car.new
.new creates an instance of a Class, in this case, Car. The returned instance is also known as an Object.
In Ruby, everything is an object (or class)
Ruby is a pure Object-Oriented language, comparatively, Java is not as it has primitive types. Learning OO with Ruby is easier.
a_string = "Ruby is cool"
a_string.size # => 12
iphones = ["iPhone 4s", "iPhone 5S", "iPhone 5C"]
iphones.sort #=>["iPhone 4s", "iPhone 5C", "iPhone 5S"]
a_stringis an object of String class, and
iphones is an object of class Array. Here is how to find out an object’s class.
a_string.class # => String
iphones.class # => Array
The reason we are able to use
.sort is because these functions are defined in the String and Array classes respectively.
Inheritance is one type of relationship between two classes. For example, seagulls and parrots are both birds, thus they share the common features of a bird.
The code below defines three classes: Bird, Seagull, and Parrot.
puts "I am flying"
endclass Seagull < Bird
endclass Parrot < Bird
puts "if someone teaches me"
Inheritance can be described as “is-a” relationship. For example, we can say “Parrot is a Bird” and “Seagull is a Bird”, but not “Parrot is a Seagull”.
An object of a child class can invoke a function defined in the parent class.
a_seagull = Seagull.new
a_seagull.fly # => "I am flying"
my_parrot = Parrot.new
my_parrot.fly # => "I am flying"a_seagull.is_a?(Bird) # => true
my_parrot.is_a?(Bird) # => true
As you can see, by using inheritance, the code (defined in functions, such as
Birdclass) can be reused.
A child class can define its exclusive functions, such as
speak in Parrot class.
my_parrot.speak # => "if someone teaches me"
a_seagull.speak # NoMethodError: undefined method `speak' for #<Seagull:0x..>
Child Class Can Override Behaviours
A function defined in a class is also known as “Behaviour”. A child class can override the behavior defined in its parent class, like the example below.
class Ostrich < Bird
puts "I'd rather run"
endostrich = Ostrich.new
ostrich.is_a?(Bird) # => true
ostrich.fly #=> "I'd rather run"
This has a fancy term, Polymorphism, one of the most asked questions in programmer interviews. Don’t over-think it, do some OO practices, and you will get a deep understanding. For now, at least for test automation engineers, my one-line explanation will do.
Use your newly learned OO knowledge to understand Automated Test Script
Below is a raw Selenium WebDriver test that follows Maintainable Automated Test Design.
it "Select One-way trip" do
driver.get("https://travel.agileweay.net")login_page = LoginPage.new(driver)
flight_page = FlightPage.new(driver)
flight_page.click_continueexpect(page_text).to include("2023-05-02 Sydney to New York")
These two Page Classes represent the two web pages you see in the browser.
driver, the instance of Selenium WebDriver
flight_pageare instances of
You can think a function in a page class is an operation that a user can perform on the web page.
What does a page class look like?
Here is the content of
flight_page.rb file, which defines
require File.join(File.dirname(__FILE__), "abstract_page.rb")class FlightPage < AbstractPagedef initialize(driver)
super(driver, "") # <= TEXT UNIQUE TO THIS PAGE
driver.find_element(:xpath, "//input[@name='tripType' and @value='" + trip_type + "']").click
Selenium::WebDriver::Support::Select.new(driver.find_element(:name, "fromPort")).select_by(:text, from_port)
# more functions ...def click_continue
Now with this quick guide, do you understand the above test scripts better? 😊