How to disable certain test based on Browser or Environment

Rahul
1 min readJul 20, 2020
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;
}

}
this is the commit for not running tests based on test environment
sample usage of how to use it in test

We can use the IMethod interceptor feature of testng to do it.

--

--