Macaca win7 环境 eclipse + android app java 使用记

拖拉机 · 2016年08月23日 · 最后由 huang 回复于 2016年09月30日 · 1294 次阅读

一、配置 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

共收到 4 条回复 时间 点赞
老马 [该话题已被删除] 中提及了此贴 08月23日 16:00
老马 [该话题已被删除] 中提及了此贴 08月23日 16:07


请问楼主,运行以后,chrome 根本不启动,控制台输出了这些怎么破

@haiquan180 我试了下你这种方式创建 maven 项目 是不行的 因为你的/src/main/java 下是空的
但代码里 有 import macaca.client.MacacaClient; 会提示找不到报错的。

@haiquan180 你的本地项目里的 setting.xml 是什么样的

@harsayer 执行你给的测试类,结果报这样的异常了

session.js:39:14 [master] pid:74112 Error: Cannot find module 'macaca-android
'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at detectDevice (D:\Program Files\nodejs\node-global\node_modules\macaca-cli
\node_modules\webdriver-server\lib\server\controllers\session.js:36:20)
at Object.createDevice (D:\Program Files\nodejs\node-global\node_modules\mac
aca-cli\node_modules\webdriver-server\lib\server\controllers\session.js:10:18)
at next (native)
at onFulfilled (D:\Program Files\nodejs\node-global\node_modules\macaca-cli\
node_modules\co\index.js:65:19)
at D:\Program Files\nodejs\node-global\node_modules\macaca-cli\node_modules\
co\index.js:54:5
at Object.co (D:\Program Files\nodejs\node-global\node_modules\macaca-cli\no
de_modules\co\index.js:50:10)

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册