TestNG中测试用例的有条件执行

时间:2015-11-30 13:31:19

标签: java automation annotations testng appium

我使用appium对移动应用程序执行测试。我尝试做的只是执行第1类中编写的测试用例,如果" numberOfAssets"然后只执行第2类中编写的测试用例,如果" numberOfAssets"是2.
我一直试图找到一个解决方案,偶然发现了#34; Annotation Transformer"类。但可悲的是,它最终设置了所有的" @Test"注释为真。

我想要的只是基于" 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
        }
    }

1 个答案:

答案 0 :(得分:0)

有关您的信息,@Listeners(AnnotationTransformer.class)无效。 The documentation says

  

@Listeners注释可以包含任何扩展的类   org.testng.ITestNGListener除了IAnnotationTransformer和   IAnnotationTransformer2。原因是这些听众需要   在过程的早期就知道,以便TestNG可以使用它们来重写   你的注释,因此你需要指定这些听众   你的testng.xml文件。

然后,关于您遇到的问题,来自侦听器的initialSetUp实例不是测试本身。因此,setup.numberOfAssets永远不会改变。注释转换器在测试运行之前使用,没有运行时上下文。 相反,您可以尝试使用IHookable