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.