2

Goal: Create my own method that calls the Selenium method passed into it and repeats that method a specified number of times.

Problem: No matter what I try, the following code ALWAYS results in: java.lang.NoSuchMethodException: org.openqa.selenium.WebDriver.sendKeys()

Discussion: As far as I know sendKeys() exists as a method within/of org.openqa.selenium.WebDriver

Problem code:

Method objTest = WebDriver.class.getMethod(strMethod, CharSequence.class);

Where strMethod = sendKeys

Code

public void repeatAction(String strMethod, int numberOfTimes) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    int i = 0;
    Method objTest = WebDriver.class.getMethod(strMethod, CharSequence.class); // PROBLEM CODE - results in NoSuchMethodException
    while (i <= numberOfTimes) {
        objTest.invoke(strMethod, Keys.DOWN);  // I've hardcoded Keys.DOWN for now but will make this flexible later
        i++;
    }
}

I call my repeatAction() method in main():

AutocompleteDropdownPractice objADDP = new AutocompleteDropdownPractice();
objADDP.repeatAction("sendKeys", 5);     // Repeat Selenium WebDriver's sendKeys() 5 times

Runtime Error

Exception in thread "main" java.lang.NoSuchMethodException: org.openqa.selenium.WebDriver.sendKeys(java.lang.CharSequence)
at java.base/java.lang.Class.getMethod(Class.java:2109)
at AutocompleteDropdownPractice.repeatKeysAction(AutocompleteDropdownPractice.java:17)
at AutocompleteDropdownPractice.main(AutocompleteDropdownPractice.java:45)

Any assistance as to what I'm doing incorrectly and how I should be doing it will be greatly appreciated.

2 Answers 2

1

If you look at the Selenium API you'll see that there isn't any method of WebDriver class as sendKeys() and that is the very reason why you are getting the NoSuchMethodException.

I think you were looking for the org.openqa.selenium.WebElement.sendKeys(java.lang.CharSequence... keysToSend) method, which in your case you could use it as follows:

Method objTest = WebElement.class.getMethod(strMethod, CharSequence[].class);

I used the array version CharSequence[].class instead of CharSequence.class because the sendKeys method accepts an array of CharSequence.class which can be clearly seen in the API docs.

Sign up to request clarification or add additional context in comments.

Comments

0

First

If you refer to Java documentation you will see that invoke() method takes two parameters:

  1. Object that you invoke a method from (since you need the object to invoke non-static method)
  2. Method arguments

These lines in your example:

Method objTest = WebDriver.class.getMethod(strMethod, CharSequence.class);
objTest.invoke(strMethod, Keys.DOWN);

mean that you take method from WebDriver class and invoke it on String object which is incorrect.

Second

sendKeys method is declared in WebElement interface, not in WebDriver.

Third

What you are trying to do can be done with Proxy objects like shown here:

public WebElement poxiedElement(String methodName, int times, WebElement element){
    return (WebElement) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{WebElement.class}, new InvocationHandler() {
        @Override
        public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
            Object[] results = new Object[times];
            if(methodName.equals(method.getName())){
                for(int i = 0; i < times; i++){
                    results[i] = method.invoke(element, objects);
                }
            }
            return results;
        }
    });
}

void test(){
    WebElement element = driver.findElement(By.xpath("..."));
    poxiedElement("sendKeys", 5, element).sendKeys("blah");
}

P.S. - You should understand that a method can return a value. So calling any method several times might return several values. You have to consider this. In my example the method will return array of results.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.