Macaca是一套面向用户端软件的测试解决方案,提供了自动化驱动,周边工具,集成方案,旨在解决终端
上的测试、自动化、性能等方面的问题,很多人选择它的原因简单:轻量化(相比于appium),跨平台(windows,android,ios),持续集成的reliable(Reliable 是 Node.js 实现的分布式持续集成服务) ,元素查看app-inspector,支持用例录制的uirecorder;等等
Macaca环境搭建:
- 安装nodejs: https://nodejs.org/en/ (安装这个原因不用说了吧)
- 安装cnpm切换淘宝源: npm install -g cnpm --registry=https://registry.npm.taobao.org ; (cnpm安装东西能快一点)
- 安装 macaca-cli: cnpm i -g macaca-cli
- 安装webdriver: cnpm i -g webdriver-client
- 安装electron: cnpm i -g macaca-electron
- 安装chrome: npm i macaca-chrome -g
- 安装chromedriver: npm i macaca-chromrdrivet -g
- 安装Android: npm i macaca-android -g
- 安装ios: npm i macaca-ios -g
- 安装uirecorder(录制脚本): npm i uirecorder -g
- 安装app-inspector(查看元素): npm i app-inspector -g
- 安装Python API: pip install wd
安装完上面的命令,基本上Mcaca+python的环境已经搭建好了;如果是需要进行移动测试,安卓还需要安卓SDK;见我下面这张大图;
Macaca执行测试用例
- 启动macaca服务: macaca server --verbose;
C:\Users\Administrator>macaca server --verbose
>> index.js:: [master] pid: webdriver server start with config:
{ port: ,
verbose: true,
always: true,
ip: '172.27.35.1',
host: 'USER-6IG90K3098',
loaded_time: '2017-02-08 15:43:03' }
>> middlewares.js:: [master] pid: base middlewares attached
>> router.js:: [master] pid: router set
>> webdriver sdk launched - 执行Python测试用例:python baidu.py;
C:\Users\Administrator>d: D:\>cd D:\zsfile\macaca-test-sample-python-master\tests D:\zsfile\macaca-test-sample-python-master\tests>python baidu.py
C:\Python35\lib\site-packages\wd-0.1.-py3..egg\macaca\remote_invoker.py:: R
esourceWarning: unclosed <socket.socket fd=, family=AddressFamily.AF_INET6, t
ype=SocketKind.SOCK_STREAM, proto=, laddr=('::1', , , ), raddr=('::1',
, , )>
C:\Python35\lib\site-packages\wd-0.1.-py3..egg\macaca\remote_invoker.py:: R
esourceWarning: unclosed <socket.socket fd=, family=AddressFamily.AF_INET6, t
ype=SocketKind.SOCK_STREAM, proto=, laddr=('::1', , , ), raddr=('::1',
, , )>
C:\Python35\lib\site-packages\wd-0.1.-py3..egg\macaca\remote_invoker.py:: R
esourceWarning: unclosed <socket.socket fd=, family=AddressFamily.AF_INET6, t
ype=SocketKind.SOCK_STREAM, proto=, laddr=('::1', , , ), raddr=('::1',
, , )>
.C:\Python35\lib\site-packages\wd-0.1.-py3..egg\macaca\remote_invoker.py::
ResourceWarning: unclosed <socket.socket fd=, family=AddressFamily.AF_INET6,
type=SocketKind.SOCK_STREAM, proto=, laddr=('::1', , , ), raddr=('::1',
, , )>
C:\Python35\lib\site-packages\wd-0.1.-py3..egg\macaca\remote_invoker.py:: R
esourceWarning: unclosed <socket.socket fd=, family=AddressFamily.AF_INET6, t
ype=SocketKind.SOCK_STREAM, proto=, laddr=('::1', , , ), raddr=('::1',
, , )>
C:\Python35\lib\site-packages\wd-0.1.-py3..egg\macaca\remote_invoker.py:: R
esourceWarning: unclosed <socket.socket fd=, family=AddressFamily.AF_INET6, t
ype=SocketKind.SOCK_STREAM, proto=, laddr=('::1', , , ), raddr=('::1',
, , )>
C:\Python35\lib\site-packages\wd-0.1.-py3..egg\macaca\remote_invoker.py:: R
esourceWarning: unclosed <socket.socket fd=, family=AddressFamily.AF_INET6, t
ype=SocketKind.SOCK_STREAM, proto=, laddr=('::1', , , ), raddr=('::1',
, , )>
C:\Python35\lib\site-packages\wd-0.1.-py3..egg\macaca\remote_invoker.py:: R
esourceWarning: unclosed <socket.socket fd=, family=AddressFamily.AF_INET6, t
ype=SocketKind.SOCK_STREAM, proto=, laddr=('::1', , , ), raddr=('::1',
, , )>
C:\Python35\lib\site-packages\wd-0.1.-py3..egg\macaca\remote_invoker.py:: R
esourceWarning: unclosed <socket.socket fd=, family=AddressFamily.AF_INET6, t
ype=SocketKind.SOCK_STREAM, proto=, laddr=('::1', , , ), raddr=('::1',
, , )>
C:\Python35\lib\site-packages\wd-0.1.-py3..egg\macaca\remote_invoker.py:: R
esourceWarning: unclosed <socket.socket fd=, family=AddressFamily.AF_INET6, t
ype=SocketKind.SOCK_STREAM, proto=, laddr=('::1', , , ), raddr=('::1',
, , )>
C:\Python35\lib\site-packages\wd-0.1.-py3..egg\macaca\remote_invoker.py:: R
esourceWarning: unclosed <socket.socket fd=, family=AddressFamily.AF_INET6, t
ype=SocketKind.SOCK_STREAM, proto=, laddr=('::1', , , ), raddr=('::1',
, , )>
C:\Python35\lib\site-packages\wd-0.1.-py3..egg\macaca\remote_invoker.py:: R
esourceWarning: unclosed <socket.socket fd=, family=AddressFamily.AF_INET6, t
ype=SocketKind.SOCK_STREAM, proto=, laddr=('::1', , , ), raddr=('::1',
, , )>
C:\Python35\lib\site-packages\wd-0.1.-py3..egg\macaca\remote_invoker.py:: R
esourceWarning: unclosed <socket.socket fd=, family=AddressFamily.AF_INET6, t
ype=SocketKind.SOCK_STREAM, proto=, laddr=('::1', , , ), raddr=('::1',
, , )>
C:\Python35\lib\site-packages\wd-0.1.-py3..egg\macaca\remote_invoker.py:: R
esourceWarning: unclosed <socket.socket fd=, family=AddressFamily.AF_INET6, t
ype=SocketKind.SOCK_STREAM, proto=, laddr=('::1', , , ), raddr=('::1',
, , )>
.C:\Python35\lib\site-packages\wd-0.1.-py3..egg\macaca\remote_invoker.py::
ResourceWarning: unclosed <socket.socket fd=, family=AddressFamily.AF_INET6,
type=SocketKind.SOCK_STREAM, proto=, laddr=('::1', , , ), raddr=('::1',
, , )> ----------------------------------------------------------------------
Ran tests in .592s OK执行成功如上图;
- 附上baidu.py代码,用例为测试百度查询;
# coding=utf-8
import unittest
import time
from macaca import WebDriver
from macaca import WebElement desired_caps = {
'platformName': 'desktop',
'browserName': 'electron'
} server_url = {
'hostname': 'localhost',
'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.set_window_size(1280, 800)
self.driver.get("https://www.baidu.com") def test_search_macaca(self):
self.driver.element_by_id('kw').send_keys('macaca')
self.driver.element_by_id('su').click()
time.sleep(5)
html = self.driver.source
self.assertTrue('macaca'in html)
self.assertTrue(self.driver.element_by_css_selector_if_exists(
'#head>div.head_wrapper'))
self.driver.element_by_xpath_or_none(
'//*[@id="kw"]').send_keys('elementByXPath')
self.driver.element_by_id('su').click()
self.driver.take_screenshot()
if __name__ == '__main__':
unittest.main()基本上桌面上的就是这样了;android和ios后续再跟上;