LOADING

Type to search

Automation Java

OOPs concepts in Test Automation – Where you have applied

Share

As a tester you might have come across with an interview question on OOPs concepts in Test Automation, where you have applied. You have / might implemented all of OOPs concepts in your test automation project.

Object-Oriented Programming

Object means a real-world entity such as a pen, chair, table, computer, watch, etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes and objects.

Apart from these concepts, there are some other terms which are used in Object-Oriented design:

  • Coupling
  • Cohesion
  • Association
  • Aggregation
  • Composition

He in this article we would look into 5 concepts and where we have actually applied.OPs concepts in Test Automation

Learn OOPs concept in Java or any programming language you prefer before going further. Previously we have posted OOPs concept in Java with some easy to understand examples. You can understand about Objects and Classes, If you have read through OOPs concept in Java. So would not cover this part in this article as it is common everywhere.

Lets move on to actual topic OOPs concepts in test automation.

Abstraction

Abstraction is a process of showing only relevant details and hide implementation details from user or outside program. Similarly in our automation we write all  locators in Page Class. an dusing them in Tests. In Other words we are hiding our implementation from user.

package Automation;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

public class login_page extends TestClass{
    
    public login_page(WebDriver driver) {
        
        super(driver);
        
    }
    
    @FindBy(how=How.XPATH, using="//a[contains(text(),'Login in')]")
    public static WebElement loginTxt;
    
    @FindBy(how=How.XPATH, using="//input[@id='email' and @name='email']")
    public static WebElement email;
    
    @FindBy(how=How.XPATH, using="//input[@id='passwd']")
    public static WebElement pswd;
    
    @FindBy(how=How.XPATH, using="//button[@id='Login']//span")
    public static WebElement loginBtn;
    
    @FindBy(how=How.XPATH, using="//a[@title='Account Details']/span")
    public static WebElement accountTitle;

}

Interface

An interface is an abstract type that is used to specify a behavior that classes must implement. This can have methods and variables just like the class but the methods declared in interface are by default abstract. Although Interface is similar to a class, both are two different concepts.

Coming back to our concepts in test automation, WebDriver itself is an Interface.

WebDriver driver = new ChromeDriver();

Here we are initializing Chrome browser by creating a reference variable (driver) of the interface (WebDriver) and initializing an Object. In this case WebDriver is an Interface and ChromeDriver is a class.

Inheritance

The process in which one class utilizes properties (variables and objects) and functionalities of another class is called Inheritance.

We create a Base Class in our automation Framework to initialize WebDriver interface, WebDriver waits, loggers, reports, etc.,

Then we extend this Base Class in other classes such as Tests, Page and Utility Class etc.,  Here class extending Base Class is called as Subclass and Base Class itself is called a Superclass.

package Automation;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class BaseClass {

    public static WebDriver driver;
    public static boolean bResult;

    public BaseClass(WebDriver driver) {
        String path = "/drivers/chrome/chromedriver.exe";
        String resourcePath = getClass().getResource(path).getPath();
        System.setProperty("webdriver.chrome.driver", resourcePath);
        BaseClass.driver = new ChromeDriver();
        BaseClass.bResult = true;
    }

    
    public WebDriver returnDriver() {
        return driver;
    }

}
package Automation

import org.openqa.selenium.WebDriver;

//Here ChildClass is extendign Baseclass
public class ChildClass extends BaseClass{


    private static WebDriver driver;
    
    public ChildClass() {
        // Initializing BaseClass
        super(driver);

        //Storing return value of WebDriver to class specific variable
        ChildClass.driver = super.returnDriver();
        
    }

    
    
    public void openURL(String url) {
        driver.get(url);
        
    }

}
package Automation

public class Main {
    
    public static void main (String arg[]) {
        
        //By Initializing jsut ChildClass you would initialize BaseClass and open Chrome browser       
        ChildClass cc = new ChildClass();
        cc.openURL("https://google.com");
        
    }
}

 

Polymorphism

This will allows us to perform same task in multiple ways, means having many forms. Polymorphism is combination of overloading and overriding.

Method Overloading

Method Overloading is nothing but Multiple methods of a class having the same name but different parameters.

For Instance in Selenium we us Implicit wait, which is an example of overloading. In Implicit wait we can different time stamps such as SECONDS, MINUTES, HOURS etc.,

Similarly you can also consider findElement for an example.

@BeforeMethod
public void driverTimeOut() throws Exception {

//As par tof Method Overloading any of below mentioned implicitlyWait can be used
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.MINUTES);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.HOURS);

}

public void findWebElement(String iValue){

element = driver.findElement(By.id(iValue));
element = driver.findElement(By.cssSelector(iValue));

}

Method Overriding

Using Method which was already implemented in another class by changing its parameters, is known as method overriding.

To understand this you need to understand Overriding in Java.

For instance consider below example.

package Automation

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;

public class BaseClass {

    public static WebDriver driver;
    public static boolean bResult;

    public BaseClass(WebDriver driver) {
        String path = "/drivers/chrome/chromedriver.exe";
        String resourcePath = getClass().getResource(path).getPath();
        System.setProperty("webdriver.chrome.driver", resourcePath);
        BaseClass.driver = new ChromeDriver();
        BaseClass.bResult = true;
        
    }

    public void openURL(String url) {
        driver.get(url);
        
    }
    
        //In this Base class Verify Text for exactl match
    public void verifyText(WebElement element, String expectedText) {
        String actualText =  element.getText();
        Assert.assertTrue(actualText.equals(expectedText),"Expected Text not Found");
        
    }
    
    public WebDriver returnDriver() {
        return driver;
    }

}
package Automation

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Assert;

public class ChildClass extends BaseClass{


    private static WebDriver driver;
    
    public ChildClass() {
        super(driver);
        ChildClass.driver = super.returnDriver();
        
    }
    
    //In this Child class we are verifying whether element text contains desired
    @Override
    public void verifyText(WebElement element, String expectedText) {
        String actualText =  element.getText();
        Assert.assertTrue(actualText.contains(expectedText),"Expected Text not Found");
        
    }

}

Encapsulation

Encapsulation is a mechanism of wrapping data (variables) and code together as a single unit. All the classes which we write in our automation framework are an example of Encapsulation.
For example In Page object model classes, we declare the WebElement locator using @FindBy and initialization of this data members will be done using Constructor in test methods.

Explore more on Automation.

Leave a Comment

Your email address will not be published. Required fields are marked *