Automation framework with Reflection API

Maintaining automation frameworks have always been aiding in maintaining quality software releases. We are quite aware of some of the types of automation frameworks such as Modular Testing Framework and Data-driven Framework. Irrespective of design pattern of our automation framework, maintenance of code has always been a challenge.

In any automation framework, several calls to a data member are made through object of any class, be it any method or any other data member like a variable. For this approach to work flawlessly we must know the object we are using to access a class data members. There may be some scenarios in which we just have to implement a certain functionality without knowing the complete information like in which class the functionality is implemented or which method is implementing the functioanlity.

We have a concept known as Reflection in java so as to provide us with the benefits of object identification while we are writing our scripts. We need not to be aware of all the methods and classes in our framework. We can create instances of an unknown class and call methods on them.

We may enjoy few of the advantages in our automation scripts if we incorporate reflection in our automation framework.

Java’s reflection API offers types like Class, Field, Constructor, Method, Annotation, and others.

Let’s start with an example with a small demo and see how the usage of reflection API in our automation framework should look like:

First let’s create a Class Demo:

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

public class Demo {

                        void goTo() {

                                                WebDriver driver = new FirefoxDriver();

                                                driver.navigate().to(“https://www.google.com/”);

                        }

private String getTitle() {

                                                return driver.getTitle();

                        }

}

Now in order to implement Reflection, we need to create a class from which we will be calling methods :

import java.lang.reflect.Method;

public class ReflectionHelper {

                        public static void main(String[] args) throws ClassNotFoundException {

                                                Class c = Class.forName(“Demo”);

                                                Method[] methods = c.getDeclaredMethods();

                                                for (Method m : methods) {

                                                                        System.out.println(m.getName());

                                                }

//here we are able to call a private method using reflection.

                                                Method meth = c.getMethod(“getTitle”, null);                      

}

}

Few of the functionalities of Reflection are briefed below :

    getFields() – Used to get all the fields except the private fields.

    getDeclaredField() – Used to get the private fields.

    getDeclaredFields() – Used to get the private and public fields.

    getMethods()– Used to get all the methods except the private methods.

    getDeclaredMethods() –Used to get the public and private methods.

Author : Ashish Ranjan

SHARE THIS

Get A Free Quote

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.