As per my understanding, keyword driven framework is, we create a keyword for each action we do and write the test cases in excel using those keywords. For example, opening browser, entering username, password, clicking on login button etc we create a keyword for each action and create a method for each of these keywords and store all these methods in a class like actionmethods() etc.
We use java reflection class to call these methods.
If we have less no. of methods that should be ok. I am working on a small project where I got like 200 keywords. So I have to write 200 methods here. Should I store all these methods in one class?
What if I have 1000 keywords (for a big project)?
If I create separate files grouping keyword methods based on the pages, it is becoming very complicated. Can someone please explain if we use only one class to hold all the methods?
Thank you.
Maintain the keyword methods as separate class for each page like we do in page object pattern.
While calling the keyword, we can specify the class name as well along with method name. For e.g., LoginPage.login
For e.g., if you are maintaining the page class under package com.myproject.test.pages
You can change the reflection code for invoking as,
public Object invokeKeywordMethod(String keywordName)
throws InvocationTargetException, IllegalAccessException, InstantiationException {
String[] keywords = keywordName.split("\\.");
if (keywords.length == 1)
throw new Error("Invalid keyword: " + keywordName + ". The keyword must be as ClassName.methodName");
String className = keywords[0];
String methodName = keywords[1];
Class<?> pageClass = getPageClass(className);
Method method;
try {
method = getPageClass("").getDeclaredMethod(methodName);
} catch (NoSuchMethodException e) {
throw new Error("The keyword method '" + methodName + "' is not found in the class");
}
return method.invoke(pageClass.newInstance());
}
private Class<?> getPageClass(String className) {
Class<?> pageClass = null;
try {
pageClass = Class.forName("com.myproject.test.pages." + className);
} catch (ClassNotFoundException e) {
throw new Error(className + " not found in package 'com.myproject.test.pages' ");
}
return pageClass;
}