package com.beam.utils;
import com.beam.framework.exception.FrameworkRunTimeException;
import org.testng.*;
import java.io.IOException;
import java.util.List;
import java.util.Properties;
import java.util.function.Predicate;
public class MethodInterceptor implements IMethodInterceptor {
private static final ObjectLogger LOG = new ObjectLogger(org.apache.log4j.Logger.getLogger(MethodInterceptor.class));
private static final String BROWSER_EXCLUDED = "browserExcluded";
public static final Properties prop = new Properties();
private static final String PROPERTIES_FILE = "web-tests-resource.properties";
static {
try {
prop.load(MethodInterceptor.class.getClassLoader().getResourceAsStream(PROPERTIES_FILE));
} catch (IOException e) {
throw new FrameworkRunTimeException("Unable to find and load: " + PROPERTIES_FILE);
}
}
@Override
public List<IMethodInstance> intercept(List<IMethodInstance> testMethods, ITestContext context) {
Predicate<IMethodInstance> isBrowserExcluded = (MethodInstance) -> {
if(context.getCurrentXmlTest().getParameter(BROWSER_EXCLUDED) == null) return false;
return context.getCurrentXmlTest().getParameter(BROWSER_EXCLUDED)
.equalsIgnoreCase(prop.getProperty("test.browser"));
};
boolean isTestRemoved = testMethods.removeIf(isBrowserExcluded);
testMethods.stream()
.map(IMethodInstance::getMethod)
.forEach(method -> method.setIgnoreMissingDependencies(isTestRemoved));
return testMethods;
}
}
We can use the IMethod interceptor feature of testng to do it.