Reflection API
Scenario: In Keyword Driven Framework we have Test Case in below format. We need to implement framework in such a way that underlying framework code works like ReflectionAPITestCase.java
============================================================================
package test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectionAPITestCase {
public static void main(String[] args) {
a.CallMethod("OpenBrowser","","http://www.google.com");
a.CallMethod("EnterText", "//input[@id='gs_htif0']", "Selenium");
a.CallMethod("ClickButton", "//button[@id='gbqfba']", "");
}
}
=============================================================================
Solution : Reflection API can be helpful to implement above. It will help you to call methods of a class as shown below
package test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
class a {
public static void CallMethod(String Action,String Object,String Value) {
Method method= a.class.getMethod(Action, String.class,String.class);
method.invoke(Action,Object,Value);
}
public static void OpenBrowser(String Object,String Value){
System.out.println("Opening browser URL:" + Value );
}
public static void EnterText(String Object,String Value){
System.out.println("Entering text in : " + Object + " Value:" + Value) ;
}
public static void ClickButton(String Object,String Value){
System.out.println("Clicking Object : " + Object ) ;
}
}
Note : Above implementation allows you to keep on adding “a “ to your “a” class without worrying about call strategy.
-----------------------------------------------------------------------------------------------------------------------------
Invoking Methods using Method Object
http://tutorials.jenkov.com/java-reflection/methods.html
Example 2 :
public class MyClass{
public static void doSomething(String s){
}
}
MyClass MyObject = new MyClass();
Method method = MyObject.class.getMethod("doSomething", String.class);
//get method that takes a String as argument
//Method m=oTestcase.getClass().getMethod("TestCase"+iTestCase_ID,java.util.HashMap.class);
Object returnValue = method.invoke(null, "parameter-value1");
Invoking Methods using Method Object
http://tutorials.jenkov.com/java-reflection/methods.html
Example 2 :
public class MyClass{
public static void doSomething(String s){
}
}
MyClass MyObject = new MyClass();
Method method = MyObject.class.getMethod("doSomething", String.class);
//get method that takes a String as argument
//Method m=oTestcase.getClass().getMethod("TestCase"+iTestCase_ID,java.util.HashMap.class);
Object returnValue = method.invoke(null, "parameter-value1");
- If the method is static you supply null instead of an object instance.
- If the method is none static you supply something like :
- The Method.invoke(Object target, Object ... parameters) method takes an optional amount of parameters, but you must supply exactly one parameter per argument in the method you are invoking. In this case it was a method taking a String, so one String must be supplied.
No comments:
Post a Comment