一 先配置好 Eclipse 及其相关的 maven 插件和 git 插件等和 android 开发环境

当然还有 ubuntu 环境下的 macaca-cli 和各种驱动 等 macaca 执行环境,请参考ubuntu16.04 环境折腾 macaca 总结记
此处省略,自行度娘 谷哥.许久之前就配置好了,这次是直接拿来用的.

二 创建 Macaca Java 项目的三种方式

2.1 Eclipse 导入已存在的 maven 项目

2.1.1 Eclipse 中 Git 仓库的 clone a git repository

Eclipse 中 Git 仓库的 clone a git repository 项目到本地 git 仓库
或者直接下载项目的 zip 包到本地解压,再导入已存在的 Maven 项目.下载地址 https://github.com/macacajs/wd.java

打开 Eclipse 导入 /home/cmd/workspace/git/wd.java/ 已存在的 Maven 项目. 参考 https://hacpai.com/article/1358680207381
首先开一终端 macaca server --verbose
然后选中项目 macacaclient 右键 Run as ->Maven Test. 这时 Eclipse Console 控制台会输出.

[INFO] Scanning for projects...

[INFO]                                                                         

[INFO] ------------------------------------------------------------------------

[INFO] Building macacaclient 1.0.0

[INFO] ------------------------------------------------------------------------

[INFO] 

[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ macacaclient ---

[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!

[INFO] Copying 1 resource

[INFO] 

[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ macacaclient ---

[INFO] Changes detected - recompiling the module!

[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!

[INFO] Compiling 20 source files to /home/cmd/workspace/git/wd.java/target/classes

[INFO] 

[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ macacaclient ---

[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!

[INFO] skip non existing resourceDirectory /home/cmd/workspace/git/wd.java/src/test/resources

[INFO] 

[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ macacaclient ---

[INFO] Changes detected - recompiling the module!

[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!

[INFO] Compiling 1 source file to /home/cmd/workspace/git/wd.java/target/test-classes

[INFO] 

[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ macacaclient ---

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-booter/2.12.4/surefire-booter-2.12.4.pom

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-booter/2.12.4/surefire-booter-2.12.4.pom (3 KB at 1.4 KB/sec)

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-api/2.12.4/surefire-api-2.12.4.pom

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-api/2.12.4/surefire-api-2.12.4.pom (3 KB at 1.9 KB/sec)

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/maven-surefire-common/2.12.4/maven-surefire-common-2.12.4.pom

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/maven-surefire-common/2.12.4/maven-surefire-common-2.12.4.pom (6 KB at 10.7 KB/sec)

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugin-tools/maven-plugin-annotations/3.1/maven-plugin-annotations-3.1.pom

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugin-tools/maven-plugin-annotations/3.1/maven-plugin-annotations-3.1.pom (2 KB at 3.1 KB/sec)

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugin-tools/maven-plugin-tools/3.1/maven-plugin-tools-3.1.pom

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugin-tools/maven-plugin-tools/3.1/maven-plugin-tools-3.1.pom (16 KB at 19.7 KB/sec)

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/reporting/maven-reporting-api/2.0.9/maven-reporting-api-2.0.9.pom

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/reporting/maven-reporting-api/2.0.9/maven-reporting-api-2.0.9.pom (2 KB at 3.1 KB/sec)

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/reporting/maven-reporting/2.0.9/maven-reporting-2.0.9.pom

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/reporting/maven-reporting/2.0.9/maven-reporting-2.0.9.pom (2 KB at 2.5 KB/sec)

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-toolchain/2.0.9/maven-toolchain-2.0.9.pom

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-toolchain/2.0.9/maven-toolchain-2.0.9.pom (4 KB at 5.9 KB/sec)

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/commons/commons-lang3/3.1/commons-lang3-3.1.pom

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/commons/commons-lang3/3.1/commons-lang3-3.1.pom (17 KB at 28.0 KB/sec)

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-common-artifact-filters/1.3/maven-common-artifact-filters-1.3.pom

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-common-artifact-filters/1.3/maven-common-artifact-filters-1.3.pom (4 KB at 6.3 KB/sec)

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-components/12/maven-shared-components-12.pom

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-shared-components/12/maven-shared-components-12.pom (10 KB at 15.8 KB/sec)

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/13/maven-parent-13.pom

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-parent/13/maven-parent-13.pom (23 KB at 38.0 KB/sec)

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/apache/6/apache-6.pom

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/apache/6/apache-6.pom (13 KB at 21.2 KB/sec)

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-container-default/1.0-alpha-9/plexus-container-default-1.0-alpha-9.pom

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-container-default/1.0-alpha-9/plexus-container-default-1.0-alpha-9.pom (2 KB at 1.1 KB/sec)

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-booter/2.12.4/surefire-booter-2.12.4.jar

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-booter/2.12.4/surefire-booter-2.12.4.jar (34 KB at 38.1 KB/sec)

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-api/2.12.4/surefire-api-2.12.4.jar

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-api/2.12.4/surefire-api-2.12.4.jar (115 KB at 67.9 KB/sec)

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/maven-surefire-common/2.12.4/maven-surefire-common-2.12.4.jar

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/maven-surefire-common/2.12.4/maven-surefire-common-2.12.4.jar (257 KB at 104.0 KB/sec)

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/commons/commons-lang3/3.1/commons-lang3-3.1.jar

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/commons/commons-lang3/3.1/commons-lang3-3.1.jar (309 KB at 31.2 KB/sec)

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-common-artifact-filters/1.3/maven-common-artifact-filters-1.3.jar

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/shared/maven-common-artifact-filters/1.3/maven-common-artifact-filters-1.3.jar (31 KB at 15.8 KB/sec)

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.8/plexus-utils-3.0.8.jar

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-utils/3.0.8/plexus-utils-3.0.8.jar (227 KB at 41.1 KB/sec)

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/reporting/maven-reporting-api/2.0.9/maven-reporting-api-2.0.9.jar

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/reporting/maven-reporting-api/2.0.9/maven-reporting-api-2.0.9.jar (10 KB at 17.3 KB/sec)

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/maven-toolchain/2.0.9/maven-toolchain-2.0.9.jar

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/maven-toolchain/2.0.9/maven-toolchain-2.0.9.jar (38 KB at 26.7 KB/sec)

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/plugin-tools/maven-plugin-annotations/3.1/maven-plugin-annotations-3.1.jar

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/plugin-tools/maven-plugin-annotations/3.1/maven-plugin-annotations-3.1.jar (14 KB at 23.9 KB/sec)

[INFO] Surefire report directory: /home/cmd/workspace/git/wd.java/target/surefire-reports

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.pom

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.pom (3 KB at 2.8 KB/sec)

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-providers/2.12.4/surefire-providers-2.12.4.pom

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-providers/2.12.4/surefire-providers-2.12.4.pom (3 KB at 4.2 KB/sec)

[INFO] Downloading: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.jar

[INFO] Downloaded: https://repo.maven.apache.org/maven2/org/apache/maven/surefire/surefire-junit4/2.12.4/surefire-junit4-2.12.4.jar (37 KB at 31.6 KB/sec)


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

 T E S T S

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

Running macaca.client.SampleTest

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.637 sec

Results :
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 41.531 s

[INFO] Finished at: 2016-08-19T11:02:45+08:00

[INFO] Final Memory: 18M/293M

[INFO] ------------------------------------------------------------------------

如此便执行跑成功了原项目里自带的 SampleTest.java 。说明运行环境搭建正常。

2.1.2 git clone 方式 wd.java

cmd@TR:~/workspace/git/wd.java$ cd ..
cmd@TR:~/workspace/git$ ls -al
总用量 8
drwxrwxrwx 2 cmd cmd 4096 8月  23 13:54 .
drwxrwxr-x 6 cmd cmd 4096 8月  19 11:12 ..
cmd@TR:~/workspace/git$ git clone https://github.com/macacajs/wd.java.git
正克隆到 'wd.java'...
remote: Counting objects: 977, done.
remote: Compressing objects: 100% (221/221), done.
remote: Total 977 (delta 174), reused 0 (delta 0), pack-reused 750
接收对象中: 100% (977/977), 294.54 KiB | 343.00 KiB/s, 完成.
处理 delta 中: 100% (691/691), 完成.
检查连接... 完成。
cmd@TR:~/workspace/git$ cd wd.java/
cmd@TR:~/workspace/git/wd.java$ ls -al
总用量 44
drwxrwxr-x 5 cmd cmd 4096 8月  23 13:55 .
drwxrwxrwx 3 cmd cmd 4096 8月  23 13:55 ..
drwxrwxr-x 4 cmd cmd 4096 8月  23 13:55 doc
drwxrwxr-x 8 cmd cmd 4096 8月  23 13:55 .git
-rw-rw-r-- 1 cmd cmd  100 8月  23 13:55 .gitignore
-rw-rw-r-- 1 cmd cmd 1100 8月  23 13:55 LICENSE
-rw-rw-r-- 1 cmd cmd 1312 8月  23 13:55 pom.xml
-rw-rw-r-- 1 cmd cmd  591 8月  23 13:55 README.md
-rw-rw-r-- 1 cmd cmd 1074 8月  23 13:55 settings.xml
drwxrwxr-x 4 cmd cmd 4096 8月  23 13:55 src
-rw-rw-r-- 1 cmd cmd  114 8月  23 13:55 .travis.yml
cmd@TR:~/workspace/git/wd.java$ git pull
Already up-to-date.
cmd@TR:~/workspace/git/wd.java$ mvn -s settings.xml clean install
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building macacaclient 1.0.0
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ macacaclient ---
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ macacaclient ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.2:compile (default-compile) @ macacaclient ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 21 source files to /home/cmd/workspace/git/wd.java/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ macacaclient ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory /home/cmd/workspace/git/wd.java/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.2:testCompile (default-testCompile) @ macacaclient ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /home/cmd/workspace/git/wd.java/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.17:test (default-test) @ macacaclient ---
[INFO] Surefire report directory: /home/cmd/workspace/git/wd.java/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running macaca.client.SampleTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.346 sec - in macaca.client.SampleTest

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ macacaclient ---
[INFO] Building jar: /home/cmd/workspace/git/wd.java/target/macacaclient-1.0.0.jar
[INFO] 
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ macacaclient ---
[INFO] Installing /home/cmd/workspace/git/wd.java/target/macacaclient-1.0.0.jar to /home/cmd/.m2/repository/macaca/webdriver/client/macacaclient/1.0.0/macacaclient-1.0.0.jar
[INFO] Installing /home/cmd/workspace/git/wd.java/pom.xml to /home/cmd/.m2/repository/macaca/webdriver/client/macacaclient/1.0.0/macacaclient-1.0.0.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.910 s
[INFO] Finished at: 2016-08-23T13:58:10+08:00
[INFO] Final Memory: 21M/305M
[INFO] ------------------------------------------------------------------------

如此也可执行跑成功了原项目里自带的 SampleTest.java 。说明运行环境搭建正常。
以上 2.1 节的构建项目方式的好处就是可以调试 debug wd.java 提供的源码或改造源码,进而更打造适合本公司项目相结合的 UI 自动化。

利用 mvn 命令行跑脚本

1 编辑修改 SampleTest.java
https://github.com/macacajs/macaca-test-sample-java/blob/master/src/test/java/macaca/client/SampleTest.java 替换 SampleTest.java
这里改成 porps.put("browserName", "chrome"); 或者完全改成自己项目的脚本都可以。

2 参考官方说明执行
https://github.com/macacajs/macaca-test-sample-java

先 开一终端

cmd@TR:~$ macaca server --verbose

>> index.js:17:12 [master] pid:1362 webdriver server start with config:

 { port: 3456,

  verbose: true,

  always: true,

  ip: '192.168.66.113',

  host: 'TR',

  loaded_time: '2016-08-19 11:21:51' }

>> middlewares.js:17:10 [master] pid:1362 base middlewares attached

>> router.js:108:10 [master] pid:1362 router set

>> webdriver sdk launched

然后开另一终端

cmd@TR:~/workspace/git/wd.java$ mvn -s settings.xml clean install

程序“mvn”尚未安装。 您可以使用以下命令安装:

sudo apt install maven

cmd@TR:~/workspace/git/wd.java$ sudo apt install maven

最后再执行 cmd@TR:~/workspace/git/wd.java$ mvn -s settings.xml clean install
再 cmd@TR:~/workspace/git/wd.java$ mvn test   

日志略。
这样就成功运行了脚本 SampleTest.java 了.然后你就可以改造来跑自己项目的脚本了.

你可以修改 SampleTest.java 也可以新建新的 java class 测试类,然后拷贝到项目环境/home/cmd/workspace/git/wd.java/ 执行 mvn test -Dtest=macaca.client.H5MobileTest 来运行。

2.2 git clone 方式 macaca-test-sample-java

cmd@TR:~/macaca$  git clone  https://github.com/macacajs/macaca-test-sample-java.git
正克隆到 'macaca-test-sample-java'...
remote: Counting objects: 72, done.
remote: Compressing objects: 100% (26/26), done.
remote: Total 72 (delta 27), reused 69 (delta 26), pack-reused 0
展开对象中: 100% (72/72), 完成.
检查连接... 完成。
cmd@TR:~/macaca$ cd macaca-test-sample-java
cmd@TR:~/macaca/macaca-test-sample-java$ ls -al
总用量 44
drwxrwxr-x 4 cmd cmd 4096 8月  23 12:53 .
drwxr-xr-x 4 cmd cmd 4096 8月  23 12:53 ..
drwxrwxr-x 8 cmd cmd 4096 8月  23 12:53 .git
-rw-rw-r-- 1 cmd cmd  100 8月  23 12:53 .gitignore
-rw-rw-r-- 1 cmd cmd 1100 8月  23 12:53 LICENSE
-rw-rw-r-- 1 cmd cmd  229 8月  23 12:53 Makefile
-rw-rw-r-- 1 cmd cmd 1383 8月  23 12:53 pom.xml
-rw-rw-r-- 1 cmd cmd  439 8月  23 12:53 README.md
-rw-rw-r-- 1 cmd cmd  910 8月  23 12:53 settings.xml
drwxrwxr-x 3 cmd cmd 4096 8月  23 12:53 src
-rw-rw-r-- 1 cmd cmd  346 8月  23 12:53 .travis.yml

接下来就很简单了,
/home/cmd/macaca/macaca-test-sample-java/src/test/java/macaca/client 下修改 SampleTest.java 为 porps.put("browserName", "chrome");
然后就是
mvn -s settings.xml clean install
和 mvn test

所以还是此种方式好些,你可以在 Eclipse 编写 ***Test.java 或改写 SampleTest.java 然后复制到/home/cmd/macaca/macaca-test-sample-java/src/test/java/macaca/client 此处.
当有 settings.xml 更新时 只需要 git pull 来获取最新 和 mvn -s settings.xml clean install 来更新.原团队作者也建议如此弄,不建议在 Eclipse 中的 wd.java 项目下弄.

如果你在 Eclipse 自建的本地 maven 项目下或者是已导入已存在的 maven 项目下,自建了其它个人的 java 测试类如 H5MobileTest.java 或 H5WebTest.java 也可以复制到 macaca-test-sample-java/src/test/java/macaca/client 此处.但是要注意修改代码里的 package macaca.client; 保持与 macaca-test-sample-java 一致.这样也可以 mvn test -Dtest=macaca.client.H5MobileTest 来执行.

这种方式,个人感觉比较适合 macaca 测试执行服务环境的部署和维护。

2.3 maven 的 pom 和 settings 设置 创建个人的 macaca java 项目

已补充 ubuntu16.04 Eclipse 使用 Maven pom 和 settings 创建本地 macaca java 项目记

如果你不需要调试 debug wd.java 的源码,只想利用 macaca java 来编写 UI 自动化脚本,这种创建和维护方式是适合你的。

总结:

此三种方式均测试通过

git 仓库或 git clone 下来的 wd.java
Eclipse 构建项目方式的好处就是可以调试 debug wd.java 提供的源码或改造源码,进而更打造适合本公司项目相结合的 UI 自动化。

git clone 方式或直接下载解压 macaca-test-sample-java 的
这种方式,个人感觉比较适合 macaca 测试执行服务环境的部署和维护。可以只需要 macaca-cli macaca 各种驱动安装好 当然还有 mvn 等执行环境部署好后,直接
把编写好的 java 测试类,cp 到脚本目录/macaca/macaca-test-sample-java/src/test/java/macaca/client
cmd@TR:~/macaca/macaca-test-sample-java$ mvn test 或 mvn test -Dtest=macaca.client.H5MobileTest 即可执行了. (此处要注意修改 package macaca.client; )

maven 的 pom 和 settings 设置 创建个人的 macaca java 项目
如果你不需要调试 debug wd.java 的源码,只想利用 macaca java 来编写 UI 自动化脚本,这种创建和维护方式是适合你的。

不知道 理解的还 对不对 有高人的话还请指点

更多信息请参考 wiki 汇总

小马的 macaca 入门指引合集

更多的关于 wd.java 样例学习可参考 wd.java 的作者的样例:

https://github.com/Yinxl/bootstrap
这个例子很好 common 里 提取了一些公共 可复用部分


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