Mcaca+Python 测试环境搭建及上手

Macaca是一套面向用户端软件的测试解决方案,提供了自动化驱动,周边工具,集成方案,旨在解决终端上的测试、自动化、性能等方面的问题,很多人选择它的原因简单:轻量化(相比于appium),跨平台(windows,android,ios),持续集成的reliable(Reliable 是 Node.js 实现的分布式持续集成服务) ,元素查看app-inspector,支持用例录制的uirecorder;等等

Macaca环境搭建:

  1. 安装nodejs:   https://nodejs.org/en/             (安装这个原因不用说了吧)
  2. 安装cnpm切换淘宝源:  npm install -g cnpm --registry=https://registry.npm.taobao.org ;      (cnpm安装东西能快一点)
  3. 安装 macaca-cli:  cnpm i  -g macaca-cli       
  4. 安装webdriver:  cnpm i -g webdriver-client
  5. 安装electron:  cnpm i -g macaca-electron
  6. 安装chrome:  npm i macaca-chrome -g
  7. 安装chromedriver:  npm i macaca-chromrdrivet -g
  8. 安装Android:   npm i macaca-android -g
  9. 安装ios:   npm i macaca-ios -g
  10. 安装uirecorder(录制脚本):    npm i uirecorder -g
  11. 安装app-inspector(查看元素):    npm i app-inspector -g
  12. 安装Python API: pip install wd

安装完上面的命令,基本上Mcaca+python的环境已经搭建好了;如果是需要进行移动测试,安卓还需要安卓SDK;见我下面这张大图;

Mcaca+Python 测试环境搭建及上手_第1张图片

Macaca执行测试用例

  1. 启动macaca服务: macaca server --verbose;
    C:\Users\Administrator>macaca server --verbose
    >> index.js:17:12 [master] pid:7848 webdriver server start with config:
     { port: 3456,
      verbose: true,
      always: true,
      ip: '172.27.35.1',
      host: 'USER-6IG90K3098',
      loaded_time: '2017-02-08 15:43:03' }
    >> middlewares.js:17:10 [master] pid:7848 base middlewares attached
    >> router.js:127:10 [master] pid:7848 router set
    >> webdriver sdk launched

     

  2. 执行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.5-py3.5.egg\macaca\remote_invoker.py:110: R
    esourceWarning: unclosed 292, family=AddressFamily.AF_INET6, t
    ype=SocketKind.SOCK_STREAM, proto=0, laddr=('::1', 53248, 0, 0), raddr=('::1', 3
    456, 0, 0)>
    C:\Python35\lib\site-packages\wd-0.1.5-py3.5.egg\macaca\remote_invoker.py:110: R
    esourceWarning: unclosed 180, family=AddressFamily.AF_INET6, t
    ype=SocketKind.SOCK_STREAM, proto=0, laddr=('::1', 53251, 0, 0), raddr=('::1', 3
    456, 0, 0)>
    C:\Python35\lib\site-packages\wd-0.1.5-py3.5.egg\macaca\remote_invoker.py:110: R
    esourceWarning: unclosed 196, family=AddressFamily.AF_INET6, t
    ype=SocketKind.SOCK_STREAM, proto=0, laddr=('::1', 53252, 0, 0), raddr=('::1', 3
    456, 0, 0)>
    .C:\Python35\lib\site-packages\wd-0.1.5-py3.5.egg\macaca\remote_invoker.py:110:
    ResourceWarning: unclosed 292, family=AddressFamily.AF_INET6,
    type=SocketKind.SOCK_STREAM, proto=0, laddr=('::1', 53258, 0, 0), raddr=('::1',
    3456, 0, 0)>
    C:\Python35\lib\site-packages\wd-0.1.5-py3.5.egg\macaca\remote_invoker.py:110: R
    esourceWarning: unclosed 180, family=AddressFamily.AF_INET6, t
    ype=SocketKind.SOCK_STREAM, proto=0, laddr=('::1', 53259, 0, 0), raddr=('::1', 3
    456, 0, 0)>
    C:\Python35\lib\site-packages\wd-0.1.5-py3.5.egg\macaca\remote_invoker.py:110: R
    esourceWarning: unclosed 196, family=AddressFamily.AF_INET6, t
    ype=SocketKind.SOCK_STREAM, proto=0, laddr=('::1', 53265, 0, 0), raddr=('::1', 3
    456, 0, 0)>
    C:\Python35\lib\site-packages\wd-0.1.5-py3.5.egg\macaca\remote_invoker.py:110: R
    esourceWarning: unclosed 292, family=AddressFamily.AF_INET6, t
    ype=SocketKind.SOCK_STREAM, proto=0, laddr=('::1', 53266, 0, 0), raddr=('::1', 3
    456, 0, 0)>
    C:\Python35\lib\site-packages\wd-0.1.5-py3.5.egg\macaca\remote_invoker.py:110: R
    esourceWarning: unclosed 180, family=AddressFamily.AF_INET6, t
    ype=SocketKind.SOCK_STREAM, proto=0, laddr=('::1', 53284, 0, 0), raddr=('::1', 3
    456, 0, 0)>
    C:\Python35\lib\site-packages\wd-0.1.5-py3.5.egg\macaca\remote_invoker.py:110: R
    esourceWarning: unclosed 196, family=AddressFamily.AF_INET6, t
    ype=SocketKind.SOCK_STREAM, proto=0, laddr=('::1', 53285, 0, 0), raddr=('::1', 3
    456, 0, 0)>
    C:\Python35\lib\site-packages\wd-0.1.5-py3.5.egg\macaca\remote_invoker.py:110: R
    esourceWarning: unclosed 292, family=AddressFamily.AF_INET6, t
    ype=SocketKind.SOCK_STREAM, proto=0, laddr=('::1', 53286, 0, 0), raddr=('::1', 3
    456, 0, 0)>
    C:\Python35\lib\site-packages\wd-0.1.5-py3.5.egg\macaca\remote_invoker.py:110: R
    esourceWarning: unclosed 180, family=AddressFamily.AF_INET6, t
    ype=SocketKind.SOCK_STREAM, proto=0, laddr=('::1', 53287, 0, 0), raddr=('::1', 3
    456, 0, 0)>
    C:\Python35\lib\site-packages\wd-0.1.5-py3.5.egg\macaca\remote_invoker.py:110: R
    esourceWarning: unclosed 196, family=AddressFamily.AF_INET6, t
    ype=SocketKind.SOCK_STREAM, proto=0, laddr=('::1', 53292, 0, 0), raddr=('::1', 3
    456, 0, 0)>
    C:\Python35\lib\site-packages\wd-0.1.5-py3.5.egg\macaca\remote_invoker.py:110: R
    esourceWarning: unclosed 292, family=AddressFamily.AF_INET6, t
    ype=SocketKind.SOCK_STREAM, proto=0, laddr=('::1', 53293, 0, 0), raddr=('::1', 3
    456, 0, 0)>
    C:\Python35\lib\site-packages\wd-0.1.5-py3.5.egg\macaca\remote_invoker.py:110: R
    esourceWarning: unclosed 180, family=AddressFamily.AF_INET6, t
    ype=SocketKind.SOCK_STREAM, proto=0, laddr=('::1', 53294, 0, 0), raddr=('::1', 3
    456, 0, 0)>
    .C:\Python35\lib\site-packages\wd-0.1.5-py3.5.egg\macaca\remote_invoker.py:110:
    ResourceWarning: unclosed 196, family=AddressFamily.AF_INET6,
    type=SocketKind.SOCK_STREAM, proto=0, laddr=('::1', 53295, 0, 0), raddr=('::1',
    3456, 0, 0)>
    
    ----------------------------------------------------------------------
    Ran 2 tests in 14.592s
    
    OK

    执行成功如上图;

  3. 附上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后续再跟上;

你可能感兴趣的:(Mcaca+Python 测试环境搭建及上手)