• 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】

    {
      "platformName": "Android",
      "platformVersion": "11",
      "deviceName": "emulator-5554",
      "appPackage": "com.wandoujia.phoenix2",
      "appActivity": "com.pp.assistant.activity.PPMainActivity"
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述
    连接后页面
    在这里插入图片描述

    java代码实现-第一版

    pom

     <dependency>
                <groupId>io.appiumgroupId>
                <artifactId>java-clientartifactId>
                <version>9.1.0version>
            dependency>
            <dependency>
                <groupId>org.seleniumhq.seleniumgroupId>
                <artifactId>selenium-remote-driverartifactId>
                <version>4.9.1version>
            dependency>
            <dependency>
                <groupId>org.seleniumhq.seleniumgroupId>
                <artifactId>selenium-apiartifactId>
                <version>4.9.0version>
            dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    查找比较新的jar地址
    阿里云maven仓库
    在这里插入图片描述

    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();
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    在这里插入图片描述

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

    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();
        }
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    pom文件

    
    <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.0modelVersion>
    
        <groupId>org.examplegroupId>
        <artifactId>app_uiartifactId>
        <version>1.0-SNAPSHOTversion>
    
        <properties>
            <maven.compiler.source>18maven.compiler.source>
            <maven.compiler.target>18maven.compiler.target>
        properties>
        <dependencies>
            <dependency>
                <groupId>io.appiumgroupId>
                <artifactId>java-clientartifactId>
                <version>9.1.0version>
            dependency>
            <dependency>
                <groupId>org.seleniumhq.seleniumgroupId>
                <artifactId>selenium-remote-driverartifactId>
                <version>4.9.1version>
            dependency>
            <dependency>
                <groupId>org.seleniumhq.seleniumgroupId>
                <artifactId>selenium-apiartifactId>
                <version>4.9.0version>
            dependency>
            <dependency>
                <groupId>org.testnggroupId>
                <artifactId>testngartifactId>
                <version>7.4.0version> 
                <scope>testscope>
            dependency>
        dependencies>
    
    
        <build>
            <plugins>
                
                <plugin>
                    <groupId>org.apache.maven.pluginsgroupId>
                    <artifactId>maven-surefire-pluginartifactId>
                    <version>3.0.0-M5version> 
                    <configuration>
                        <suiteXmlFiles>
                            
                            <suiteXmlFile>src/test/resources/testng.xmlsuiteXmlFile>
                        suiteXmlFiles>
                    configuration>
                plugin>
            plugins>
        build>
    
    project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
  • 相关阅读:
    生命形式问题
    RASP技术进阶系列(二):东西向Web流量的智能检测防御
    汇编语言与微机原理 期末复习题整理(大题)
    JAVA财务管理系统的设计与实现计算机毕业设计Mybatis+系统+数据库+调试部署
    Intellij IDEA--格式化SQL文件的方法
    关于产品和消费的思考
    会议OA之我的会议(排座&送审)
    Spring框架之IOC容器的底层原理(一篇就会)
    HBase面试题
    INVETA peer-stream 自述文件
  • 原文地址:https://blog.csdn.net/xiancaolj/article/details/136239926