[英]How to execute other test cases in a TestNG test suite while waiting for one test case execution to finish
[英]Conditional Execution of Test Cases in TestNG
我正在使用appium在移动应用程序上执行测试。 我想做的是,如果“ numberOfAssets”的值为1,则仅执行用Class 1编写的测试用例,然后,如果“ numberOfAssets”为2,则仅执行用Class 2编写的测试用例。
我一直在尝试为此找到一个解决方法,并偶然发现了“ Annotation Transformer”类。 但可悲的是,它最终将所有“ @Test”注释都设置为true。
我想要的只是基于“ numberOfAssets”值执行的整个类。 我会对此提出一些建议。
我有三节课
initialSetUp类-
public class initialSetUp{
AndroidDriver driver;
int numberOfAssets = 1;
@BeforeClass
public void setUp() throws MalformedURLException {
//Set up desired capabilities and pass the Android app-activity and app-package to Appium
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("BROWSER_NAME", "chrome");
capabilities.setCapability("VERSION", "4.4.2");
capabilities.setCapability("deviceName","Emulator");
capabilities.setCapability("platformName","Android");
capabilities.setCapability("appPackage", "com.temp.app_name");
capabilities.setCapability("appActivity","com.temp.app_name.start_activity");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@AfterClass
public void teardown(){
//close the app
driver.quit();
}
}
1类
@Listeners(AnnotationTransformer.class)
@Test(enabled = false, groups = "yo")
public class oneAssetType extends initialSetUp{
//Check if first page is asset type specified
// @Test (groups = "onlyOne", priority = 1)
public void test_1() throws Exception {
WebElement centerLable = driver.findElement(By.id("pagerTitle"));
assert centerLable.getText().equalsIgnoreCase("Whatever") : "did not match with expected value";
}
}
2类
@Test(enabled = false)
public class twoAssetType extends initialSetUp{
//Check if first page is stickers
@Test (groups = {"twoAssets", "a"}, priority = 1)
public void test_1() throws Exception {
WebElement centerLable = driver.findElement(By.id("pagerTitle"));
assert centerLable.getText().equals("WhereEver") : "did not match with expected value";
}
}
TestNG.xml-
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Main App Suite">
<listeners>
<listener class-name="package_name.AnnotationTransformer" />
</listeners>
<test name="Navigate one Asset Types">
<classes>
<class name="package_name.oneAssetType" />
</classes>
</test>
<test name="Navigate two asset types">
<classes>
<class name="package_name.twoAssetType" />
</classes>
</test>
</suite>
我还设置了注释转换器类-
public class AnnotationTransformer implements IAnnotationTransformer {
initialSetUp setup = new initialSetUp();
@Override
public void transform (ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod){
try{
if (testClass.getName().equals("package_name.oneAssetType") && (setup.numberOfAssets == 1)) {
annotation.setEnabled(true);
}
}catch(NullPointerException e){
//YO
}
}
供您参考, @Listeners(AnnotationTransformer.class)
无法正常工作。 该文件说 :
@Listeners批注可以包含任何扩展org.testng.ITestNGListener的类,但IAnnotationTransformer和IAnnotationTransformer2除外。 原因是这些侦听器需要在过程的早期就知道,以便TestNG可以使用它们来重写您的注释,因此您需要在testng.xml文件中指定这些侦听器。
然后,关于您遇到的问题,来自侦听器的initialSetUp
实例不是测试本身。 因此, setup.numberOfAssets
将永远不会改变。 注释转换器在测试运行之前使用,并且没有运行时上下文。 相反,您可以尝试使用IHookable 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.