win10搭建Macaca环境及简单试用
各组件版本:
1 2 3 4 5 6 7 8 OS: win 10 java: 1.8.0_91 node:10.16.0 npm:6.9.0 Gradle:5.5.1 SDK Tools:25.2.5 SDK Platform-tools:29.0.1 SDK Build-tools:25.0.2
jdk和android环境配置就不说了,jdk用1.8的 android:
下载地址
1 2 ANDROID_HOME: D:\Android\android-sdk-windows Path: ;%ANDROID_HOME%\tools;%ANDROID_HOME%\platform-tools;%ANDROID_HOME%\build-tools;
node 安装稳定版
下载地址
验证:
1 2 3 4 5 6 7 E:\Macaca_workstation\run1 {git} {lamb} node -v v10.16.0 E:\Macaca_workstation\run1 {git} {lamb} npm -v 6.9.0
设置cache路径,node安装路径下,新建node-global
和 node-cache
1 2 3 4 5 E:\Macaca_workstation\run1 {git} {lamb} npm config set prefix "D:\dev\js\node-global" E:\Macaca_workstation\run1 {git} {lamb} npm config set cache "D:\dev\js\node-cache"
验证:
1 2 3 4 5 6 7 E:\Macaca_workstation\run1 {git} {lamb} npm config get prefix D:\dev\js\node-global E:\Macaca_workstation\run1 {git} {lamb} npm config get cache D:\dev\js\node-cache
虽然安装时候设置了node环境变量,还是建议将D:\dev\js\node-global
配置到path里面,防止后面用cnpm
时候报错
设置淘宝源:npm config set registry http://registry.npm.taobao.org
升级npm:npm install npm@latest -g
(可指定具体版本号)
gradle
下载地址
配置环境变量:
1 2 3 GRADLE_HOME: D:\QA\gradle-5.5.1 CLASSPATH: %GRADLE_HOME%\lib Path: %GRADLE_HOME%\bin;
验证:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 E:\Macaca_workstation\run1 {git} {lamb} gradle -v ------------------------------------------------------------ Gradle 5.5.1 ------------------------------------------------------------ Build time: 2019-07-10 20:38:12 UTC Revision: 3245f748c7061472da4dc184991919810f7935a5 Kotlin: 1.3.31 Groovy: 2.5.4 Ant: Apache Ant(TM) version 1.9.14 compiled on March 12 2019 JVM: 1.8.0_91 (Oracle Corporation 25.91-b14) OS: Windows 10 10.0 amd64
安装Macaca
检查:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 {lamb} macaca doctor macaca-doctor version: 2.0.18 Node.js checklist: node env: D:\dev\js\node.exe node version: v10.16.0 Android checklist: JAVA version is `1.8` JAVA_HOME is set to `D:\dev\java\jdk1.8.0_91` ANDROID_HOME is set to `D:\dev\android\android-sdk-windows` Platforms is set to `D:\dev\android\android-sdk-windows\platforms\android-28` ADB tool is set to `D:\dev\android\android-sdk-windows\platform-tools\adb.exe` GRADLE_HOME is set to `D:\QA\gradle-5.5.1` Installed driver list:
环境配置完成。。。
Not accepted Android SDK license agreements macaca doctor出现 “Not accepted Android SDK license agreements”(win 10)
terminal进入SDK/tools/bin目录(我的路径:D:\dev\android\android-sdk-windows\tools\bin),有个sdkmanager.bat
,执行sdkmanager.bat --update
,系统会让你确认,按y
就行,然后在D:\dev\android\android-sdk-windows\licenses
下就会生成licenses,继续执行macaca doctor
就不会出现那个红字
安装驱动:
嗯,报错:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 >> version: 2.33 >> chromedriver cdn url: https://chromedriver.storage.googleapis.com/2.33/chromedriver_win32.zip internal/streams/legacy.js:57 throw er; // Unhandled stream error in pipe. ^ Error: read ECONNRESET at TLSWrap.onStreamRead (internal/stream_base_commons.js:111:27) npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! macaca-chromedriver@1.0.45 install: `node ./bin/macaca-chromedriver install` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the macaca-chromedriver@1.0.45 install script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! D:\dev\js\node-cache\_logs\2019-07-25T00_47_29_733Z-debug.log
网上找了方法,换成
1 cnpm i macaca-android -g
然后,**’cnpm’ 不是内部或外部命令,也不是可运行的程序**。。。再装cnpm:
1 npm install -g cnpm --registry=https://registry.npm.taobao.org
装完验证:
1 2 3 4 5 6 7 8 9 E:\Macaca_workstation\run1 {git} {lamb} cnpm -v cnpm@6.1.0 (D:\dev\js\node-global\node_modules\cnpm\lib\parse_argv.js) npm@6.10.2 (D:\dev\js\node-global\node_modules\cnpm\node_modules\npm\lib\npm.js) node@10.16.0 (D:\dev\js\node.exe) npminstall@3.22.1 (D:\dev\js\node-global\node_modules\cnpm\node_modules\npminstall\lib\index.js) prefix=D:\dev\js\node-global win32 x64 10.0.17134 registry=https://r.npm.taobao.org
可以顺便macaca都这么装:
再装驱动:
1 2 3 4 5 cnpm i macaca-android -g cnpm i macaca-electron -g cnpm i app-inspector -g
Macaca sample-python 由于只认识python,老老实实的试python的sample吧,先去官方示例库下例子:
macaca-sample
下载下来之后,windows terminal进到目录里面,pip安装依赖
1 2 3 4 5 6 7 8 # 路径 E:\Macaca_workstation\sample-python {git} {lamb} pwd E:\Macaca_workstation\sample-python # 安装依赖 E:\Macaca_workstation\sample-python {git} {lamb} pip install -r requirements.txt
测试脚本稍微改了下,基本还是示例脚本:
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 58 59 60 61 62 63 64 65 66 67 68 69 #!/usr/bin/env python3 # -*-coding: utf-8 -*- """ @author: kyle @time: 2019/7/25 11:13 """ #coding:utf-8 import unittest import time from macaca import WebDriver from retrying import retry desired_caps = { 'platformName': 'android', 'app': 'F:/downloads/chrome/Macaca/android_app_bootstrap-debug.apk', } server_url = { 'hostname': 'localhost', 'port': 3456 } def switch_to_webview(driver): contexts = driver.contexts driver.context = contexts[-1] return driver def switch_to_native(driver): contexts = driver.contexts driver.context = contexts[0] return driver class MacacaTest(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = WebDriver(desired_caps, server_url) cls.initDriver() @classmethod def tearDownClass(cls): cls.driver.quit() @classmethod @retry def initDriver(cls): print("Retry connecting server...") cls.driver.init() def test_01_login(self): el = self.driver \ .elements_by_class_name('android.widget.EditText')[0] \ .send_keys('中文+Test+12345678') \ el = self.driver \ .elements_by_class_name('android.widget.EditText')[1] \ .send_keys('111111') time.sleep(1) # self.driver.keys(Keys.ENTER.value + Keys.ESCAPE.value) self.driver \ .element_by_name('Login') \ .click() if __name__ == '__main__': unittest.main()
顺便为了省事,直接把示例app先下载下来了:
android_app_bootstrap-debug.apk
启server: macaca server -p 3456 --verbose
(指定端口,打印详细日志)
执行测试脚本
查看结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 Testing started at 11:20 ... D:\dev\python\python.exe "D:\dev\pycharm\PyCharm Community Edition 2018.3.4\helpers\pycharm\_jb_pytest_runner.py" --path E:/Macaca_workstation/sample-python/test_run1.py Launching pytest with arguments E:/Macaca_workstation/sample-python/test_run1.py in E:\Macaca_workstation\sample-python ============================= test session starts ============================= platform win32 -- Python 3.6.6, pytest-4.6.3, py-1.8.0, pluggy-0.12.0 rootdir: E:\Macaca_workstation\sample-python plugins: allure-pytest-2.6.5, forked-1.0.2, html-1.21.0, metadata-1.8.0, rerunfailures-7.0, xdist-1.29.0collected 1 item test_run1.py [100%] ========================== 1 passed in 24.38 seconds ==========================Retry connecting server... . Process finished with exit code 0
试验结束。实话实说,国内的开源生态是真的一言难尽。。。这个环境加跑通结果,折腾了大半天,太多坑了,官方的教程真的无力吐槽。记录下搭建过程,有兴趣试这个的,少踩点坑。。。
浏览器也试了下,最简版本:
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 #!/usr/bin/env python3 # -*-coding: utf-8 -*- """ @author: kyle @time: 2019/7/25 10:42 """ import time import os from macaca import WebDriver,WebElement # 因为挂着梯子,顺便设置个代理 os.environ['https_proxy'] = 'https://127.0.0.1:1080' os.environ['http_proxy'] = 'http://127.0.0.1:1080' desired_caps = { 'autoAcceptAlerts' : True, 'browserName' : 'electron', 'platformName' : 'desktop' } driver = WebDriver(desired_caps) driver.init() driver.set_window_size(1280, 800) driver.get('https://www.baidu.com') driver.element_by_id("kw").send_keys('macaca') driver.element_by_id("su").click() time.sleep(5) driver.quit()