APP的UI自动化demo(appium+java)

文章目录

appium连接手机

准备工作

1、查看连接手机模拟器是否连接成功,获取设备名称

执行命令:adb devices

2、查看android内核版本号--->paltformVersion

执行命令:adb shell getprop ro.build.version.release

3、模拟器上打开要测试的app,查看包名和活动名

adb shell dumpsys window |grep mCurrentFocus

打开并启动appium

之前出现过appium连接不上手机模拟器,在修改配置中JAVA_HOME和ANDROID_HOME填写下正确的路径后可以了

点击打开连接配置页面

填写并保存后点击【start Session】

bash 复制代码
{
  "platformName": "Android",
  "platformVersion": "11",
  "deviceName": "emulator-5554",
  "appPackage": "com.wandoujia.phoenix2",
  "appActivity": "com.pp.assistant.activity.PPMainActivity"
}

连接后页面

java代码实现-第一版

pom

xml 复制代码
 <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>9.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-remote-driver</artifactId>
            <version>4.9.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-api</artifactId>
            <version>4.9.0</version>
        </dependency>

查找比较新的jar地址
阿里云maven仓库

java 复制代码
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.android.nativekey.AndroidKey;
import io.appium.java_client.android.nativekey.KeyEvent;
import java.net.URL;

public class AppTest {
    public static void main(String[] args) throws Exception {
        AndroidDriver driver = null;
        DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability("platformName", "Android");
        cap.setCapability("platformVersion", "11");
        cap.setCapability("deviceName", "emulator-5554");
        cap.setCapability("appPackage", "com.wandoujia.phoenix2");
        cap.setCapability("appActivity", "com.pp.assistant.activity.PPMainActivity");
        cap.setCapability("unicodeKeyboard", "true");
        cap.setCapability("resetKeyboard", "true");
        cap.setCapability("noSign", "true");
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
        Thread.sleep(5000);
        //点击【同意】
        driver.findElement(By.id("com.wandoujia.phoenix2:id/n8")).click();
        Thread.sleep(2000);
        //点击系统的返回
        driver.pressKey(new KeyEvent(AndroidKey.BACK));
        Thread.sleep(5000);
        driver.quit();

    }
}

第二版-接入testng和隐式等待显示等待

java 复制代码
import io.appium.java_client.android.nativekey.AndroidKey;
import io.appium.java_client.android.nativekey.KeyEvent;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import io.appium.java_client.android.AndroidDriver;

import org.openqa.selenium.remote.DesiredCapabilities;

import org.openqa.selenium.support.ui.ExpectedConditions;

import org.openqa.selenium.*;

import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;

public class AppTest {
    AndroidDriver driver;

    @BeforeClass
    public void setUp() throws MalformedURLException, InterruptedException {

        DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability("platformName", "Android");
        cap.setCapability("platformVersion", "11");
        cap.setCapability("deviceName", "emulator-5554");
        cap.setCapability("appPackage", "com.wandoujia.phoenix2");
        cap.setCapability("appActivity", "com.pp.assistant.activity.PPMainActivity");
        cap.setCapability("unicodeKeyboard", "true");
        cap.setCapability("resetKeyboard", "true");
        cap.setCapability("noSign", "true");
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
        //隐式等待
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

    }

    @Test
    public void testNew() {
        //显示等待
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10), Duration.ofSeconds(1));
        WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("com.wandoujia.phoenix2:id/n8")));
        element.click();
        driver.pressKey(new KeyEvent(AndroidKey.BACK));
    }

    @AfterClass
    public void tearDown() {
        driver.quit();
    }


}

pom文件

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>app_ui</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>18</maven.compiler.source>
        <maven.compiler.target>18</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>9.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-remote-driver</artifactId>
            <version>4.9.1</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-api</artifactId>
            <version>4.9.0</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.4.0</version> <!-- 这里使用最新版本的 TestNG -->
            <scope>test</scope>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <!-- Maven Surefire 插件,用于运行 TestNG 测试 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version> <!-- 这里使用最新版本的 Maven Surefire 插件 -->
                <configuration>
                    <suiteXmlFiles>
                        <!-- 指定 TestNG 测试套件 XML 文件 -->
                        <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
相关推荐
听你说329 小时前
伊萨推出 ROBBI 360 协作机器人焊接工作站 简化自动化焊接部署流程
人工智能·机器人·自动化
jkyy201410 小时前
食物识别与卡路里估算技术:以视觉技术重构膳食健康管理新范式
人工智能·语言模型·自动化·健康医疗
ipad协议开发10 小时前
视频号直播间全功能自动化场控插件开发指南:基于 API 调用的全链路拆解
运维·微信·自动化·视频·ipad
智算菩萨10 小时前
【论文复现】Applied Intelligence 2025:Auto-PU正例无标签学习的自动化实现与GPT-5.4辅助编程实战
论文阅读·python·gpt·学习·自动化·复现
老神在在00111 小时前
【Selenium 自动化精讲】浏览器弹窗与登录界面的本质区别 & 实操指南
javascript·学习·selenium·测试工具·自动化
梦想的旅途211 小时前
效率革命:实现外部群聊信息的自动化同步方案
运维·自动化
SuperEugene12 小时前
Element Plus/VXE-Table UI 组件库规范:统一用法实战,避开样式冲突与维护混乱|工程化与协作规范篇
前端·javascript·vue.js·ui·前端框架·element plus·vxetable
橙露13 小时前
Shell 脚本实战:自动化备份、监控、告警脚本模板
运维·自动化·github
Agent产品评测局15 小时前
物流供应链自动化解决方案选型,全链路提效指南:从硬件集成到AI Agent的演进路径
运维·人工智能·ai·chatgpt·自动化
新缸中之脑15 小时前
用Stitch和AI Studio改造应用UI
人工智能·ui