Macaca
Macaca是阿里开源的一套完整的自动化测试解决方案。同时支持PC和移动端测试,支持的语言有JS,Java,Python。本文介绍PC端。
安装
1.安装node.js。
2.安装管理工具CNPM
3.安装macaca
安装命令
cnpm i macaca-client -g
装完后查看版本
$ macaca -V
查看环境
$ macaca doctor
确保环境搭建成功。
4.安装macaca-cli
cnpm i macaca-cli -g
5.安装webdriver-client
cnpm install webdriver-client
6.安装maraca-electron
cnpm install macaca-electron -g
7.安装 macaca-chrome
cnpm install macaca-chrome -g
8.安装 macaca python client
python3 install wd
使用官方实例
先去我是链接下载官方实例代码。官方的代码使用的是基于electron开发的Macaca驱动,之后我们使用chrome。
下载好之后,执行脚本。
maraca run -o ./log.html -d ./macaca-test/macaca-desktop-sample.js
如果要使用chrome,修改脚本。

再执行脚本即可。
使用Python来编写脚本
Macaca只支持Python3.4以上版本,官方api。
启动Macaca服务
macaca server --verbose
看到以下输出后,即为成功。

下面编写python脚本,脚本基于python的单元测试框架unites,不熟悉的自行Google。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
__author__ = 'LiBin'
__mtime__ = '16/9/18'
┏┓ ┏┓
┏┛┻━━━┛┻┓
┃ ☃ ┃
┃ ┳┛ ┗┳ ┃
┃ ┻ ┃
┗━┓ ┏━┛
┃ ┗━━━┓
┃ 神兽保佑 ┣┓
┃ 永无BUG! ┏┛
┗┓┓┏━┳┓┏┛
┃┫┫ ┃┫┫
┗┻┛ ┗┻┛
"""
import unittest
from macaca import WebDriver
from time import sleep
desired_caps = {
'platformName': 'Desktop',
'browserName': 'Chrome',
}
server_url = {
'hostname': '10.1.1.1',
'port': 3456
}
class MacacaTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = WebDriver(desired_caps, server_url)
cls.driver.init()
@classmethod
def tearDownClass(cls):
cls.driver.quit()
def test_get_url(self):
self.driver.get('https://www.baidu.com')
self.assertEqual(self.driver.title, '百度一下,你就知道')
def test_search_macaca(self):
self.driver.element_by_id('kw').send_keys('macaca')
self.driver.element_by_id('su').click()
sleep(2)
title = self.driver.title
self.assertTrue('macaca', title)
if __name__ == '__main__':
unittest.main()
可以从脚本看出,macaca和selenium的api非常的相似,熟悉selenium的可以很快上手。
吐槽一下测试报告

macaca自带的这个报告真的是有些low。
网友评论
E
======================================================================
ERROR: setUpClass (__main__.MacacaTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\macaca\macaca-test\macaca-desktop-sample2.test.py", line 37, in setUpClass
cls.driver.init()
File "D:\Python\lib\site-packages\macaca\util.py", line 145, in fluent_interface
ret = func(instance, *args, **kwargs)
File "D:\Python\lib\site-packages\macaca\webdriver.py", line 148, in init
}, False)
File "D:\Python\lib\site-packages\macaca\webdriver.py", line 66, in _execute
res = self.remote_invoker.execute(command, data)
File "D:\Python\lib\site-packages\macaca\remote_invoker.py", line 110, in execute
return self._request(method, url, body)
File "D:\Python\lib\site-packages\macaca\remote_invoker.py", line 144, in _request
res = s.send(prepped, timeout=self._timeout or None)
File "D:\Python\lib\site-packages\requests\sessions.py", line 609, in send
r = adapter.send(request, **kwargs)
File "D:\Python\lib\site-packages\requests\adapters.py", line 473, in send
raise ConnectionError(err, request=request)
ConnectionError: ('Connection aborted.', BadStatusLine("''",))
----------------------------------------------------------------------
Ran 0 tests in 120.013s
FAILED (errors=1)
你阔以找找别的教程先玩着=。=