Skip to content

Instantly share code, notes, and snippets.

@dougnoel
Created August 24, 2018 03:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dougnoel/654bf01d92b62a7bf07e2f8771b638ab to your computer and use it in GitHub Desktop.
Save dougnoel/654bf01d92b62a7bf07e2f8771b638ab to your computer and use it in GitHub Desktop.
Old reflection code
private static Object getObject(String elementName) throws Throwable {
Page page = PageManager.getPage(); // Get the current reference to the page I am on.
elementName = elementName.replaceAll("\\s+", "_").toLowerCase(); // Turn our text into all lower case with spaces replaced by underscores to match object names.
Method pageElementName = null;
try {
pageElementName = page.getClass().getMethod(elementName); // Create a Method object to store the PageElement we want to exercise.
} catch(NoSuchMethodException e) {
log.error("Element " + elementName + " is not defined for the page object " + page.getClass().toString() + ". Make sure you have spelled the page object name correctly in your Cucumber step definition and in the page object.");
throw e;
}
log.debug("PageElement Name: " + pageElementName.getName());
Object classObject = (Object)pageElementName.invoke(page); // Invoke the creation of the PageElement and return it into a variable.
log.debug("PageElement Object Name: " + classObject.getClass().getName());
return classObject;
}
private static Method getMethod(Object element, String methodName) throws Throwable {
Method method = (Method)element.getClass().getMethod(methodName); // Create an object to store the method we want to call on the PageElement object.
log.debug("Method Name: " + method.getName());
return method;
}
private static Method getMethodWithStringArgument(Object element, String methodName) throws Throwable {
Method method = (Method)element.getClass().getMethod(methodName, String.class); // Create an object to store the method we want to call on the PageElement object.
log.debug("Method Name: " + method.getName());
return method;
}
private static Method getMethodWithIntArgument(Object element, String methodName) throws Throwable {
Method method = (Method)element.getClass().getMethod(methodName, int.class); // Create an object to store the method we want to call on the PageElement object.
log.debug("Method Name: " + method.getName());
return method;
}
/**
* Executes a method with no parameters on an element.
* @param elementName String
* @param methodName String
* @return Object - Returns a castable object if there is one. You have to know what you expect to get.
* @throws Throwable - Throws any exceptions received farther upstream.
*/
public static Object executeMethod(String elementName, String methodName) throws Throwable {
Object element = getObject(elementName); // Get the element object.
Method method = getMethod(element, methodName); // Get the method to run.
return method.invoke(element); // Call the method on the element.
}
/**
* Executes a method on an element and passes a String parameter.
* @param elementName String
* @param methodName String
* @param text String
* @return Object - Returns a castable object if there is one. You have to know what you expect to get.
* @throws Throwable - Throws any exceptions received farther upstream.
*/
public static Object executeMethod(String elementName, String methodName, String text) throws Throwable {
Object element = getObject(elementName); // Get the element object.
Method method = getMethodWithStringArgument(element, methodName); // Get the method to run.
log.debug("Text to " + methodName.substring(0, 1).toUpperCase() + methodName.substring(1) + ": " + text);
return method.invoke(element, text); // Call the method on the element.
}
/**
* Executes a method on an element and passes two String parameters.
* @param elementName String
* @param methodName String
* @param text String
* @param text2 String
* @return Object - Returns a castable object if there is one. You have to know what you expect to get.
* @throws Throwable - Throws any exceptions received farther upstream.
*/
public static Object executeMethod(String elementName, String methodName, String text, String text2) throws Throwable {
Object element = getObject(elementName); // Get the element object.
Method method = getMethodWithStringArgument(element, methodName); // Get the method to run.
log.debug("Text to " + methodName.substring(0, 1).toUpperCase() + methodName.substring(1) + ": " + text);
return method.invoke(element, text, text2); // Call the method on the element.
}
/**
* Executes a method on an element and passes an integer parameter.
* @param elementName String
* @param methodName String
* @param number int
* @return Object - Returns a castable object if there is one. You have to know what you expect to get.
* @throws Throwable - Throws any exceptions received farther upstream.
*/
public static Object executeMethod(String elementName, String methodName, int number) throws Throwable {
Object element = getObject(elementName); // Get the element object.
Method method = getMethodWithIntArgument(element, methodName); // Get the method to run.
log.debug("Send int to " + methodName.substring(0, 1).toUpperCase() + methodName.substring(1) + ": " + number);
return method.invoke(element, number); // Call the method on the element.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment