一、配置 Eclipse 及其相关的 maven 插件和 android 开发环境

此处无声胜有声,自行度娘或在 testerhome 搜索解决传送门

二、安装配置 nodejs

请参考传送门 三 安装配置 nodejs 的 1~4

三、配置淘宝 NPM 镜像

C:\Users\cmd>npm install -g cnpm --registry=https://registry.npm.taobao.org

四、安装配置 macaca

1. 安装 macaca

C:\Users\cmd>cnpm i macaca-cli -g

2. 安装 Android Driver

C:\Users\cmd>cnpm i macaca-android -g

五、创建 maven 项目

pom.xml 文件添加依赖包

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>macaca.webdriver.client</groupId>
    <artifactId>macacatestsample</artifactId>
    <packaging>jar</packaging>
    <version>1.0.0</version>
    <name>macacatestsample</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>macaca.webdriver.client</groupId>
            <artifactId>macacaclient</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.15</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-library</artifactId>
            <version>1.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

创建 java 类

package macaca.client;

import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.alibaba.fastjson.JSONObject;
import macaca.client.MacacaClient;

public class SampleTest {

    MacacaClient driver = new MacacaClient();

    @Before
    public void setUp() throws Exception {
        Logger logger = Logger.getLogger(getClass());
        JSONObject porps = new JSONObject();

        porps.put("platformName", "Android");//指定platformName为Android
        porps.put("package", "com.android.settings");//package
        porps.put("activity", ".SettingsMain");//activity
        JSONObject desiredCapabilities = new JSONObject();
        desiredCapabilities.put("desiredCapabilities", porps);
        driver.initDriver(desiredCapabilities);
    }

    @Test
    public void test_case_1() throws Exception {
        driver
            .elementByName("全部")
            .click()
            .elementByName("显示设置")
            .click();
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
    }
}

六、运行测试

####1. 运行 macaca

C:\Users\cmd>macaca server --verbose

####2. 右键测试类 Run As->JUnit Test


↙↙↙阅读原文可查看相关链接,并与作者交流