appium想在自动化中集成录制功能,报错

实现app视频录制功能

这里是实现方法:写了一个装饰器

def recordvideo(func):
    '''
    实现运行录屏的方法
    '''
    project_root = os.path.abspath(os.path.dirname(__file__))
    video_path = os.path.join(project_root, "video")

    start_cmd = "adb shell screenrecord --bugreport /data/local/tmp/{name}.mp4".format(name=repr(func))
    pull_cmd = "adb pull /data/local/tmp/{name}.mp4 {video_path}".format(name=repr(func),video_path=video_path)
    clear_cmd = "adb shell rm /data/local/tmp/{name}.mp4".format(name=repr(func))

    def record():
        # 启动录屏 adb shell screenrecord --bugreport --time-limit 20 /data/local/tmp/用例名.mp4
        # os.system("adb shell screenrecord --bugreport /data/local/tmp/{name}.mp4".format(name=repr(func)))
        print("开始录制")
        P = subprocess.Popen(start_cmd, shell=True)
        # 运行用例
        print("运行用例")
        func()
        # 停止录制
        print("停止录制")
        os.kill(P.pid, signal.CTRL_C_EVENT)
        # 拉取视频 adb pull /data/local/tmp/用例名.mp4  ./
        print("拉去视频")
        P = subprocess.Popen(pull_cmd, shell=True)
        print("清理录制")
        # 清理手机上的视频
        P = subprocess.Popen(clear_cmd, shell=True)

    return record

运行时对指定的用例进行装饰

class TestDemo:

    def setup(self):
        self.main = App().start().main()

    def teardown(self):
        pass


    @recordvideo
    def test_demo(self):
        self.main.search()

实现后发现异常

test_demo.py::TestDemo::test_demo <- ..\utils\utils.py FAILED            [100%]
..\utils\utils.py:43 (TestDemo.test_demo)
cls = <class '_pytest.runner.CallInfo'>
func = <function call_runtest_hook.<locals>.<lambda> at 0x00000266F66C7AE8>
when = 'call'
reraise = (<class '_pytest.outcomes.Exit'>, <class 'KeyboardInterrupt'>)

    @classmethod
    def from_call(cls, func, when, reraise=None) -> "CallInfo":
        #: context of invocation: one of "setup", "call",
        #: "teardown", "memocollect"
        start = time()
        excinfo = None
        try:
>           result = func()

..\venv\lib\site-packages\_pytest\runner.py:244: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\venv\lib\site-packages\_pytest\runner.py:217: in <lambda>
    lambda: ihook(item=item, **kwds), when=when, reraise=reraise
..\venv\lib\site-packages\pluggy\hooks.py:286: in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
..\venv\lib\site-packages\pluggy\manager.py:93: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
..\venv\lib\site-packages\pluggy\manager.py:87: in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
..\venv\lib\site-packages\_pytest\runner.py:143: in pytest_runtest_call
    raise e
..\venv\lib\site-packages\_pytest\runner.py:135: in pytest_runtest_call
    item.runtest()
..\venv\lib\site-packages\_pytest\python.py:1479: in runtest
    self.ihook.pytest_pyfunc_call(pyfuncitem=self)
..\venv\lib\site-packages\pluggy\hooks.py:286: in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
..\venv\lib\site-packages\pluggy\manager.py:93: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
..\venv\lib\site-packages\pluggy\manager.py:87: in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

pyfuncitem = <Function test_demo>

    @hookimpl(trylast=True)
    def pytest_pyfunc_call(pyfuncitem: "Function"):
        testfunction = pyfuncitem.obj
        if iscoroutinefunction(testfunction) or (
            sys.version_info >= (3, 6) and inspect.isasyncgenfunction(testfunction)
        ):
            async_warn(pyfuncitem.nodeid)
        funcargs = pyfuncitem.funcargs
        testargs = {arg: funcargs[arg] for arg in pyfuncitem._fixtureinfo.argnames}
>       result = testfunction(**testargs)
E       TypeError: record() takes 0 positional arguments but 1 was given

..\venv\lib\site-packages\_pytest\python.py:184: TypeError

Assertion failed


================================== FAILURES ===================================
_____________________________ TestDemo.test_demo ______________________________

cls = <class '_pytest.runner.CallInfo'>
func = <function call_runtest_hook.<locals>.<lambda> at 0x00000266F66C7AE8>
when = 'call'
reraise = (<class '_pytest.outcomes.Exit'>, <class 'KeyboardInterrupt'>)

    @classmethod
    def from_call(cls, func, when, reraise=None) -> "CallInfo":
        #: context of invocation: one of "setup", "call",
        #: "teardown", "memocollect"
        start = time()
        excinfo = None
        try:
>           result = func()

..\venv\lib\site-packages\_pytest\runner.py:244: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\venv\lib\site-packages\_pytest\runner.py:217: in <lambda>
    lambda: ihook(item=item, **kwds), when=when, reraise=reraise
..\venv\lib\site-packages\pluggy\hooks.py:286: in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
..\venv\lib\site-packages\pluggy\manager.py:93: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
..\venv\lib\site-packages\pluggy\manager.py:87: in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
..\venv\lib\site-packages\_pytest\runner.py:143: in pytest_runtest_call
    raise e
..\venv\lib\site-packages\_pytest\runner.py:135: in pytest_runtest_call
    item.runtest()
..\venv\lib\site-packages\_pytest\python.py:1479: in runtest
    self.ihook.pytest_pyfunc_call(pyfuncitem=self)
..\venv\lib\site-packages\pluggy\hooks.py:286: in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
..\venv\lib\site-packages\pluggy\manager.py:93: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
..\venv\lib\site-packages\pluggy\manager.py:87: in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

pyfuncitem = <Function test_demo>

    @hookimpl(trylast=True)
    def pytest_pyfunc_call(pyfuncitem: "Function"):
        testfunction = pyfuncitem.obj
        if iscoroutinefunction(testfunction) or (
            sys.version_info >= (3, 6) and inspect.isasyncgenfunction(testfunction)
        ):
            async_warn(pyfuncitem.nodeid)
        funcargs = pyfuncitem.funcargs
        testargs = {arg: funcargs[arg] for arg in pyfuncitem._fixtureinfo.argnames}
>       result = testfunction(**testargs)
E       TypeError: record() takes 0 positional arguments but 1 was given

..\venv\lib\site-packages\_pytest\python.py:184: TypeError
=========================== short test summary info ===========================
FAILED test_demo.py::TestDemo::test_demo - TypeError: record() takes 0 positi...
============================= 1 failed in 11.68s ==============================

Process finished with exit code 1

你的装饰器在调用的时候会将原本方法的参数传进去,所以record方法需要有参数,如果使用非常固定的话按照你这个情况就写一个参数接收self就行了 或者通用一点就是写上*args, **kwargs 后续如果需要参数再从中获取处理