com.aventstack.extentreports.GherkinKeyword Maven / Gradle / Ivy
package com.aventstack.extentreports;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.aventstack.extentreports.exceptions.GherkinKeywordNotFoundException;
import com.aventstack.extentreports.gherkin.GherkinDialect;
import com.aventstack.extentreports.gherkin.GherkinDialectProvider;
import com.aventstack.extentreports.gherkin.model.Asterisk;
import com.aventstack.extentreports.gherkin.model.IGherkinFormatterModel;
import freemarker.template.utility.StringUtil;
/**
* Allows {@link IGherkinFormatterModel} to be returned by using a name, from the below gherkin model classes:
*
*
* - {@link com.aventstack.extentreports.gherkin.model.Feature}
* - {@link com.aventstack.extentreports.gherkin.model.Background}
* - {@link com.aventstack.extentreports.gherkin.model.Scenario}
* - {@link com.aventstack.extentreports.gherkin.model.Given}
* - {@link com.aventstack.extentreports.gherkin.model.When}
* - {@link com.aventstack.extentreports.gherkin.model.Then}
* - {@link com.aventstack.extentreports.gherkin.model.And}
*
*
*
* Example:
*
*
*
* extent.createTest(new GherkinKeyword("Feature"), "bddTest");
* test.createNode(new GherkinKeyword("Scenario"), bddNode");
*
*
* @see IGherkinFormatterModel
*/
public class GherkinKeyword {
private static final Logger logger = Logger.getLogger(GherkinKeyword.class.getName());
private Class clazz = IGherkinFormatterModel.class;
private IGherkinFormatterModel keywordClazz;
public GherkinKeyword(String keyword) throws ClassNotFoundException {
GherkinDialect dialect = null;
String apiKeyword = StringUtil.capitalize(keyword.trim());
String refPath = clazz.getPackage().getName();
try {
apiKeyword = apiKeyword.equals("*") ? Asterisk.class.getSimpleName() : apiKeyword;
dialect = GherkinDialectProvider.getDialect();
if (dialect != null && !dialect.getLanguage().equalsIgnoreCase(GherkinDialectProvider.getDefaultLanguage())) {
apiKeyword = null;
Map> keywords = dialect.getKeywords();
for (Entry> key : keywords.entrySet()) {
boolean keywordLocated = key.getValue()
.stream()
.anyMatch(x -> x.trim().equalsIgnoreCase(keyword.trim()));
if (keywordLocated) {
apiKeyword = StringUtil.capitalize(key.getKey());
break;
}
}
}
if (apiKeyword == null) {
throw new GherkinKeywordNotFoundException("Keyword " + apiKeyword + " cannot be null");
}
String clazzName = refPath + "." + apiKeyword.replace(" ", "");
Class> c = Class.forName(clazzName);
keywordClazz = (IGherkinFormatterModel) c.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
logger.log(Level.SEVERE, "", e);
}
}
IGherkinFormatterModel getKeyword() {
return keywordClazz;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy