【发布时间】:2016-03-04 03:51:55
【问题描述】:
我正在使用 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
}
}
【问题讨论】:
标签: java automation annotations testng appium