Java 如何跳过基于测试环境的Selenium TestNG测试
我有一个登录到应用程序并检查一些内容的测试,但是登录屏幕不会出现在所有的测试环境中。如果没有登录屏幕,登录验证测试将失败,我如何根据环境禁用/跳过登录测试来克服这一问题Java 如何跳过基于测试环境的Selenium TestNG测试,java,selenium,selenium-webdriver,testng,Java,Selenium,Selenium Webdriver,Testng,我有一个登录到应用程序并检查一些内容的测试,但是登录屏幕不会出现在所有的测试环境中。如果没有登录屏幕,登录验证测试将失败,我如何根据环境禁用/跳过登录测试来克服这一问题 另外,我不太清楚的是,这些测试将与自动化CI/CD管道集成。如果没有手动干预,测试将如何知道在什么环境下运行测试。很抱歉,可能有一个明显的答案,但我是自动化和CI/CD的新手。我的测试是用Java编写的,非常感谢您的帮助我有一个非常准确的用例。事实上,有两个类似的用例 根据缺陷状态跳过测试 跳过基于测试环境的测试 尽管它们听起来
另外,我不太清楚的是,这些测试将与自动化CI/CD管道集成。如果没有手动干预,测试将如何知道在什么环境下运行测试。很抱歉,可能有一个明显的答案,但我是自动化和CI/CD的新手。我的测试是用Java编写的,非常感谢您的帮助我有一个非常准确的用例。事实上,有两个类似的用例
IMethodInterceptor
@Override
public List<IMethodInstance> intercept(List<IMethodInstance> testMethods, ITestContext context) {
//removeIf method returns a boolean if any of the item is removed
boolean isTestRemoved = testMethods.removeIf(somePredicate);
//if any item is removed, then there is a chance that we might get execption due to missing dependencies.
testMethods.stream()
.map(IMethodInstance::getMethod)
.forEach(method -> method.setIgnoreMissingDependencies(isTestRemoved));
return testMethods;
}
@覆盖
公共列表截取(列表测试方法,ITestContext上下文){
//如果删除了任何项,removeIf方法将返回一个布尔值
布尔isTestRemoved=testMethods.removeIf(somePredicate);
//如果删除了任何项,则由于缺少依赖项,我们可能会获得execption。
testMethods.stream()
.map(IMethodInstance::getMethod)
.forEach(method->method.setIgnoreMissingDependencies(isTestRemoved));
返回测试方法;
}
为了跳过基于缺陷状态的测试,我必须使用IInvokedMethodListener
更多细节在这里
您可以使用TestNG组标记测试用例。用环境数据标记测试用例。因此,当您运行测试用例时,您将使用TestNG-groups选项指定要运行的环境(例如login env)。它将只运行那些使用指定标记/gropus标记的测试用例
有关组的更多信息,请参见以下几种方法:
@Test(groups={“Test”})
、@Test(groups={“production”})
和filter via(如果使用testNg testsuite.xml via参数)排序下面是xml示例
//将其作为参数,并在代码中对其进行解析//
//或通过testng中的组过滤//
希望这会有所帮助,好吧,TestNG中有一个“开箱即用”的注释转换器,您可以使用它实现
transform()
方法,并在测试上的@Test
注释应标记为ignore=false
但是,这种方法有一个缺点——必须在用户的“test.xml”中设置注释转换器才能使用它。
当您不使用xml构建测试DOM时,您可以这样定义自己的注释
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface EnvDependentIgnore {
String reason();
Environment env();
boolean skip();
}
您还需要实现IInvokedMethodListener
,以便在测试调用之前读取注释值
public class IgnoreTestListener implements IInvokedMethodListener {
@Override
public void beforeInvocation(IInvokedMethod iInvokedMethod, ITestResult testResult) {
Method method = iInvokedMethod.getTestMethod().getConstructorOrMethod().getMethod();
if (method.isAnnotationPresent(EnvDependentIgnore.class)) {
if (//"your environment value"
.equalsIgnoreCase(method.getAnnotation(EnvDependentIgnore.class).env().getValue()) &&
method.getAnnotation(EnvDependentIgnore.class).skip()) {
throw new SkipException(method.getAnnotation(EnvDependentIgnore.class).reason());
}
}
}
@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
// no need to implement
}
}
这是一种有点混乱的方法,但对我来说效果很好。您需要传递运行环境的参数。。n对于skip,您可以在testNG中使用ignore属性
public class IgnoreTestListener implements IInvokedMethodListener {
@Override
public void beforeInvocation(IInvokedMethod iInvokedMethod, ITestResult testResult) {
Method method = iInvokedMethod.getTestMethod().getConstructorOrMethod().getMethod();
if (method.isAnnotationPresent(EnvDependentIgnore.class)) {
if (//"your environment value"
.equalsIgnoreCase(method.getAnnotation(EnvDependentIgnore.class).env().getValue()) &&
method.getAnnotation(EnvDependentIgnore.class).skip()) {
throw new SkipException(method.getAnnotation(EnvDependentIgnore.class).reason());
}
}
}
@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
// no need to implement
}
}