1.开启模拟器

2.开始service

3.在IDEA创建maven工程

添加阿里仓库和java client依赖到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>AndroidTestB</artifactId>
    <version>1.0-SNAPSHOT</version>

    <repositories>
        <repository>
            <id>aliyun-repos</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>

    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>aliyun-plugin</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

    <dependencies>
        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>7.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>RELEASE</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>


</project>

4 在Test包下创建测试case

 5.右键 run all test 运行

 

6.运行结果

-------------------------------------------------------------------------------------------------------

要求设置环形变量

Please set the ANDROID_HOME or ANDROID_SDK_ROOT environment variables to the corect Android SDK root directory path.

C:\Users\Administrator\AppData\Local\Android\Sdk

---------------------------------------------------------------------------------------------------------

获取控件Id工具

C:\Users\Administrator\AppData\Local\Android\Sdk\tools\bin\uiautomatorviewer

 

 

代码

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Before;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.*;

import java.net.MalformedURLException;
import java.net.URL;

public class TestAndroid {
    private AppiumDriver driver;

    @BeforeTest
    public void setup() throws MalformedURLException {
        //设置自动化相关参数
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
        capabilities.setCapability("platformName", "Android");//设备的系统
        capabilities.setCapability("deviceName", "emulator-5554");//设备名称
        capabilities.setCapability("Version", "10.0");//系统版本号
        capabilities.setCapability("appPackage", "com.iffy.module_view");//需要启动的包名
        capabilities.setCapability("appActivity", ".MainActivity");//需要启动的Activity
        capabilities.setCapability("unicodeKeyboard", true);//支持中文输入
        capabilities.setCapability("resetKeyboard", true);//支持中文输入
        driver =new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    }

    @Test
    public void myTest() throws InterruptedException {
        Thread.sleep(2000);
        //允许权限
        driver.findElementById("com.android.permissioncontroller:id/permission_allow_button").click();
        Thread.sleep(2000);
        driver.findElementById("com.iffy.module_view:id/button15").click();
        Thread.sleep(2000);
        assert driver.findElementById("com.iffy.module_view:id/neirong").getText().equals("第一页");
    }

    @AfterTest
    public void testend() throws Exception {
        driver.quit();
    }

}

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐