You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Appium自动化中运用TestNG注解并改造测试类

重构Appium测试代码适配TestNG并移除冗余main方法

我来帮你搞定这两个重构需求,让你的Appium测试适配TestNG框架,同时清理掉不必要的main方法,下面是具体的修改方案:

1. 修改Base类适配TestNG

原来的Base类用main方法初始化driver,现在我们换成TestNG的注解来管理测试生命周期,这样测试类可以继承它并复用driver初始化逻辑。修改后的代码如下:

package tutcheck;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.remote.MobileCapabilityType;
import org.testng.annotations.BeforeClass;

public class Base {
    // 定义protected静态driver,让子类可以直接访问
    protected static AndroidDriver<AndroidElement> driver;

    // 使用TestNG的@BeforeClass注解,在测试类所有测试方法执行前初始化driver
    @BeforeClass
    public void setUp() throws MalformedURLException {
        File f = new File("src");
        File fs = new File(f, "ApiDemos-debug.apk");
        
        DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability(MobileCapabilityType.DEVICE_NAME, "192.168.56.101:5555");
        cap.setCapability(MobileCapabilityType.APP, fs.getAbsolutePath());
        
        driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
    }
}

改动说明:

  • 移除了原来的main方法,改用TestNG的@BeforeClass注解方法setUp来完成driver初始化,这个方法会在测试类的所有测试方法执行前运行一次
  • 将driver定义为protected static,让继承Base的测试类(比如Gestures)可以直接复用这个实例
  • 方法名改为setUp,更符合自动化测试的命名规范,清晰表达初始化的作用

2. 修改Gestures类:移除main方法并添加TestNG测试注解

现在把Gestures类改成标准的TestNG测试类,移除main方法,用@Test注解标记核心测试逻辑:

package tutcheck;

import java.time.Duration;
import java.util.concurrent.TimeUnit;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidElement;
import org.testng.annotations.Test;

public class Gestures extends Base {

    // 使用TestNG的@Test注解标记测试方法,作为测试入口
    @Test
    public void testExpandableListLongPressGesture() {
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        
        // 滚动到Views选项并点击
        driver.findElementByAndroidUIAutomator(
            "new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\"Views\").instance(0))"
        ).click();
        
        // 使用TouchAction点击Expandable Lists
        TouchAction t = new TouchAction(driver);
        t.tap(driver.findElementByXPath("//android.widget.TextView[@text='Expandable Lists']")).perform();
        
        // 进入Custom Adapter页面
        driver.findElementByXPath("//android.widget.TextView[@text='1. Custom Adapter']").click();
        
        // 长按People Names选项
        t.press(driver.findElementByXPath("//android.widget.TextView[@text='People Names']"))
          .waitAction(Duration.ofSeconds(10))
          .release()
          .perform();
        
        // 输出验证Sample menu文本
        System.out.println(driver.findElementByXPath("//android.widget.TextView[@text='Sample menu']").getText());
    }
}

改动说明:

  • 完全移除了原来的main方法,改用@Test注解的testExpandableListLongPressGesture方法作为测试入口
  • 直接继承Base类的driver实例,不需要重复初始化
  • 方法名改为更具描述性的名称,清晰表达这个测试要验证的功能

额外注意事项

要让这些代码正常运行,你需要确保:

  • 项目中已经引入TestNG依赖(比如通过Maven或Gradle添加)
  • 在IDE中配置TestNG运行器,或者用构建工具的TestNG插件执行测试

内容的提问来源于stack exchange,提问作者Amit Bhargav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:17:19