0

I am currently trying to create an automation framework using Java and Selenium.

I want to create a line of code which essentially can read any input and make it a line of runnable code. For example, in an external file, a user could post 'id' into a field, that field will then be read by my program and execute the line. driver.findElement(By.id(.......)

Currently I'm using a bunch of if statements to do this for each identifier e.g. id, cssSelector, Xpath etc etc but then I'll need to do the same for the actions used by the program .click, .sendKeys etc so the program will just keep expanding and look overall very messy.

Is there a solution that would allow me to do this in a nicer way or am I stuck with my original approach?

2 Answers2

0

Reflection is probably the most direct way to solve this. It essentially allows classes and methods to be looked up by their string names.


Here's a fag-packet example of how you might approach this using the snippet you provided, but I suggest you read some documentation before diving in.

Element findElementReflectively(Driver driver, String elementType, String thingToSearchFor) {

    try {
        Method m = By.class.getMethod(elementType, String.class);
        if(!Modifier.isStatic(m.getModifiers())) {
            throw new NoSuchMethodException("'By' method is not static.");
        }

        return driver.findElement(m.invoke(null, thingToSearchFor));
    } catch (IllegalAccessException | NoSuchMethodException e) {
        throw new IllegalArgumentException("Unknown element type: " + elementType, e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException("Failed to find requested element.", e.getCause());
    }

}
Graham
  • 7,431
  • 18
  • 59
  • 84
user31601
  • 2,482
  • 1
  • 12
  • 22
  • Thanks for the quick reply, I've never used reflection before. can you give me a quick rundown of how it would work in relation to my problem? – Nathan Grabham May 15 '17 at 09:53
  • @user31601 Great thinking :) – undetected Selenium May 15 '17 at 11:03
  • @Nathan Graham I've added an example (not tested), but you should realy read some introductory documentation before you dive straight in. – user31601 May 15 '17 at 11:18
  • Will do, Thanks for the help @user31601 – Nathan Grabham May 15 '17 at 12:27
  • Cool. Would you mind accepting my answer if you've found it useful? Thanks – user31601 May 15 '17 at 13:07
  • 1
    Just did, again really appreciate the help, worked a charm. Just trying to add my actions for .click, .sendKeys etc to the code :D – Nathan Grabham May 15 '17 at 13:35
  • @user31601 is it possible to use Reflection with non static methods? I've finally had time to work on this piece of code again and I keep getting NoSuchMethodException on my action function. I've checked the javadocs for Selenium and the main difference I can see is the static/non static. – Nathan Grabham May 30 '17 at 13:59
  • Yes, it perfectly possible and quite easy. The Oracle documentation I linked to in my answer describes how to invoke instance methods. Alternatively, you could take a look at the javadoc for the methods used in my example, and I'm sure you could work it out. – user31601 May 31 '17 at 23:55
0

It depends on what you actually want to do.

Reading an id from a file and then execute code can be achieved through config file with this : Properties

Or if you want to execute full input code just search a little bit more How to execute console or GUI input as if it was actual Java code?

Community
  • 1
  • 1
Robert
  • 66
  • 4