最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Mac中搭建uiautomator2的开发环境

Mac crifan 1445浏览 0评论
折腾:
【未解决】用uiautomator2实现安卓手机打开支付宝自动收集蚂蚁森林能量
期间,先去mac中搭建此处用于后续通过uiautomator2去自动操作实现蚂蚁森林的能量的开发环境
想了想还是新建虚拟环境吧:
virtualenv venv
Using base prefix '/Users/xxx/.pyenv/versions/3.8.0/Python.framework/Versions/3.8'
New python executable in /Users/xxx/dev/crifan/python/AutoCollectAliPayAntForestEnery/venv/bin/python3.8
Also creating executable in /Users/xxx/dev/crifan/python/AutoCollectAliPayAntForestEnery/venv/bin/python
Installing setuptools, pip, wheel...
done.
继续:
安装uiautomator2
(venv)  xxx@xxx  ~/dev/crifan/python/AutoCollectAliPayAntForestEnery  pip install uiautomator2    
Collecting uiautomator2
  Downloading uiautomator2-2.11.2.tar.gz (674 kB)
     |████████████████████████████████| 674 kB 465 kB/s 
Collecting six
  Using cached six-1.15.0-py2.py3-none-any.whl (10 kB)
Collecting requests
  Using cached requests-2.24.0-py2.py3-none-any.whl (61 kB)
Collecting whichcraft
  Using cached whichcraft-0.6.1-py2.py3-none-any.whl (5.2 kB)
Collecting logzero~=1.5
  Using cached logzero-1.5.0-py2.py3-none-any.whl (14 kB)
Processing /Users/xxx/Library/Caches/pip/wheels/f1/5b/38/115b213dfbf5562108ea22df17c063f6378350205a819795aa/progress-1.5-py3-none-any.whl
Collecting retry~=0.9
  Using cached retry-0.9.2-py2.py3-none-any.whl (8.0 kB)
Collecting adbutils<1.0,>=0.8.2
  Downloading adbutils-0.8.2.tar.gz (156 kB)
     |████████████████████████████████| 156 kB 16.1 MB/s 
Collecting Deprecated~=1.2.6
  Downloading Deprecated-1.2.10-py2.py3-none-any.whl (8.7 kB)
Collecting Pillow
  Downloading Pillow-7.2.0-cp38-cp38-macosx_10_10_x86_64.whl (2.2 MB)
     |████████████████████████████████| 2.2 MB 10.2 MB/s 
Collecting lxml>=4.3
  Downloading lxml-4.5.2-cp38-cp38-macosx_10_9_x86_64.whl (4.5 MB)
     |████████████████████████████████| 4.5 MB 27.7 MB/s 
Collecting cached-property<2.0,>=1.5.1
  Using cached cached_property-1.5.1-py2.py3-none-any.whl (6.0 kB)
Collecting packaging~=20.3
  Downloading packaging-20.4-py2.py3-none-any.whl (37 kB)
Collecting certifi>=2017.4.17
  Using cached certifi-2020.6.20-py2.py3-none-any.whl (156 kB)
Collecting chardet<4,>=3.0.2
  Using cached chardet-3.0.4-py2.py3-none-any.whl (133 kB)
Collecting idna<3,>=2.5
  Using cached idna-2.10-py2.py3-none-any.whl (58 kB)
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1
  Using cached urllib3-1.25.10-py2.py3-none-any.whl (127 kB)
Collecting decorator>=3.4.2
  Downloading decorator-4.4.2-py2.py3-none-any.whl (9.2 kB)
Collecting py<2.0.0,>=1.4.26
  Downloading py-1.9.0-py2.py3-none-any.whl (99 kB)
     |████████████████████████████████| 99 kB 15.0 MB/s 
Collecting deprecation<3.0,>=2.0.6
  Downloading deprecation-2.1.0-py2.py3-none-any.whl (11 kB)
Processing /Users/xxx/Library/Caches/pip/wheels/78/03/b3/b323c1b0b5e6c10149d645e2ec8db14fd834a6e2c0f275bd45/apkutils2-1.0.0-py3-none-any.whl
Collecting wrapt<2,>=1.10
  Downloading wrapt-1.12.1.tar.gz (27 kB)
Collecting pyparsing>=2.0.2
  Downloading pyparsing-2.4.7-py2.py3-none-any.whl (67 kB)
     |████████████████████████████████| 67 kB 12.0 MB/s 
Collecting xmltodict
  Using cached xmltodict-0.12.0-py2.py3-none-any.whl (9.2 kB)
Collecting cigam
  Using cached cigam-0.0.3-py3-none-any.whl (3.8 kB)
Collecting pyelftools
  Using cached pyelftools-0.26-py2.py3-none-any.whl (136 kB)
Building wheels for collected packages: uiautomator2, adbutils, wrapt
  Building wheel for uiautomator2 (setup.py) ... done
  Created wheel for uiautomator2: filename=uiautomator2-2.11.2-py3-none-any.whl size=215120 sha256=8032be5932e883f0be5a801c5faddd3f12e255c358a9e2cc1cb2c9b6a566054d
  Stored in directory: /Users/xxx/Library/Caches/pip/wheels/b1/9f/74/6946b5ed784db80cf8d34b6ac17d6a343939e83850c2fd8a89
  Building wheel for adbutils (setup.py) ... done
  Created wheel for adbutils: filename=adbutils-0.8.2-py3-none-any.whl size=24045 sha256=d396c25ae608e43501584aa3986291527db7696d79540beb2f554a564fbf245a
  Stored in directory: /Users/xxx/Library/Caches/pip/wheels/5a/3e/87/e67829113fdd7e139f51181b2cd89c38484007434a6bb6c728
  Building wheel for wrapt (setup.py) ... done
  Created wheel for wrapt: filename=wrapt-1.12.1-cp38-cp38-macosx_10_14_x86_64.whl size=32570 sha256=b8902938053f947a19d164c1d55344e45e231f00987f4169d86945c35d5fdde9
  Stored in directory: /Users/xxx/Library/Caches/pip/wheels/5f/fd/9e/b6cf5890494cb8ef0b5eaff72e5d55a70fb56316007d6dfe73
Successfully built uiautomator2 adbutils wrapt
Installing collected packages: six, certifi, chardet, idna, urllib3, requests, whichcraft, logzero, progress, decorator, py, retry, pyparsing, packaging, deprecation, xmltodict, cigam, pyelftools, apkutils2, adbutils, wrapt, Deprecated, Pillow, lxml, cached-property, uiautomator2
Successfully installed Deprecated-1.2.10 Pillow-7.2.0 adbutils-0.8.2 apkutils2-1.0.0 cached-property-1.5.1 certifi-2020.6.20 chardet-3.0.4 cigam-0.0.3 decorator-4.4.2 deprecation-2.1.0 idna-2.10 logzero-1.5.0 lxml-4.5.2 packaging-20.4 progress-1.5 py-1.9.0 pyelftools-0.26 pyparsing-2.4.7 requests-2.24.0 retry-0.9.2 six-1.15.0 uiautomator2-2.11.2 urllib3-1.25.10 whichcraft-0.6.1 wrapt-1.12.1 xmltodict-0.12.0
然后写代码:
# Function: Use uiautomator2 auto collect AliPay AntForest energy
# Author: Crifan
# Update: 20200827


import sys
import os
curFolder = os.path.abspath(__file__)
parentFolder = os.path.dirname(curFolder)
parentParentFolder = os.path.dirname(parentFolder)
parentParentParentFolder = os.path.dirname(parentParentFolder)
sys.path.append(curFolder)
sys.path.append(parentFolder)
sys.path.append(parentParentFolder)
sys.path.append(parentParentParentFolder)


from datetime import datetime,timedelta
from datetime import time as datetimeTime
import time


import logging


from libs.crifanLogging import loggingInit


import uiautomator2 as u2
from uiautomator2.exceptions import XPathElementNotFoundError
from uiautomator2 import UiObjectNotFoundError


################################################################################
# Const & Config & Settings
################################################################################


AndroidDeviceId_Xiaomi9 = "8c8a4d4d"


LogRoot = "logs"
OutputRoot = "output"


################################################################################
# Util Functions
################################################################################


def datetimeToStr(inputDatetime, format="%Y%m%d_%H%M%S"):
    """Convert datetime to string


    Args:
        inputDatetime (datetime): datetime value
    Returns:
        str
    Raises:
    Examples:
        datetime.datetime(2020, 4, 21, 15, 44, 13, 2000) -> '20200421_154413'
    """
    datetimeStr = inputDatetime.strftime(format=format)
    # print("inputDatetime=%s -> datetimeStr=%s" % (inputDatetime, datetimeStr)) # 2020-04-21 15:08:59.787623
    return datetimeStr


def getCurDatetimeStr(outputFormat="%Y%m%d_%H%M%S"):
    """
    get current datetime then format to string


    eg:
        20171111_220722


    :param outputFormat: datetime output format
    :return: current datetime formatted string
    """
    curDatetime = datetime.now() # 2017-11-11 22:07:22.705101
    # curDatetimeStr = curDatetime.strftime(format=outputFormat) #'20171111_220722'
    curDatetimeStr = datetimeToStr(curDatetime)
    return curDatetimeStr


def getFilenameNoPointSuffix(curFilePath):
    """Get current filename without point and suffix


    Args:
        curFilePath (str): current file path. Normally can use __file__
    Returns:
        str, file name without .xxx
    Raises:
    Examples:
        input: /Users/xxx/pymitmdump/mitmdumpOtherApi.py 
        output: mitmdumpOtherApi
    """
    root, pointSuffix = os.path.splitext(curFilePath)
    curFilenameNoSuffix = root.split(os.path.sep)[-1]
    return curFilenameNoSuffix


def initLogging():
    curDatetimeStr = getCurDatetimeStr() # '20200827_xxxxxx'
    logFile = getFilenameNoPointSuffix(__file__) # 'AutoCollectAliPayAntForestEnery'
    # logFilename = "AutoCollectAliPayAntForestEnery_%s.log" % curDatetimeStr
    logFilename = "%s_%s.log" % (logFile, curDatetimeStr) # 'AutoCollectAliPayAntForestEnery_20200827_xxxxxx.log'
    logFullFile = os.path.join(LogRoot, logFilename) # 'logs/AutoCollectAliPayAntForestEnery_20200827_xxxxxx.log'
    loggingInit(logFullFile)


################################################################################
# Main
################################################################################


initLogging()


d = u2.connect(AndroidDeviceId_Xiaomi9)
d.debug = True
logging.info("d.info=%s", d.info)
已经可以实现:
基本的初始化了。
输出:
20200827 11:08:55 AutoCollectAliPayAntForestEnery.py:103  INFO    d.info={'currentPackageName': 'com.tencent.mm', 'displayHeight': 2135, 'displayRotation': 0, 'displaySizeDpX': 393, 'displaySizeDpY': 851, 'displayWidth': 1080, 'productName': 'cepheus', 'screenOn': True, 'sdkInt': 29, 'naturalOrientation': True}
接着,再去安装weditor
(venv)  xxx@xxx  ~/dev/crifan/python/AutoCollectAliPayAntForestEnery  pip install weditor         
Collecting weditor
  Downloading weditor-0.6.1-py3-none-any.whl (1.5 MB)
     |████████████████████████████████| 1.5 MB 268 kB/s 
Requirement already satisfied: six in ./venv/lib/python3.8/site-packages (from weditor) (1.15.0)
Requirement already satisfied: uiautomator2>=2.10.1 in ./venv/lib/python3.8/site-packages (from weditor) (2.11.2)
Requirement already satisfied: cached-property in ./venv/lib/python3.8/site-packages (from weditor) (1.5.1)
Collecting facebook-wda>=1.0.5
  Downloading facebook_wda-1.1.3-py3-none-any.whl (23 kB)
Collecting tornado>=4.3
  Downloading tornado-6.0.4.tar.gz (496 kB)
     |████████████████████████████████| 496 kB 15.7 MB/s 
Requirement already satisfied: logzero in ./venv/lib/python3.8/site-packages (from weditor) (1.5.0)
Requirement already satisfied: pillow in ./venv/lib/python3.8/site-packages (from weditor) (7.2.0)
Requirement already satisfied: packaging~=20.3 in ./venv/lib/python3.8/site-packages (from uiautomator2>=2.10.1->weditor) (20.4)
Requirement already satisfied: whichcraft in ./venv/lib/python3.8/site-packages (from uiautomator2>=2.10.1->weditor) (0.6.1)
Requirement already satisfied: adbutils<1.0,>=0.8.2 in ./venv/lib/python3.8/site-packages (from uiautomator2>=2.10.1->weditor) (0.8.2)
Requirement already satisfied: progress~=1.3 in ./venv/lib/python3.8/site-packages (from uiautomator2>=2.10.1->weditor) (1.5)
Requirement already satisfied: lxml>=4.3 in ./venv/lib/python3.8/site-packages (from uiautomator2>=2.10.1->weditor) (4.5.2)
Requirement already satisfied: Deprecated~=1.2.6 in ./venv/lib/python3.8/site-packages (from uiautomator2>=2.10.1->weditor) (1.2.10)
Requirement already satisfied: requests in ./venv/lib/python3.8/site-packages (from uiautomator2>=2.10.1->weditor) (2.24.0)
Requirement already satisfied: retry~=0.9 in ./venv/lib/python3.8/site-packages (from uiautomator2>=2.10.1->weditor) (0.9.2)
Collecting attrdict~=2.0.1
  Downloading attrdict-2.0.1-py2.py3-none-any.whl (9.9 kB)
Requirement already satisfied: pyparsing>=2.0.2 in ./venv/lib/python3.8/site-packages (from packaging~=20.3->uiautomator2>=2.10.1->weditor) (2.4.7)
Requirement already satisfied: apkutils2<2.0,>=1.0.0 in ./venv/lib/python3.8/site-packages (from adbutils<1.0,>=0.8.2->uiautomator2>=2.10.1->weditor) (1.0.0)
Requirement already satisfied: deprecation<3.0,>=2.0.6 in ./venv/lib/python3.8/site-packages (from adbutils<1.0,>=0.8.2->uiautomator2>=2.10.1->weditor) (2.1.0)
Requirement already satisfied: wrapt<2,>=1.10 in ./venv/lib/python3.8/site-packages (from Deprecated~=1.2.6->uiautomator2>=2.10.1->weditor) (1.12.1)
Requirement already satisfied: chardet<4,>=3.0.2 in ./venv/lib/python3.8/site-packages (from requests->uiautomator2>=2.10.1->weditor) (3.0.4)
Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in ./venv/lib/python3.8/site-packages (from requests->uiautomator2>=2.10.1->weditor) (1.25.10)
Requirement already satisfied: idna<3,>=2.5 in ./venv/lib/python3.8/site-packages (from requests->uiautomator2>=2.10.1->weditor) (2.10)
Requirement already satisfied: certifi>=2017.4.17 in ./venv/lib/python3.8/site-packages (from requests->uiautomator2>=2.10.1->weditor) (2020.6.20)
Requirement already satisfied: decorator>=3.4.2 in ./venv/lib/python3.8/site-packages (from retry~=0.9->uiautomator2>=2.10.1->weditor) (4.4.2)
Requirement already satisfied: py<2.0.0,>=1.4.26 in ./venv/lib/python3.8/site-packages (from retry~=0.9->uiautomator2>=2.10.1->weditor) (1.9.0)
Requirement already satisfied: xmltodict in ./venv/lib/python3.8/site-packages (from apkutils2<2.0,>=1.0.0->adbutils<1.0,>=0.8.2->uiautomator2>=2.10.1->weditor) (0.12.0)
Requirement already satisfied: cigam in ./venv/lib/python3.8/site-packages (from apkutils2<2.0,>=1.0.0->adbutils<1.0,>=0.8.2->uiautomator2>=2.10.1->weditor) (0.0.3)
Requirement already satisfied: pyelftools in ./venv/lib/python3.8/site-packages (from apkutils2<2.0,>=1.0.0->adbutils<1.0,>=0.8.2->uiautomator2>=2.10.1->weditor) (0.26)
Building wheels for collected packages: tornado
  Building wheel for tornado (setup.py) ... done
  Created wheel for tornado: filename=tornado-6.0.4-cp38-cp38-macosx_10_14_x86_64.whl size=417306 sha256=f8b495f2bb9449e7ab91346980210755f5954218d68e238e76391842d160df62
  Stored in directory: /Users/xxx/Library/Caches/pip/wheels/88/79/e5/598ba17e85eccf2626eab62e4ee8452895636cd542650d450d
Successfully built tornado
Installing collected packages: attrdict, facebook-wda, tornado, weditor
Successfully installed attrdict-2.0.1 facebook-wda-1.1.3 tornado-6.0.4 weditor-0.6.1
然后去启动试试
python -m weditor
打开:
http://localhost:17310/
看到手机中界面。
至此,开发环境搭建好了。

转载请注明:在路上 » 【已解决】Mac中搭建uiautomator2的开发环境

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.193 seconds, using 22.16MB memory